Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle: Set a JVM option on an ANT task

Tags:

gradle

ant

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:

  • Setting the systemProperty on all tasks with JavaForkOptions:

    tasks.withType(JavaForkOptions) {
      systemProperty 'myproperty', 'blah'
    }
    
  • Passing -Dmyproperty=blah when I invoke gradle.
  • Various things involving ant.systemPropery, ant.options.forkOptions, ant.forkOptions, etc. (I can't actually find reliable documentation on this anywhere)

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.

like image 709
Travis Gockel Avatar asked Sep 24 '14 22:09

Travis Gockel


1 Answers

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
    }
}
like image 176
Benjamin Muschko Avatar answered Oct 05 '22 01:10

Benjamin Muschko