Can Gradle jar multiple projects into one jar ?
I know you can do it for a single project using a method like this:
task packageTests(type: Jar) {
I know that this has been answered, but since the answer is old (current Gradle version is few major versions newer - 6.2.2) and this thread shows up in search engines quite high I'll post solution that worked for me.
Generally my problem was similar to/same as the one stated in original post: I wanted to get classes from all subprojects and zip them into one jar (preferably using the most default settings as possible). Finally I've found that the following code does the trick:
jar {
from subprojects.sourceSets.main.output
}
But while it builds jar properly, I also wanted to be able to publish this jar to Maven repository as single artifact which depends on everything that all subprojects do. To achieve this the main project has to dependent on subprojects dependencies which is done by:
subprojects.each { evaluationDependsOn(it.path) }
dependencies {
api(externalSubprojectsDependencies('api'))
implementation(externalSubprojectsDependencies('implementation'))
}
private externalSubprojectsDependencies(String configuration) {
subprojects.collect { it.configurations[configuration].allDependencies }.flatten()
.findAll { !it.properties['dependencyProject'] } // this looks like hack, I'd love to see better solution
}
Only external dependencies are copied - we do not want internal (project) dependencies listed in pom.xml
as all their classes are in jar anyway.