Travis Gockel
Travis Gockel

Reputation: 27633

Gradle: Set a JVM option on an ANT task

I'm using Gradle 2.1 and have an ANT task defined something like this:

task myTask {
  doFirst {
    ant.taskdef(name:      'mytask',
                classname: 'com.blah.Blah',
                classpath: configurations.gen.asPath
               )
    ant.mytask(foo: 'bar')
  }
}

There is a property I need to pass to the com.blah.Blah as a JVM argument (because, instead of doing something sane like passing parameter values in as parameters, the creators of this ANT task have decided that system properties are a reasonable way of conveying information). I've tried a number of things, including:

I'm at a loss here. It feels like I should be able to say something like:

task myTask {
  doFirst {
    ant.taskdef(name:      'mytask',
                classname: 'com.blah.Blah',
                classpath: configurations.gen.asPath
               )
    ant.systemProperty 'myProperty', 'blah'
    ant.mytask(foo: 'bar')
  }
}

...but that obviously doesn't work.

Upvotes: 6

Views: 2518

Answers (1)

Benjamin Muschko
Benjamin Muschko

Reputation: 33436

In Gradle you can use Groovy so there's nothing preventing you from setting the system property programmatically as shown below:

task myTask {
    doFirst {
        System.setProperty('myProperty', 'blah')
        // Use AntBuilder
        System.clearProperty('myProperty')
    }
}

Keep in mind that Gradle's AntBuilder executes Ant logic in the same process used for Gradle. Therefore, setting a system property will be available to other tasks in your build. This might have side effects when two tasks use the same system property (depending on the execution order) or if you run your build in parallel.

Instead you might want to change your Ant task to use Ant properties instead to drive your logic (if that's even an option). Ant properties can be set from Gradle as such:

task myTask {
    doFirst {
        ant.properties.myProperty = 'blah'
        // Use AntBuilder
    }
}

Upvotes: 4

Related Questions