Global properties set on sourceSets

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:
apply plugin: 'java'
  apply from: 'dependencies.gradle'
group = ''
  jar.enabled = true
  configurations {
  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!

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.

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?


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.