简体   繁体   English

如何从gradle正确执行ant.java?

[英]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: 我试图调用一个罐子,但我没有看到任何输出,当我不ARGS运行命令,当与ARGS跑,我得到以下错误:

[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? 如何以这样的方式调用ant.java,我看到输出并可以传递参数?

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: 你的args应该像这样指定:

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. 除了使用Groovy样式标记构建器而不是XML之外,它几乎与使用ant相同。

By default your output will go to the screen. 默认情况下,您的输出将转到屏幕。 If you want to redirect it, set the 'output' property. 如果要重定向它,请设置'output'属性。

As I said before, it's best to use the JavaExec task. 正如我之前所说,最好使用JavaExec任务。 To execute a Jar, you can do: 要执行Jar,您可以:

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. http://issues.gradle.org/browse/GRADLE-1274中的注释也解释了如何捕获ant.java输出,但使用JavaExec是更好的解决方案。

To get the output set the --info flag on gradle or set the outputproperty on ant.java: 要获取输出,请在gradle上设置--info标志或在ant.java上设置outputproperty:

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

In Addition to Chris Dail's answer , you can also use something like this 除了Chris Dail的答案,你也可以使用这样的东西

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. 这允许我们在一行中声明所有参数,非常类似于ANT中的用法。

The Ant task needs to be invoked in the execution phase, not the configuration phase: 需要在执行阶段调用Ant任务,而不是配置阶段:

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

You could also use Gradle's JavaExec task. 您也可以使用Gradle的JavaExec任务。 See the documentation. 请参阅文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM