Skip to content

How to include subproject classes in jar?

After hours of researching and trying I hope that someone can explain how we can have Gradle including classes of one or more subproject into a specific jar.

Lets say we have just three modules

  1. Core
  2. App
  3. Web

Obviously the App and Web project artifacts (jars) should contain all classes from the Core project.

Settings gradle:

rootProject.name = 'MyProject'
include 'MyProjectCore'
include 'MyProjectApp'
include 'MyProjectWeb'

MyProjectApp / MyProjectWeb:

...
dependencies {
    ...
    compile project(':MyProjectCore')
}

The projects can be compiled but the output jars do not contain the core classes.

Answer

Okay finally I found a working solution even if it might not be the best.

For the App and Web projects I overwrite the jar task like this to add the source sets of the Core module.

jar {
    from project.sourceSets.main.allSource
    from project(":MyProjectCore").sourceSets.main.java.srcDirs
}