Gradle grails - how to exclude dependency based on environment? - grails

I have a project where I want to include the console plugin for certain environments but not for others - it should go in the (custom) deployTest environment, but not for Production.
If I build the app using grails, I have a BuildConfig.groovy that looks like:
grails.project.dependency.resolution = {
/// some stuff
plugins {
/// more dependencies
if (Environment.current != Environment.PRODUCTION){
compile ":console:1.5.1"
}
}
}
Dependency resolution for Grails when using Gradle to build it is based on the build.gradle file, not the BuildConfig.groovy.
How do I achieve this please?

It seems more to me like you want to INCLUDE a dependency based upon the environment. However, the approach will be the same either way. In your build.gradle file you have a 'dependencies' section. Since this is Groovy, it is code. The -PgrailsEnv value is used by gradle to specify the environment. Just use that:
dependencies {
// if no property, then gradle will be default to prod. So test
// for that or having it explicitly provided
if (!project.hasProperty('grailsEnv') || project.grailsEnv.equals('prod')) {
compile ':console:1.5.1'
}
}

Related

Why are there two ways to configure plugins for grails?

A grails application I work with has two ways to include plugins:
first in the application.properties file:
plugins.cache-headers=1.0.4
plugins.cached-resources=1.1
plugins.database-migration=1.1
plugins.export=1.5
plugins.font-awesome-resources=3.2.1.2
and in the BuildConfig.groovy file:
runtime ":resources:1.1.6"
compile ":database-migration:1.3.6"
compile ":quartz:0.4.2"
compile ":export:1.5"
compile ":font-awesome-resources:3.2.1.2"
It seems confusing that the database migration plugin is version 1.1 in application resources and 1.3.6 in BuildConfig.
Why are there two ways to configure plugins for grails?
Yes there are two ways of installing plugins.
The old way of declaring dependencies, using the command install-plugin. This will work with application.properties.
In Grails 2.x the preferred way is to use BuildConfig.groovy since this is more flexible, you can exclude jars/dependencies, define the scope and config the dependency to not be exported.
plugins {
test() //test scoped plugin
compile("group:name:version") {
excludes "some-dependency" //install the plugin, but not his dependency
}
compile("...") {
export = false //use this dependency, but not export.
}
}
With install-plugin, all your dependencies will be compile scoped.
More about in this discussion.

Grails 2.2.X Plugin Development - Plugin Dependencies

I'm totally confused how and where to specify my own plugin dependencies in Grails 2.2.X The documentation (Understanding Plugin Load Order) says that you can specify the dependencies in plugin descriptor class MyGrailsPlugin.groovy. Whereas, the "Upgrading from" chapter says that only pom dependencies will be taken into account. As I understand this unclear statement, only if I would specify the dependency in BuildConfig as a compile dependency that it would be used.
Using dependsOn brought me some problems in my main application (could not resolve a dependency in plugin even if it exists - I think some wild card problem "def dependsOn =['jquery-ui': "* > 1.8.24"]").
The only way how the plugin dependency works for me is specifying it in BuildConfig (MyPlugin):
grails.project.work.dir = 'target'
grails.project.dependency.resolution = {
inherits 'global'
log 'warn'
repositories {
grailsCentral()
mavenLocal()
mavenCentral()
}
plugins {
build(':release:2.2.1', ':rest-client-builder:1.0.3') {
export = false
}
compile ":resources:1.1.6"
compile ":jquery:1.8.3"
compile ":jquery-ui:1.8.24"
}
}
But my application uses resources plugin of version 1.2. When I run the app it always asks me if I'd like to upgrade to 1.1.6.
So the question is, how and where should I specify my dependencies.
Thanks,
Mateo
Actually, I am using grails 2.1.0. In that i replace resource with 1.2( runtime ":resources:1.2") in BuildConfig.groovy.
And then refresh dependencies. It is worked fine.
After reading more about Grails plug-in I realized that this behavior makes sense. If the plugin specify certain version of its dependency and your project specifies a different one, you're in conflict. You need to use following in order to exclude dependecy from the plugin and use yours:
runtime ":resources:1.2"
compile ':my-plugin:2.0.8', {
exclude 'resources'
}
In this case the plugin creator cannot assure that his plugin will run properly with newer version of dependency.
Regarding the resources plugin dependency. In my opinion it is better to use following
compile ":resources:1.1.6" {
export = false
}
which won't include the dependency for your plugin. This should be used just when you defines some ApplicationResources.groovy. If you use something from this plugin in your plugin you should not exclude resource plugin...
In my opinion you should specify your plugin dependencies in BuildConfig.groovy
Hope these things will be improved in further Grails versions.
Further reading from Burt:
http://www.slideshare.net/burtbeckwith/plugins-21828912

How to use grails.plugin.location?

I have a plugin project which I created as grails create-plugin myPlugin. I also created a 'normal' grails project as grails create-app myPluginDemo. I'm trying to install myPlugin plugin in myPluginDemo but don't understand how to use grails.plugin.location.
Where do I put grails.plugin.location inside BuildConfig.groovy? Inside plugins section? Inside repositories section?
What should I append to grails.plugin.location? Should it be grails.plugin.location.myPlugin? Or grails.plugin.location.grails-my-plugin? Something else?
grails.plugin.location is not a dependency resolution, so it goes outside grails.project.dependency.resolution.
It should be like below, if both myPluginDemo and myPlugin are in the same directory. Moreover, this will not install the plugin into the app, but the application will refer to the file system for the plugin which is convenient in development mode. In order to use the packaged plugin it has to be referred in plugins inside grails.project.dependency.resolution
grails.plugin.location.myPlugin = "../myPlugin"
grails.project.dependency.resolution = {
repositories {
}
dependencies {
}
plugins {
}
}

