开发者

How do I execute ant.java properly from gradle?

开发者 https://www.devze.com 2023-03-26 15:31 出处:网络
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:

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'])
}


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.


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.


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)
}


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消