Global properties set on sourceSets


(Furdui Marian) #1

Hi, I need a little help with a Gradle build script. The build should set various properties depending on sourceSets. Below is a short version of my build.gradle:

apply plugin: com.mindmatics.build.gradle.web.WebPlugin
apply plugin: 'java'
  apply from: 'dependencies.gradle'
group = 'com.foo.bar'
  jar.enabled = true
  configurations {
    server
    job
}
  sourceSets {
    server {
        project.ext.jarName = "server"
    }
    job {
        project.ext.jarName = "job"
    }
}
  jar {
    println("jarName: " + jarName)
    baseName = jarName
}
  artifacts {
    server jar
}

When I call “gradle jar server” I want the jarName to be "server, it is always “job” because the properties are evaluated in the order thay appear (top-down).

How can I achieve that? Or is there any other way to set the jarName depending on the called sourceSet? Thanks!


(Peter Niederwieser) #2

I don’t understand. Where does the ‘server’ task come from? In any case, you’d have to do sth. like this:

gradle.taskGraph.whenReady { graph ->
  if (graph.hasTask(tasks.server) { ... }
}

Instead of using such conditional logic, a better approach would be to use two Jar tasks configured with the desired archive names.


(Furdui Marian) #3

The ‘server’ comes from the ‘server’ sourceSet not from a task. As I mentioned, this is a simplified version. Actually, for server/job sourceSets there are also other configurations like ‘srcDirs’. The server finally ends in a ‘war’ which has the ‘jar’ and other specific resources in it. The job is a ‘rpm’ with the war… and other specific resources.

I need the build to run using specific configurations for job and server and I need some extra properties besides ‘srcDirs’.

Is there something that I misunderstood?

Thanks!


(Peter Niederwieser) #4

The ‘server’ comes from the ‘server’ sourceSet not from a task.

Is there something that I misunderstood?

You cannot call a source set from the command line.

I don’t have enough context for what you are trying to achieve, but it seems that you are trying to squeeze different things into the same tasks. Often, it is better to have separate tasks whose configuration always stays the same.

If you do want to do conditional configuration based on which task is going to be executed, you can use ‘gradle.taskGraph.whenReady’ as I’ve shown earlier.