How does a Grails plugin add its own logback.groovy? - grails

Just like to define custom Bootstrap.groovy or UrlMappings.groovy in a Grails plugin, we can define FooBootstrap.groovy or FooUrlMappings.groovy so that those can be bundled in the plugin because the earlier are excluded by default. http://docs.grails.org/latest/guide/plugins.html#_excluded_artefacts
How can we do the same for logback.groovy? I have a multi-project build where a single Grails plugin is used by 3 different Grails application.
My research & trials:
I tried creating a foo-logback.groovy but that is not loading.
Grails 3: External Logback.groovy file

Another resource: https://logback.qos.ch/manual/configuration.html
Basically, there can only be one logback.groovy or logback.xml in the classpath. In order to achieve what you are asking you could use your root project's logback.groovy and load a foo-logback.groovy using a GroovyShell instance (see https://stackoverflow.com/a/9006034/260487).
Alternatively you can have the plugin resources defined in its own package and include that package in your root logback.groovy file. For example, instead of com.example.FooService you would define it as com.example.plugin.foo.FooService, and you'd define a log rule for the com.example.plugin.foo package.

Related

Grails Multi-Project: Running a Plugin's custom script

Followed the tutorial on multi-projects
Everything mostly works. Plugin controllers & domain classes load properly in the application. However, a problem occurs when trying to run a Plugin's custom script from the application's grails CLI.
For example:
If you set up the multi-project directory structure like this:
Project Root
Application Directory
Plugin Directory
settings.gradle
And ran this command from the Plugin Directory
grails create-script hello
You'd be able to access the script when running grails from the Plugin Directory, but not the Application's Directory.
Is there a way to get this to work properly? Do I need to use an alternative set up?
Also see Creating a Custom Script in Grails
A conventional grails 3 plugin is different than a plugin within a multi-project. It doesn't seem to be designed to compile a plugin such as grails scaffolding with custom commands.
For this reason, you should package the plugin manually using:
grails package-plugin
grails install
Now in the build.gradle, add this line to dependencies:
compile "<plugin-group>:<plugin-name>:<plugin-version>
Subsituting the appropriate information within the brackets <>.
You can find the plugin-group in the plugin's build.gradle
group "org.grails.plugins"
plugin-name you specified in the grails create-plugin command
grails create-plugin plugin-name
plugin-version is also found in the plugin's build.gradle
version "0.1"

Application variables Grails 3.2

I am using Grails 3.2 and I am wondering where I should put my application variables for different enviroments.
application.yml or build.gradle?
And, how should I define them?
You should put them in the application.yml as stated in the documents:
Configuration in Grails is generally split across 2 areas: build
configuration and runtime configuration.
Build configuration is
generally done via Gradle and the build.gradle file. Runtime
configuration is by default specified in YAML in the
grails-app/conf/application.yml file.
You can also choose to use the Grails 2.0-style Groovy configuration as mentioned in the next alinea:
If you prefer to use Grails 2.0-style Groovy configuration then you
can create an additional grails-app/conf/application.groovy file to
specify configuration using Groovy's ConfigSlurper syntax.
Documentation: http://docs.grails.org/3.2.0/guide/conf.html#environments
Grails supports the concept of per environment configuration. The application.yml and application.groovy files in the grails-app/conf directory can use per-environment configuration using either YAML or the syntax provided by ConfigSlurper. As an example consider the following default application.yml definition provided by Grails:

How to copy Grails 2 plugin resources to the Grails app?

I'm writing a custom Grails 2 plugin to modularize my Grails applications. In the plugin I'm planning to define basic GSPs that can be overridden by the application that will utilize the plugin. I'm thinking of writing a Grails command script that copies those GSPs into the grails-app directory of the app the plugin is installed in. If in the plugin, I put those GSPs in grails-app/views, how do I refer to the actual Grails app directory in the Grails command script, which is also grails-app/views?
The solution to this is almost the same as this answer. The built-in properties basedir and <plugin_name>PluginBaseDir differentiate the target app and the plugin directories.

