How to execute jar file in gradle?

2019-05-21 15:56发布

问题:

When I use the ant to build my android project , I need some task like this:

<java jar="./build_tools/yuicompressor-2.4.8.jar" fork="true" failonerror="true">
    <arg value="-o"/>
    <arg value="${out.dir}/all-min-mobile.js"/>                         
    <arg value="--charset"/>
    <arg value="utf-8"/>
    <arg value="${out.dir}/all-mobile.js"/>
</java>

I need to do the process in my gradle build process.Then I put following script in my build.gradle file:

task (runCompressTool , dependsOn: jar, type: JavaExec) {

classpath files('./build_tools/yuicompressor-2.4.8.jar')
main 'com.yahoo.platform.yui.compressor.Bootstrap'
args[0] = '-o'
args[1] = '${buildDir}/all-min-web.js'
args[2] = '--charset'
args[3] = 'utf-8'
args[4] = '--type'
args[5]='js'
args[6] = '${buildDir}/all-web.js'

} When I run this task , it failed and just told me the exit code is 1 and didn't tell me what is wrong. So is there any body who can help me?

回答1:

Have a look at the JavaExec task that is shipped with Gradle. You can find a description at the gradle dsl reference at: http://gradle.org/docs/current/dsl/org.gradle.api.tasks.JavaExec.html