Reputation: 512
I have two approaches to create a fat jar with gradle:
By using an ant task:
jar {
doLast {
ant.jar(destFile: jar.archivePath, update: 'true') {
configurations.compile.files.each { file ->
zipfileset(src: file) {
exclude(name: '**/META-INF/maven')
exclude(name: '**/META-INF/maven/**/*')
exclude(name: '**/templates')
exclude(name: '**/about_files')
exclude(name: '**/about_files/**/*')
exclude(name: '**/*html')
exclude(name: '**/*readme*')
exclude(name: '**/*txt')
exclude(name: '**/*inf')
exclude(name: '**/*SF')
exclude(name: '**/*RSA')
exclude(name: '**/*.vm')
exclude(name: '**/empty.file')
}
}
}
}
}
By using the gradle way:
jar {
dependsOn configurations.compile
from (configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }) {
exclude '**/META-INF/maven'
exclude '**/META-INF/maven/**/*'
exclude '**/templates'
exclude '**/about_files'
exclude '**/about_files/**/*'
exclude '**/*html'
exclude '**/*readme*'
exclude '**/*txt'
exclude '**/*inf'
exclude '**/*SF'
exclude '**/*RSA'
exclude '**/*.vm'
exclude '**/empty.file'
}
}
Both tasks create an identical jar file. But the first approach takes 20sec, the second 3min.
Why is the first one faster? Apart from execution time: Which one is the "cleaner" solution? Or is there a smarter way to do the job?
Upvotes: 2
Views: 1001
Reputation: 123960
The former task is missing a dependsOn configurations.compile
, which may explain why executing this task is faster. Another major difference is that only for the second task, task inputs are declared properly (i.e. Gradle can reliably determine if the task is up-to-date
), which can take a while if the files in configurations.compile
are large.
PS: The second task should use from { configurations.compile.collect { ... } }
to avoid doing work in the configuration phase. This will speed up build invocations that don't involve that task.
Upvotes: 1