Convert grails app to plugin - grails

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)

Related

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.

Grails 2.3.x lib directory with customized plugins

I'm not sure how to ask this question, but I'll try anyway. I found different people having some kind of similar problem. But none with the exact same issue.
I have a grails 2.3.x application, it uses a customized version of a plugin. I'm use to old versions of Grails (pre-Maven dependency resolution), when we were able to include the customized version of the plugin into the /lib folder. I'm doing the same thing, but I am not able to solve anything. :(
Is there a way to tell grails, that lib directory (or some other, into the project folder) have the plugin zip file on it as we were use to?
UPDATE
Just to clarify. I want the packaged plugin into the project directory, not the source code.
UPDATE 2
Try to add the packaged plugin into plugins folder inside project root without any luck. While BuildConfig.groovy have the dependency declared or not.
Not sure if I have understood your question correctly. It seems you want to use a customized version of a plugin in your grails-app. You can do so by creating a "custom-plugins" folder in the root of the application and placing the custom version of the plugin inside this folder. Then in the BuildConfig we can specify the plugin location at the beginning of the file like this:
Assuming I have
myGrailsApp/custom-plugins/custom-plugin-1
grails.plugin.location."custom-plugin-1"="custom-plugins/custom-plugin-1"
That is it.
If you package the plugin as a .zip file and put it in a plugins directory in the root of your application, it will be loaded from there. You might also need to remove the plugin dependency declaration from BuildConfig.groovy before this will work.

Grails can't find classes in jars

I have an app in Grails that uses a .java to manage paypal MassPay feature. Like many .java, it needs some jars that enclose the classes that jar uses. Ok, i import that jars and the errors in the .java dissapears. But now, when I try to run the app, i receive 25 messages like this:
myapproute/grails-app/controllers/com/mycompany/widget/MassPay.java:3: package com.paypal.sdk.profiles does not exist
import com.paypal.sdk.profiles.APIProfile;
That file in the MassPay.java does not throw any error, since i imported the jar where that class is enclosed. But it doesn't allow me to run the project.
Any help? thanks.
Im using Eclipse, not NetBeans (i have read that there is a bug in Netbeans)
Adding JARs to the Eclipse project build path is not sufficient to make them visible to Grails. You need to either put them in the application's lib directory and run grails compile --refresh-dependencies or (better) if the JARs are available in a Maven-compatible repository simply declare your dependencies in BuildConfig.groovy and let Grails download the JARs itself.
Run this - it will work
grails clean

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

Grails + Tomcat6 + Multiple Instances + Shared Lib Folder

I've got a Tomcat6 server that runs multiple Instances for two separate grail apps.
When I compile my WAR file for deployment normally
run-app -Dgrails.env=production war test.war
It deploys correctly and everything works as it is suppose too.
The problem is, I don't want the JAR files included in my WAR.
So I use the following command line instead
run-app -Dgrails.env=production war test.war --nojars
Now when my grails app deploys (it doesn't) I get a java.lang.NoSuchMethodError
I have copied the lib folder (from my initial test.war) to the following locations
${catalina.base}/shared/lib
${catalina.home}/shared/lib
${catalina.home}/lib
None of these work.
My catalina.properties all point to the correct locations.
Any ideas?
A few ideas:
BuildConfig.groovy has inherits global, which has the app inherit all of the grails/plugins dependencies. If you change this, it may affect both your build and packaging - plus I have yet to encounter any documentation on what type of other things you can do with the inherits DSL
Grails deployment documentation suggests there is a way to customize which dependencies make it into the war file: http://grails.org/doc/latest/guide/17.%20Deployment.html
Event hooks give you access to provide a closure routine into various stages of the grails lifecycle. Can it strip out framework jars from the final war? Haven't tried that either - only using it to re-write various config files for additional envrionment configuration. However it does look like packaging events are exposed to this API:
http://grails.org/doc/latest/guide/4.%20The%20Command%20Line.html

Resources