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

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.

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"

Grails 3 Application or Plugin

If I'm looking at project source code (in Intellij) how can I tell whether the Grails source code I'm looking at is an Application or a Plugin?
I get it that the output of a Grails Application build is a WAR, and a JAR for a Plugin but I can't figure out how to tell the difference by looking at the source code.
Bonus question: If it is a multi-module project, how do I tell which module is the Application and which modules are the Plugins? Or am I missing some important concept here?
Plugins can be run as applications as well and will often have an Application.groovy file. Plugins will have a <pluginname>GrailsPlugin.groovy file which sets up the plugin. In grails 3, this is in /src/main/groovy file structure.

What are inline plugins?

What is an inline plugin in Grails 2.X? How to make a plugin inline? I can find the documentation for Grails 3 but not for Grails 2.
Inline plugins in Grails 2.x are outlined in the documentation section for plugins.
From the documentation:
An application can load plugins from anywhere on the file system, even
if they have not been installed. Specify the location of the
(unpacked) plugin in the application's
grails-app/conf/BuildConfig.groovy file
Creating an inline plugin is done using the grails create-plugin command, just like a non-inline plugin.
The only real difference between an inline-plugin and regular plugin is how it is referenced by your application. A normal plugin is pulled from a repository (such as maven) while an inline-plugin exists in source format local to the application that is using it. Take for example:
/usr/foo/grails/MyApplication
/usr/foo/grails/MyInlinePlugin
/usr/foo/grails/MyOtherInlinePlugin
The above application (MyApplication) can include the two plugins listed as inline plugins by using the following in the BuildConfig.groovy
// BuildConfig.groovy
grails.plugin.location.'my-inline-plugin' = "../MyInlinePlugin"
grails.plugin.location.'my-other-inline-plugin' = "../MyOtherInlinePlugin"
Overall inline plugins are useful when developing (or testing) a plugin as well as creating modular Grails applications.
You can find the documentation in this URL: Grails Documentation
Go to the section: User guide for older versions
And select your version of Grails.
Here you can find, for example, the documentation of Grails 2.5.0: Grails Documentation 2.5.0
The inline plugins can help you to debug an application or change the code of your plugins to do your tests, instead of apply the changes in the plugin and when it's released, test if it's OK. It's very useful to change different plugins in the same time.
Hope this helps!

How do I create a Grails skeleton project for plugin development?

I am working with a (sort of) framework built on top of Grails. This framework is a set of Grails plugins that add functionality to the Grails app (e.g. user authentication). This framework is a bit of a pain to setup, as it requires around 64 lines of site specific configuration in the apps's Config.groovy file.
I want to develop my addons to this app as plugins. That is, the Grails app would really just be a set of installed plugins and some configuration files.
I have created a local Maven style repository to hold all of my plugins. Thus, I can add plugin dependencies to the BuildConfig.groovy file and they will be installed automatically (side question: is it possible to specify the install order?).
So my question is, how do I create skeleton project for developing my plugins that would:
Include the base configuration for my application (the aforementioned 64 lines)
Allow me to do a grails package-plugin to package only the plugin's code
You can use the post-installation hooks mechanism: http://grails.org/doc/latest/guide/plugins.html#hookingIntoBuildEvents
Not really an ideal setup for me, but the following works:
Create the "base" application: cd ~/GrailsDev/ && grails create-app my-app
Configure my-app as desired/required
Create your dependent plugin: cd ~/GrailsDev/ && grails create-plugin my-app-plugin
Add the new plugin to the app by editing "~/GrailsDev/my-app/grails-app/conf/BuildConfig.groovy" and appending the line: grails.plugin.location.'my-app-plugin' = "../my-app-plugin"
You can now run the my-app Grails application and the plugin will be included. When your plugin is fully developed, you can do grails package-plugin from within the "~/GrailsDev/my-app-plugin" directory to package your plugin.
use gradle. you can specify the order and package your plugin alone.
e.g. include the required plugins as git modules (for easy versioning) and gradle modules (for building your plugin) in your plugin project.
this setup will serve your requirements well I suppose.
https://github.com/grails/grails-gradle-plugin
IntelliJ does have a template for gradle-backed grails applications and plugins.

How to run a local plugin in Grails 2.0?

In Grails, there is a variant how to include local plugin from sources. According to docs, one may type in BuildConfig.groovy:
// Useful to test plugins you are developing.
grails.plugin.location.shiro =
"/home/dilbert/dev/plugins/grails-shiro"
// Useful for modular applications where all plugins and
// applications are in the same directory.
grails.plugin.location.'grails-ui' = "../grails-grails-ui"
The problem is that it doesn't work in Grails 2.0.RC1. I've tried to do grails clean, to install plugin with grails install-plugin and to place it to BuildConfig.groovy. Still unable to resolve.
This works for me
grails.plugin.location.shiro = "/home/dilbert/dev/plugins/grails-shiro"
Where shiro is the name of the plugin (not the name of the directory it's in). Make sure the path to the plugin is either an absolute path or the relative path to the plugin from the application.
I've found that this sometimes doesn't work if the plugin is listed in application.properties or BuildConfig.groovy, so if it is, remove it, then execute grails clean and restart the app.
You can also install the plugin into your local maven cache.
The documentation speaks about this:
3.7.10 Deploying to a Maven Repository
maven-install
The maven-install command will install the Grails project or plugin artifact into your local Maven cache:
grails maven-install
This has the advantage of allowing you to include the plugin in your parent application using the more common ":plugin-name:version" syntax
Which allows your application to determine the best place to retrieve the plugin when in production. From an internal maven-repo or equivalent.
With Grails 3.x there is another way to do this. Suppose you've a grails app and plugin (source code) inside the same project directory:
/my-project
---/my-app
---/grails-shiro
To run your local plugin, you must create a settings.gradle file in the my-projectdirectory specifying the location of your application and plugin:
include 'my-app', 'grails-shiro'
Then add the dependency in your application's build.gradle:
compile project(':grails-shiro')
You've done.
Look at the plugins documentation for more information.
Surround the plugin name with quotes in case it contains dashes:
grails.plugin.location.'plugin-name-with-dashes' = "<path>"
You can add the .zip file for the plugin in your /lib and it will be installed.
Example:
compile ":myPlugin:1.0"
File:
/lib/myPlugin-1.0.zip
Note: You have to zip the content of the plugin folder.
Source: http://grails.1312388.n4.nabble.com/Insert-own-local-plugin-into-build-config-td4646704.html

Resources