Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include subproject classes in jar?

Tags:

java

gradle

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.

like image 303
K. D. Avatar asked Nov 11 '15 14:11

K. D.


2 Answers

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
}
like image 105
K. D. Avatar answered Oct 05 '22 10:10

K. D.


With this you can add all *.class files created in your subprojects into your jar automatically:

jar {
    from {
        subprojects.collect { it.sourceSets.main.output }
    }
}

To make sure the .class are there, make sure the root project depends on the subprojects:

dependencies {
    compile project(':mySubProject')
}
like image 34
Topera Avatar answered Oct 02 '22 10:10

Topera