How to add a “non-mavenized” jar dependency to a grails project (Grails 3.x)

I'm trying to find documentation and code samples on how to add a local / non-maven jar file to my Grails 3.x project?
I found the separate thread How to add a non-maven jar to grails - but that's only to grails 2.3, and the file structure and configuration has undergone a big overhaul in 3.x.
Any help and (especially) code samples would be wonderful! The .jar is in the local project directory, and I intend to package with the .war for deployment.
Additionally, once i add the dependency, should i just be able to call it's methods from the controller & service files? or do i need to include them in those as well?
thx!
Grails 3 uses Gradle, so there's nothing Grails specific about including a local jar. It's as easy as adding a file dependency to the dependencies block of your build.gradle file.
Per the Gradle documentation on File Dependencies:
To add some files as a dependency for a configuration, you simply pass a file collection as a dependency:
dependencies {
...
compile files('libs/a.jar', 'libs/b.jar')
// or
compile fileTree(dir: 'libs', include: '*.jar')
}
The above example shows two ways to include jars that exist in a local libs/ directory; you can do either/or. The jar(s) can be anywhere on the filesystem, just make sure you point to the correct path.
To use the classes from the dependency in your application, you'll include them in your services, controllers and all other classes like you normally would. Say libs/a.jar has a class org.example.Something, you'd add an import to the top of your Grails class like so:
import org.example.Something

Convert grails app to plugin

I started a grails application by grails create-app. For modularity, I feel like it would be better that component be a plugin. Can I convert this application into a grails plugin?
thanks,
Babu.
I never created a plugin based on an application written before but looking at the documentation for grails plugins you can read the following statement:
The structure of a Grails plugin is exactly the same as a regular Grails project's directory structure, except that in the root of the plugin directory you will find a plugin Groovy file called the "plugin descriptor".
So I would suggest to create a new plugin with grails create-plugin *your-plugin-name* and copy all files from your application into the plugin.
In case anyone else is looking, this should be exactly what you need: http://burtbeckwith.com/blog/?p=1973
Excerpt:
So to convert an application to a plugin, the general workflow would
be something like
Create the plugin descriptor, FooGrailsPlugin.groovy. The easiest way to do this is to rungrails create-plugin pluginname and copy the
generated file from there
delete everything from application.properties except the app.grails.version property
if you have jars in the lib directory that are available in a Maven repo, delete them and replace with BuildConfig.groovy dependencies
change any plugin and jar dependencies that are needed for development and testing but not when the plugin is installed to not be
exported, by adding export = false
If you need the _Install.groovy, _Uninstall.groovy, or _Upgrade.groovy scripts (you probably don’t) grab those from the dummy plugin from step 1 (but delete any you don’t need, they’re all
optional)
delete ApplicationResources.groovy if you aren’t using it and don’t depend on resources plugin
move code from BootStrap.groovy init() toFooGrailsPlugin.doWithApplicationContext
and/orFooGrailsPlugin.doWithDynamicMethods and destroy() to
FooGrailsPlugin.onShutdown, and delete BootStrap.groovy
add a dependency for the release plugin in BuildConfig.groovy
delete everything but the log4j configuration from Config.groovy
delete UrlMappings.groovy unless you have exported mappings; only keep the added ones
move bean definitions from resources.groovy to FooGrailsPlugin.doWithSpring and delete resources.groovy
delete grails-app/i18n message bundle files unless you added messages; only keep the added ones
delete everything from grails-app/views that you don’t use (in particular error.gsp,index.gsp, and layouts/main.gsp)
delete everything from web-app that you don’t use (including WEB-INF xml and tld files)
now would be a great time to write those tests you’ve been meaning to get to
create one or more test applications to install the plugin into to ensure that it works as a plugin; consider scripting this
write documentation for how to use the plugin; at a minimum a README file, but Grails gdoc files would be much better (run grails doc
--init to get started)

Resources