I am sorry if this belongs to “Questions”, for me it appears as a problem but Im pretty sure it is caused by me doing something wrong Im sorry that this will be longer text, I will try to be as brief as possible.
Here is the problem: I recently switched to Gradle because our project now has very specific build structure and Gradle seemed like excelent choice. In last three days I learned as much Groovy and Gradle as I can and solved most problems but Im stuck with last one - my custom jar tasks are producing jar files in which every *.class file is packaked twice (Im attaching screenshot of jar content).
Before I post the build script (as simplified as possible) I have to explain why it is done such way.
Our build structure and requirements:
-
one src directory with *.java sources
-
these sources represents one core and many components
-
what we need during build is to compile it alltogether but produce one jar for core and them component jars - one for every component
-
in jar we want to include sources also
-
there are many components and the number is raising -> build script should have component configuration at one place, the rest has to be done dynamically (upload stage is not important for this case)
I managed to create script that is working exactly as described but jar files contains every *.class twice, *.java files are ok (not duplicated). Im sure Im doing something wrong. Here is how my script works: - create own configuration ‘geCoreComponents’ - define big sourceSet containing all sources for compiling in one step and then one sourceSet for every component - dynamically create custom jar task for every component sourceSet. Jar task is configured with info from sourceSet’s config and is assigned to my ‘geCoreComponents’ configuration - assign my configuration to default jar task. I suppose here could be the problem. I suppose it would be better to create my own jar task with my configuration only as default contains also compile/runtime/test configuration. But I dont know how to do it
here is the script (im runinng it by ‘gradle compileGeCoreAllJava jar uploadGeCoreComponents’) apply plugin: ‘java’
project.version = “core” project.sourceCompatibility = 1.6 ext {
svnRevision = null
srcDir = “src”
outputDir = “bin”
svnAntLibDir = “lib”
compileCPLibsDir = “./…/GE_SharedLib_v7”
geCoreDistDir = “./…/GE_SharedLib_v7/ge_core”
}
sourceSets {
//main set which is used for compiling all source alltogether
geCoreAll {
java.srcDir file("$srcDir"); resources.srcDir file(“resources”); output.classesDir
= “$outputDir”; output.resourcesDir = “$outputDir”;
}
//component sourceset definitions. Used for creating component jar tasks which create jar files
core {
java.srcDirs = ["$srcDir/cz/trask/ge/digiflow/common", “$srcDir/cz/trask/ge/wps/”]
}
avizaDaily { java.srcDirs = ["$srcDir/cz/trask/ge/avizaDaily"] }
avizaWeekly { java.srcDirs = ["$srcDir/cz/trask/ge/avizaWeekly"] }
bankAtWork { java.srcDirs = ["$srcDir/cz/trask/ge/bankAtWork"] }
caprocessing { java.srcDirs = ["$srcDir/cz/trask/ge/caprocessing"] }
/* any more in original build script…*/ }
configurations {
geCoreComponents }
dependencies {
geCoreAllCompile fileTree(dir: compileCPLibsDir, include: ‘*.jar’) }
repositories {
flatDir {
name “geCoreLibsRepo”
dirs “$geCoreDistDir”
} }
compileGeCoreAllJava.doFirst {
tasks.withType(Compile) {
options.encoding = ‘UTF-8’
} }
/*
One jar task is created dynamically for every component sourceset and is attached to our ‘geCoreComponents’ configuration */ task createComponentJarTasks {
println “Creating component JAR tasks”
sourceSets.matching { it.name != “main” && it.name != “test” && it.name != “geCoreAll”}.each { set ->
String jarTaskName = “createJar_$set.name”
def jarTask = task("$jarTaskName", type: Jar) {
from sourceSets.geCoreAll.java.srcDirs //packaging sources
from sourceSets.geCoreAll.output //compiled classes
//every srcDir in defined in sourceSet is added to component jar task
set.java.srcDirs.each { dir ->
String componentClassesDir = (dir.path - projectDir.path).substring("$srcDir".length() + 2).replace(’\’, ‘/’) //get rid of ‘src/’
include("$componentClassesDir/**/*.class")
include("$componentClassesDir/**/*.java")
}
archiveName = “${set.name}.jar”
baseName = “$set.name”
manifest {
attributes(
“Implementation-Title”: “${set.name.toUpperCase()} GE_Core component”,
“built-by”: System.properties[‘user.name’])
}
}
artifacts {
geCoreComponents jarTask
}
} }
/* adding our configuration to jar task */ jar {
dependsOn configurations.geCoreComponents }
/* Custom upload task */ task uploadgeCoreComponents(type: Upload) {
setConfiguration(configurations.geCoreComponents)
uploadDescriptor = false
repositories {
flatDir {
name “geCoreLibsRepo”
dirs “$geCoreDistDir”
}
}
}
uploadgeCoreComponents.doFirst {
println “Uploading created JARs to $geCoreDistDir” }
String getRelativePath(String absolutePath) {
return (absolutePath - projectDir.path).substring(1).replace(’\’, ‘/’) }