I’m trying to create multiple jar’s in a task, but as of yet I can’t seem to get it to work.
The premise is that I have a bunch of individual JARs used as a thirdparty libraries, but a single source zip for all of the code (ie, the project produced a bunch of modules, but the source was one big dump). I’m trying to write a gradle script that will produce individual source jar’s for each jar I have.
def libraryJars = [ "moduleA.jar", "moduleB.jar", "moduleC.jar" ]
task extractSrc {
ext.SrcDir = file("build/srcs")
// Copies all of the *.java files out of the source zip into a folder, all collapsed and merged into package heirarchy. ie,
// build/srcs
//
'-- org
//
'-- company
//
+-- package
//
...
}
task createSrcPackages (dependsOn: extractSrc) {
ext.jarDirectory = "build/thirdparty" // contains a bunch of stuff, and at some area underneath, the jars we care about
doLast {
libraryJars.each { jarName ->
def jarFile = fileTree(jarDirectory).include('*/' + <some-extra-path-stuff> + jarName).singleFile
def jarSources = []
// Inspect the contents of the jar, finding all the .class files that we have source for.
// Cut it down to package-path level, then add it to the list
zipTree(jarFile).each { clazz ->
def ourClazz = clazz.absolutePath.replaceFirst(".*[\\/]com[\\/]mcafee", "com/mcafee")
if ( !ourClazz.equals(clazz.absolutePath) ) {
jarSources.add( extractSrc.SrcDir.absolutePath + "/" + ourClazz.replaceFirst("\.class", ".java") )
}
}
//jarSources now contains a list of strings to files in the sources directory
jar {
baseName jarFile.name.replaceFirst("\.jar", "-sources")
from files(jarSources)
destinationDir buildDir
}
}
}
}
Executing the task, it takes time and completes successfully, but no jars are produced at all.