Chris Prior
Chris Prior

Reputation: 449

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

I have the following setup within my project...

What I want to do is:

What are my options?

  1. Add a task into the grails build lifecycle within gradle to build any dependant jars and copy them into the grails lib folder?
  2. 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.
  3. 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.

Upvotes: 3

Views: 1490

Answers (3)

Chris Prior
Chris Prior

Reputation: 449

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...

Upvotes: 2

Chris Prior
Chris Prior

Reputation: 449

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

Upvotes: 0

MrJohnBBQ
MrJohnBBQ

Reputation: 313

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?

Upvotes: 0

Related Questions