How can I include a dependant jar in a multi project grails / gradle build?

I have the following setup within my project...
A multi project gradle build for a SOA style project, wired together using rabbitmq and spring integration.
Contains a number of grails projects as well as plain java / groovy projects to represent the services.
Alongside each of the service projects are a project (jar) containing all of the public interfaces (and messages) for the service that can be proxied using spring integration.
The projects are related to each other using gradle dependencies and then I generate IntelliJ projects files using the gradle idea plugin.
What I want to do is:
Include the interfaces jar in the grails project so that I can use spring integration there to proxy my calls into the services via rabbitmq.
When I run the grails app have this intefaces jar built and included within grails.
When I run the grails app from IntelliJ have it compile the latest version of the interfaces and include them in the grails project.
When I build the entire project from gradle, have gradle correctly associate the interfaces jar with the grails app.
Ideally I would love to be able to do this just using dependency declaration within gradle, but this is probably a pipe dream...
What are my options?
Add a task into the grails build lifecycle within gradle to build any dependant jars and copy them into the grails lib folder?
Hook into the grails build lifecycle by using Events.groovy or similar to call out to grails to build and package the dependant jars. This would cover both the IntelliJ and command line routes.
Build the interfaces as a grails plugin? I had discounted this as they also need to be used from non-grails projects.
Any help / advice would be appreciated.
Turns out all I needed to do was add the following and then the grails plugin deals with the dependencies for me...
compile project(':dependent-project')
Works nicely for run-app and war...
A partial solution to the problem would be to use both Gradle and Grails maven plug-ins. I have a similar situation where I am building jars that are dependencies of the Grails project.
The approach I've chosen is to install the java artifacts into the local .m2/repo and then declare the dependency under grails/conf/BuildConfig.groovy using the mavenLocal() repo.
What I hadn't considered was to hook gradle into the events lifecycle (interesting idea, btw) and instead defined a gradle project that wraps the grails app (executes test-app, run-app, etc). The gradle wrapper for my grails app has a dependency on the other component's install task so it always checks to see if it needs to be rebuilt.
I'm an Eclipse user so I can't comment on the Intellij part of your question but the above works for me so I hope it gives you some ideas?
My solution for the moment is to:
Use the grails / gradle plugin to build the grails projects.
Use this plugin to run my grails apps, ie. gradle grails-run-app.
Hook into the grails-run-app task in gradle (which is created on the fly) to call a task which builds and copies the dependencies into the lib directory.
This doesn't help a whole load with IntelliJ at the moment but I will run my gradle tasks as IntelliJ run configurations.
My build.gradle is as follows (dependent-project is being jarred and put in lib in this example):
import org.grails.gradle.plugin.GrailsTask
evaluationDependsOn(':dependent-project')
buildscript {
repositories {
mavenCentral()
mavenRepo name: "grails", url: 'http://repo.grails.org/grails/repo'
}
dependencies {
classpath "org.grails:grails-gradle-plugin:1.1.1-SNAPSHOT"
}
}
repositories {
mavenCentral()
mavenRepo name: "grails", url: 'http://repo.grails.org/grails/repo'
}
ext {
version = "1.0"
grailsVersion = "2.2.0.RC2"
grailsTaskPrefix = "grails-"
}
apply plugin: "grails"
dependencies {
['dependencies', 'resources', 'core', 'test', 'hibernate', 'plugin-datasource', 'plugin-domain-class', 'plugin-tomcat', 'plugin-services'].each { plugin ->
compile "org.grails:grails-$plugin:2.2.0.RC2"
}
bootstrap "org.codehaus.groovy:groovy-all:2.0.5"
}
// Get hold of the grails-run-app task (which is created on the fly) and add a dependency to the copyDependencies task
project.gradle.afterProject { p, ex ->
if (p == project) {
project.tasks.addRule("Grails dependencies") { String name ->
if (name.startsWith(grailsTaskPrefix)) {
tasks.getByName(name).dependsOn(copyDependencies)
}
}
}
}
// Build and copy any dependent jar files...
task copyDependencies(type: Sync) {
from project(':dependent-project').configurations.archives.allArtifacts.files
into "$projectDir/lib"
}

Best Way To Tie Together Gradle Module and Grails Module

We have a Project that contains 2 modules; web and client
The web module is a Grails application and the client module is mostly just groovy code and touch of Java code. The client module is being managed by a Gradle build script.
The Grails module has dependencies in the client module. In IntelliJ I can define that in the module config with no problems. However, when building the Grails WAR I really need to build the client JAR and have it placed in the WAR file. I'm wondering what is the best approach for this?
Here is what I ended up doing.
In my Gradle script I have the following:
repositories {
mavenCentral()
flatDir {
name "genRocketRepos"
dirs "${System.properties['user.home']}/.genRocket/repos"
}
}
uploadArchives {
repositories {
add project.repositories.genRocketRepos
}
}
I then run the uploadArchives task via Gradle. This published the JAR file to a local repos. In Grails BuildConfig.groovy I defined the following:
repositories {
...
flatDir name:'myRepo', dirs:'${userHome}/.genRocket/repo'
}
dependencies {
...
runtime 'genRocket:client:1.0'
}
Now Grails sees the dependency and includes in the classpath as well as places the JAR in the WAR for me.

Resources