Best Way To Tie Together Gradle Module and Grails Module - grails

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.

Related

Missing grails 3.3 plugin jar gson view class files

Grails 3.3. I render gson views in a plugin. During plugin stand-alone run-app the views render fine as expected. After including the plugin in a main app the views cannot be resolved. I notice that the published plugin jar has no compiled gson class files in it, only the source gson files. Shouldn't there be gson class files in the jar? There is no gradle "compileGsonViews" task in the plugin app. Also, in Intellij the "build Artifacts" action is disabled. Shouldn't there also be a jar file artifact? Am I missing something in my build dependencies? The only "views" line in build.gradle is
compile "org.grails.plugins:views-json:1.2.6"
Indeed I was missing some things in build.gradle. I was missing:
buildscript {
dependencies {
classpath "org.grails.plugins:views-gradle:1.2.6"
}
}
apply plugin: "org.grails.plugins.views-json"
I should add that this does cause the gson class files to be included in the jar file. However, it has no effect on the larger issue of the inability to resolve the plugin view classes when running the main application.

Gradle grails - how to exclude dependency based on environment?

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'
}
}

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"
}

grails plugin DSL specify zip file

When using the grails plugin dsl in BuildConfig.groovy, such as
plugins {
build "org.grails.plugins:db-util:0.4"
}
Is there a way to specify to use a plugin from a zip file like you can do with grails install-plugin?
Not directly, but of you put the zip file in a directory and name it without the grails- prefix, then declare that directory as a flatDir repository then Grails will be able to resolve the plugin from there.
repositories {
flatDir name:'localPlugins', dirs:'../local-plugins'
}
// copy plugin zip to ../local-plugins/my-plugin-1.2.zip
plugins {
compile ':my-plugin:1.2'
}
Or if it's a locally built plugin you could install it into your local maven cache using grails maven-install and just use mavenLocal() instead of the flatDir.

Resources