Skip to content

Instantly share code, notes, and snippets.

@musketyr
Created April 12, 2012 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save musketyr/2367907 to your computer and use it in GitHub Desktop.
Save musketyr/2367907 to your computer and use it in GitHub Desktop.
Fat JAR Task
import org.gradle.api.file.FileCollection
apply plugin: 'java'
apply plugin: 'war'
dependencies {
compile fileTree(dir: 'lib', includes: [
'a.jar',
'b.jar'
])
}
def stageDir = buildDir.path + '/tmp/fatjar-stage/'
task fatjarStageClasses(type: Copy, dependsOn: classes){
from sourceSets.main.output.classesDir
into stageDir
}
task fatjarStageResources(type: Copy, dependsOn: classes){
from sourceSets.main.output.resourcesDir
into stageDir
}
task fatjarStageDependencies(type: Copy){
from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
into stageDir
}
private boolean isServiceFile(File file){
file.parent?.endsWith('META-INF/services') || file.parent?.endsWith('META-INF/services/')
}
private FileCollection collectFatJarFiles(){
def fileCollections = fileTree sourceSets.main.output.classesDir
fileCollections += fileTree sourceSets.main.output.resourcesDir
configurations.compile.each {
fileCollections = fileCollections + (it.isDirectory() ? it : zipTree(it))
}
fileCollections
}
task fatjarCollectServices(dependsOn: [fatjarStageClasses, fatjarStageResources, fatjarStageDependencies]) << {
def serviceFiles = collectFatJarFiles().filter{ isServiceFile(it) }
File serviceDir = new File(stageDir + 'META-INF/services/')
serviceDir.deleteDir()
serviceDir.mkdirs()
for(File file in serviceFiles.files){
def serviceFile = new File(serviceDir, file.name)
if(!serviceFile.exists()){
serviceFile.createNewFile()
}
serviceFile.append file.text.trim() + '\n'
}
}
task fatJar(type: Jar, dependsOn: fatjarCollectServices) {
classifier = 'fat'
manifest { attributes 'Main-Class': 'Default' }
from stageDir
}
task slimWar(type: War, dependsOn: fatJar) {
classifier = 'slim'
classpath = files fatJar.archivePath
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment