how to configure log4j2.xml for non-spring projects - log4j2

I understand in spring projects, if we put the "log4j2.xml" under resources it will be picked up.
Mine is not spring project, but how do i configure "log4j2.xml" in java code ?

Log4j looks for a file named log4j2.xml (or log4j2.yaml, log4j2.json, or log4j2.properties) on the classpath. If you are using Maven to build your project then placing the file in the src/main/resources directory will accomplish that. If you want it to come from a different location you can set the log4j2.configurationFile system property before log4j starts and it will retrieve the file from that location. If you do not want to set a system property you can also use one of the initialize methods in Log4j's Configurator class.

Related

How to run GrailsPlugin.groovy during 'war'

Grails Version: 2.3.11
Dear all,
I have a plugin where in the doWithSpring Closure I use some central resources to build up a properties file in the war that is then used during runtime. The central resources will only be available when building the war in a production environment so putting the files into the /src (for example) of the plugin is out of the question.
Currently my plugin will work in a run-app and testing environment, but the file is never created when 'war' is run.
I have tried setting the scopes variable to include war but this doesn't appear to be doing much (or at least, is not invoking the doWithSpring closure). Does anyone know how to do this or if it is possible?
All of the logic is within doWithSpring (which delegates to a class) but that is only using the classLoader and grailsApplication so if there is anywhere else I can do this that will also be great.
Thanks
Adam
Okay, the best approach is to hook into the events with your plugin. I highly recommend you read the documentation on events. It's as simple as creating the correct file within your plugin and placing your code within the associated closure.
For example:
scripts/_Events.groovy
eventCreateWarStart = { warName, stagingDir ->
// place your code here.
}

How can a Grails plugin modify the app's default mappings/constraints config?

I'm creating a Grails plugin which will modify the value of the following config property:
grails.gorm.default.constraints
The problem is that by the time my plugin descriptor starts running (doWithSpring) if the Grails application that uses the plugin had an existing value for the default constraints property, it would have been already executed.
I'd like my plugin to modify the value of the default constraints before Grails begins executing it so that the constraints I'm adding will also be included. The default constraints closure seems to get executed multiple times during Grails app startup.
I've tried a few approaches:
Use the platform-core plugin which this post talks about: How to configure a grails plugin from another grails plugin.
Problem there is similar: the Grails app's default constraints block is executed at least once before the plugin's doWithConfig begins to run.
(Hackish) Modify Grails app to include a FooConfig.groovy in its "locations", a file which exists in the plugin's grails-app/conf/ dir and thus accessible on the classpath (see below).
Problem: Hackish, still working through it but might be my only option.
grails.config.locations = ["classpath:FooConfig.class"] // Yes, *.class

Grails: Using "external configuration" to get a plugin's data source

I'm building a plugin that will contain sharable code and configuration for several applications. One things that I'm trying to share is the data source information. Basically I need the application to not have to define it's own data source and instead use the plugin's data source. The best way that I can think of doing this is to take advantage of the external configuration functionality that's available in Grails (http://grails.org/doc/latest/guide/conf.html#3.4%20Externalized%20Configuration). However, I'm not exactly sure how to do this. All the examples I can find online show you how to do this when using an external file on the file system somewhere. I want to use configuration files from the plugin.
According to the documentation linked to above you can specify a "config script" class to use like this:
grails.config.locations = [com.my.app.MyConfig]
This would probably work, however, I can't find documentation on what a "config script" class actually is and how to create one.
By default, the DataSource file of your plugin will not be used (it's ignored in the package stage) but you can create another file that ends with "DataSource" (eg MyPluginDataSource). This is also true for BootStrap and Config.
You will probably need to leave the application DataSource file empty.

How to configure db-reverse-engineer plugin

I am a total Grails noob trying to configure the db-reverse-engineer plugin for my first project. Documentation for the plugin indicates that I need to configure it, but I don't see where I am supposed to edit configuration.
Is there a configuration file in my project I need to edit? I have searched through the ./grails-app/conf folder for grails.plugin (the prefix for this plugin's configuration) and found nothing. An SO or Google search for how to configure grails plugins also returns void. I know this is a lame question, but how do I configure this plugin? Is there a UI I need to use, or are there files somewhere to edit?
You need to configure your database in grails-app/conf/DataSource.groovy. In particular, you'll need to provide the JDBC URL, the database dialect and the databases's username and password.
You'll also have to add some extra db-reverse-engineer configuration to grails-app/conf/Config.groovy. This file will already exist. Just append the new properties at the end.
Finally, run the reverse engineer script to generate your domain classes:
grails db-reverse-engineer
The right place for that would be the file grails-app/conf/Config.groovy.
Just add what you need at the bottom.

How can I access a file in my Grails plugin from a src file in the same plugin?

I'm working on a JavaScript testing plugin for Grails. I wrote some Groovy classes to perform the testing that I've stored in my src/groovy folder. I hook into the testing events in my plugin's _Events.groovy script and inject an instance of the test runner. From that instance of the test runner, I need to access the JavaScript files, which I've stored in src/js, to perform the testing.
The plugin documentation specifies a way to get the path from my Gant scripts, but that doesn't work elsewhere. I've also tried to get access to the GrailsApplication via grailsApplication or ApplicationHolder, but I get null. Finally, I've tried accessing BuildSettings and ConfigurationHolder, but those show me an empty configuration.
To make my plugin work, I am currently copying the JavaScript files into the application's test/resources folder so it's in a known location relative to the working directory, which I'm assuming is the project folder. This feels invasive and fragile to me, so I'd like to figure out a "right" way.
How can I get a path to my plugin from my test runner so I can find those files?
If you have the BuildSettings and the pluginManager bean (either dependency-injected with def pluginManager or via PluginManagerHolder) then you can get the path with
new File(buildSettings.projectPluginsDir, pluginManager.getPluginPath('foo'))

Resources