Stefan Kendall
Stefan Kendall

Reputation: 67802

How do I execute ant.java properly from gradle?

I'm trying to invoke a jar, but I don't see any output when I run the command without args, and when I do run with args, I get the following error:

[ant:java] The args attribute is deprecated. Please use nested arg elements.
[ant:java] Java Result: 1

How do I invoke ant.java in such a way that I see output and can pass arguments?

task compressJs(){
  ant.java(jar:"lib/yuicompressor-2.4.6.jar",fork:true,args:['js/file.js', '-o', 'build/js/file.js'])
}

Upvotes: 11

Views: 7659

Answers (5)

Nishok
Nishok

Reputation: 1

In Addition to Chris Dail's answer , you can also use something like this

ant.java(jar:"lib/yuicompressor-2.4.6.jar",fork:true) {
    arg(line: "js/file.js -o build/js/file.js")
}

This allows one to declare all the arguments in a single line, very similar to the usage in ANT.

Upvotes: 0

Peter Niederwieser
Peter Niederwieser

Reputation: 123890

As I said before, it's best to use the JavaExec task. To execute a Jar, you can do:

task exec(type: JavaExec) { 
    main = "-jar" 
    args relativePath("lib/yuicompressor-2.4.6.jar") 
    args ... // add any other args as necessary 
}

The comments in http://issues.gradle.org/browse/GRADLE-1274 also explain how to capture output from ant.java, but using JavaExec is the better solution.

Upvotes: 8

thoredge
thoredge

Reputation: 12591

To get the output set the --info flag on gradle or set the outputproperty on ant.java:

task compressJs(){
  ant.java(outputproperty: 'cmdOut', jar:"lib/yuicompressor-2.4.6.jar",fork:true,args:['js/file.js', '-o', 'build/js/file.js'])
  println(ant.project.properties.cmdOut)
}

Upvotes: 1

Peter Niederwieser
Peter Niederwieser

Reputation: 123890

The Ant task needs to be invoked in the execution phase, not the configuration phase:

task compressJs() << { // note the <<
  ant.java(...)
}

You could also use Gradle's JavaExec task. See the documentation.

Upvotes: 0

Chris Dail
Chris Dail

Reputation: 26029

Your args should be specified like this:

ant.java(jar:"lib/yuicompressor-2.4.6.jar",fork:true) {
    arg(value: "js/file.js")
    arg(value: "-o")
    arg(value: "build/js/file.js")
}

Pretty much it is the same as you would do with ant except using the Groovy style markup builder instead of XML.

By default your output will go to the screen. If you want to redirect it, set the 'output' property.

Upvotes: 17

Related Questions