Grails inline plugin notation for Java project dependencies? - grails

I've successfully used Grails's inline / inplace plugin notation to develop my Grails app and several plugins concurrently. I only have to compile my Grails app and all the inline plugins get compiled too (great!):
grails.plugin.location.myFooPlugin = '../plugins/foo-plugin'
Can I do the same thing for a Java dependency rather than a Grails plugin?
Let's say I have some Java project that ultimately produces a JAR, but rather than compile and store the JAR in my Maven local repo I'd like to simply compile my Grails app and have the Java project's code also compiled as a result. Possible? If so, what are the rules, such as dir structure adherence? I might want to use Gradle or Maven, not sure.

I'm using a java dependency in my Grails project with help of /scripts/_Events.groovy:
eventCompileStart = {
projectCompiler.srcDirectories << "${basedir}/../your_java_proj/src".toString()
}
The java project will be compiled automatically along with e.g. grails war commando.

Related

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.

Grails Project missing Java library at runtime

I was trying to add some Java Libraries (AWS SDK for Java, Apache Commons Math, etc.) to my Grails project since some of my Java source code (placed in src/java) had dependencies. By following this answer, I was able to resolve compile errors by adding the jar files to the /lib folder and add it to the build path, as answered here: Add Java Libraries to a Netbeans Grails Project
However, when I call my Java source code from my controller during runtime, it is unable to find the Java libraries that I added, showing a NoClassDefFoundError. Should I be adding something to the BuildConfig.groovy file? I'm not sure what the name convention for the jar files to be added to the dependencies.
The question you refer to is 5 years old. You should use newer resources :)
The preferred approach now is to use dependencies in BuildConfig.groovy, and let Grails (via Ivy or Maven) download the jars for you once and reuse them for various projects.
It's not always obvious what the syntax is, and I find that http://mvnrepository.com/ is a great resource. For example if you search for "commons math" and click through to http://mvnrepository.com/artifact/org.apache.commons/commons-math you'll see a few versions. Click on version 2.2 and you'll see the Maven dependency XML but you can click on the Gradle tab and it's going to be similar to what you need for Grails. So I'd add
dependencies {
compile 'org.apache.commons:commons-math:2.2'
}
and if necessary change compile to runtime, build, etc. depending on what you need the jar in the build process.
In the rare case that you do have a jar that isn't available in a Maven repo (e.g. a shared library at your company) then you can put the jar file in the lib directory. As you've seen, Grails doesn't auto-detect it (this is as of version 2.0). But you can run grails compile --refresh-dependencies to get your jar added to the classpath.
My issue turns out to be the fact that AWS Java SDK had dependencies (Apache HTTP Client) that were not installed yet and that I was unaware of.
This is what I had to configure this for my BuildConfig.groovy file
dependencies {
runtime 'org.apache.httpcomponents:httpclient:4.2.5'
runtime 'com.amazonaws:aws-java-sdk:1.4.7'
}
All the dependencies for AWS Java SDK 1.4.7 can be found here: http://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk/1.4.7. All the dependencies outside of HTTP client were already installed for me, but may not be for your Grails setup.

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 Plugin Maven Integration

I'm trying to create Mavenized Grails application. Everything works fine but as I understood all the dependencies (all .jars like mysql-connector and also all grails (public) plugins like spring-security-core plugin) should be listed in pom.xml.
The thing is that I don't know how to include public grails plugins (is there any Maven repository for that, or should I include used plugins into my local repo?). Or is the proper way how to handle grails plugin to list them in "application.properties" and let the grails to manage these plugins?
Thank you for any comment.:-)
Mateo
You can specify your plugin dependencies in grails-app/conf/BuildConfig.groovy, for example:
grails.project.dependency.resolution = {
plugins {
runtime ':hibernate:1.2.1'
}
}
Update
In response to your comments below, a plugin dependency specified in BuildConfig.groovy (or application.properties) will still be resolved by Grails rather than Maven. I don't think there's any way that you can get Maven to resolve a Grails plugin dependency, because Maven can only work with JAR dependencies in Maven repositories. Remember, Grails plugins are not (typically) available from Maven repositories.
If you want to hand as much control as possible over to Maven, you can try excluding the JARs from your plugin dependencies, e.g.
plugins {
runtime( "org.grails.plugins:hibernate:1.2.1" ) {
excludes "javassist"
}
}
and add them to your pom.xml instead. Here be dragons (see below).
Editorializing
FWIW, unless you really have to build your Grails project with Maven (e.g. because another Maven project depends on it), my advice would be don't. I say this because Maven is very much a second-class citizen in the world of Grails build tools. The usual way to build a Grails app is using the built-in GAnt commands. Future versions of Grails will move towards Gradle as the default build tool, so it seems that Maven will be an afterthought for the forseeable future
By default, Grails plugins are included at the source level. A plugin zip is expanded, and the plugin source is compiled as part of the grails build process.
Since 2.0, grails can use binary plugins. You can depend on plain old JARS if those jars represent binary grails plugins.
Binary grails plugins can be referenced by normal maven coordinates.
Your project's BuildConfig.groovy is where you specify maven repositories and binary plugins.

Does Grails + Maven + JCL work in conjunction?

I'm working with IntelliJ IDEA 10.0.1 and Grails 1.3.7. I have a mavenized Grails project which depends on many logging libraries.
Here's the problem:
I have to use JCL as logging framework, but grails per default is working with SLF4J and has some default dependencies like jcl-over-slf4j, which are inherited by every grails project. First of all I have excluded every jcl-over-slf4j transitive dependency in my project pom file and verified with mvn dependency:tree that my pom is clean of any SLF4J bridging libraries.
But nevertheless jcl-over-slf4j is still beeing downloaded to my local maven repo when I try to start my grails app. This leads obviously to a StackOverflowError at runtime, since both jcl-over-slf4j and slf4j-jcl are in the classpath.
So because of which declaration the jcl-over-slf4j dependency is still beeing downloaded?
Since my pom is clean the obvious conclusion would be that Grails itself depends on those libraries. As mentioned before Grails has some default dependencies, on which every Grails project depends.
I know that I can exclude inherited depencencies in the BuildConfig.groovy file and if I run grails dependency-report I can also see that these dependencies are not listed anymore.
grails.project.dependency.resolution = {
inherits("global") {
excludes "jcl-over-slf4j", "jul-to-slf4j", "slf4j-log4j12"
}
}
But even then the jcl-over-slf4j dependency is still beeing downloaded to my repo when I start my grails app! Am I missing something? Is there a different way to exclude inherited grails dependencies when you're using a mavenized grails project?
Any help would be appreciated
Thanks!
Slash
Ok I think I got the answer now..
The problem is that the defined maven-grails-plugin (which is mandatory when you use maven + grails) within my pom file depends on jcl-over-slf4j and therefore gets downloaded when I start my application through maven. With my current maven version (2.2.1) it's not possible to exclude a dependency from a plugin. There is also a jjira issue regarding this problem. Can not exclude a dependency from a plugin
As soon as I remove the maven-grails-plugin the dependency is not downloaded anymore, but as drawback I'm not able to start the application through maven anymore..
Lessons learned: Don't use Maven + Grails + JCL in conjunction.
Note that with mvn dependency:tree just project dependencies are listed, but plugin dependencies are NOT listed.
Hope this is of any help!
Regards Slash

Resources