Custom Gradle plugin

(smurfs.doll) #1


I am working on a multi module grails plugin project. I am planning to use gradle to build the project.

Since, gradle needs a build.gradle under each and every project to specify the dependencies and repositories, is it possible to create a custom gradle plugin project that copies the dependencies and repositories section from buildconfig.groovy into build.gradle at build time.

Buid time, in the sense, if i am doing gradle release in jenkins, it will checkout the project from svn and custom gradle plugin should be executed to generate the build.gradle under each and every project and then the normal flow should happen.

Is that feasible in gradle? In maven, we can inject similar plugins in the build phase or install phase.

Can you please let me know, if it can be done through gradle plugin or is ther any other way of doing things.

Thanks Smurf

(Eric Berry) #2

Do you want it to actually generate a build.gradle file for the project? or do you just want to pull out common dependencies so that you don’t have to write them over and over again in each project - similar to how Maven works with parent poms?

If it’s the later, you can do this pretty easily by using an apply script and then applying it in each of your project’s build.gradle file.

I did this at my previous company where all our projects were using basic modules of the Spring framework. I used them also to set up the maven repositories and some default tasks (like javadoc, unversioned war files for deployment, etc…)

Apply script hosted in your maven repo, or on an internal intranet somewhere:

project.ext.springVersion = '3.2.3.RELEASE'
repositories {
   maven { url "http://internal.maven.repo/path"}
dependencies {
   compile "org.springframework:spring-core:${springVersion}"
javadoc {
   // ... etc. ...

Then you apply it in your build.gradle file:

apply from: 'http://internal.maven.repo/install/apply.groovy'

Note that you can also apply scripts to subprojects - you still need the build.gradle file in each sub-module, but you can reduce duplicate code. See here for more information on that:

(smurfs.doll) #3


Consider my grails multi module project as below:

Main ----A ----B Settings.gradle build.gradle

Main is the main project which has two grails project A and B. As per grails-gradle plugin, we should not put the dependencies in buildconfig.groovy and we should place them in build.gradle. In order to make the development simple in grails and not to mix up with gradle, I thought modules (A) and (B) can be developed in grails and we can generate the build.gradle under module (A) and module(B)

from the main project [main]. For this to happen, i have to pick the dependencies and repository section from buildConfig.groovy and place it in the generated build.gradle

Can you please let me know, how to do it

Thanks Smurf

(Eric Berry) #4

I’m not 100% familiar with the gradle grails plugin, but I’m assuming you’re using this one:

If so, I don’t see any where saying that you shouldn’t use the buildconfig.groovy file.

However, seems to me that all you really need is to set up your main project to setup project’s A and B. Which you can do from reading the link I posted earlier.

You would have something like this in your main/build.gradle file:

subprojects {
 buildscript {
   repositories {
     mavenRepo urls: ""
      dependencies {
     classpath "org.grails:grails-gradle-plugin:1.0",
    apply plugin: "grails"
    repositories {
   mavenRepo urls: ""
    dependencies {
   compile "org.grails:grails-crud:1.3.4",

(smurfs.doll) #5


As per , the dependencies section in the buildconfig.groovy is switched off. can you please confirm.

If that is the case, then i have to pick the plugins and dependencies from buildconfig.groovy and place that in build.gradle. I am not sure how to read/ get the dependencies and plugins from buildconfig.groovy to build.gradle. Can you please help me on that.

Thanks Smurf

(Eric Berry) #6

Based on that presentation, yes the buildconfig.groovy dependencies are switched off. I think the intention there is to actually move away from grails doing that management altogether. I think the recommendation then is to actually take out those plugin dependencies from the buildconfig and put them as normal dependencies in your build.gradle file. Indeed he removes the whole dependency resolution section from the buildconfig altogether.

I imagine it might be rather difficult to actually read the buildconfig file and pull those dependencies out automagically using Gradle. True, that file is using a ConfigSlurper, but I believe there are some special methods that you’d need to somehow mock out.

My suggestion would be to do what he does with the tomcat plugin and just take those plugin dependencies you have in your buildconfig file, and put them manually in your build.gradle file as a ‘org.grails:grails-plugin-[whatever]’ dependency.

(smurfs.doll) #7


I tried using configSluper for buildconfig.groovy, i was able to access other properties defined there. but for dependencies it shows the value 'dependency:[resolution:script13733932805231614944274$_run_closure1@71dc48dd]]

Can you please let me know, how to iterate that :frowning:

Thanks Smurf

(smurfs.doll) #8

I do have so many grails plugins, so i think the manual process will be time consuming. Using configslurper i was able to get the other properties in buildconfig.groovy. but, i am not able to do for the dependency section because it returns [resolution:script13733932805231614944274$runclosure1@71dc48dd]] …

Can you help on this.

(smurfs.doll) #9

Hi Can you please let me know, on how to invoke and apply the groovy files to all the subprojects from the main build.gradle?


(Peter Niederwieser) #10

gradle needs a build.gradle under each and every project to specify the dependencies and repositories

Gradle doesn’t need this. If you want, you can configure all projects from a single build script. Actually, you can even omit that, and configure everything from the (mandatory) settings script.

(smurfs.doll) #11

Thanks for the info. I am using some classes from grails in the gradle custom plugin. So, i have added below lines in the build.gradle of the custom plugin

compile "org.grails:grails-bootstrap:2.2.2"

I was able to compile, build, and upload the jar to the nexus repo.

When i try using that plugin in another gradle project , am getting

vCaused by: java.lang.NoClassDefFoundError: grails.util.BuildSettingsHolder

If i remove the grails code, the custom plugin worked fine.

Can you please help me on how to integrate grails with gradle.

(Peter Niederwieser) #12

I’m afraid I don’t know enough about Grails and the Grails Gradle plugin to help.