Thursday, December 27, 2012

Getting Started with Gradle Multiproject

Project structure:
.
├── api
│   └── src
│       └── main
│           └── java
│               └── api
│                   └── API.java
├── build.gradle
├── cli
│   └── src
│       └── main
│           └── java
│               └── cli
│                   └── CLI.java
├── core
│   └── src
│       └── main
│           └── java
│               └── core
│                   └── Core.java
├── gui
│   └── src
│       └── main
│           └── java
│               └── gui
│                   └── GUI.java
└── settings.gradle
In this project, core depends on api, cli depends on core, and gui depends on core.

settings.gradle

include ":api", ":core", ":gui", ":cli"
build.gradle
def distDir = "dist"

defaultTasks = ["clean", "jar", "dist"]

subprojects {
    apply plugin: "java"
    apply plugin: "eclipse"
    sourceCompatibility = 1.7
    targetCompatibility = 1.7
    version = "0.1.0"
}

project(":core") {
    dependencies {
        compile project(":api")
    }
}

project(":gui") {
    dependencies {
        compile project(":core")
    }
}

project(":cli") {
    dependencies {
        compile project(":core")
    }
}

task clean << {
    delete distDir
}

task dist << {
    def d = mkdir(distDir)
    subprojects.each { subproject ->
        subproject.tasks.jar.each { archiveTask ->
            copy {
                from archiveTask.archivePath
                into d
            }
        }
    }
}
The build.gradle shown above has a dist task that will pull all the generated jars into dist directory.

1 comment:

  1. I am new to gradle build. I have multi projects which are dependent and I am looking for best practice to organize them in subversion. I have problem details here http://stackoverflow.com/questions/22090165/gradle-multiple-projects-structure-best-practice

    Thank you in advance.

    ReplyDelete