简体   繁体   English

使用 Gradle 任务执行 Gradle 命令

[英]Execute Gradle command with Gradle task

I want to re-create verification-metadata.xml file with single click.我想通过单击重新创建 verify-metadata.xml 文件。 I can create verification-metadata.xml with this command ./gradlew --write-verification-metadata sha256 .我可以使用此命令./gradlew --write-verification-metadata sha256创建 verify-metadata.xml 。 I try to create Gradle Task inside the build.gradle(app) and execute, but it didn't work我尝试在 build.gradle(app) 中创建 Gradle 任务并执行,但没有成功

task verificationMeta(type: Exec) {
    workingDir "${rootDir}"
    commandLine './gradlew ', '--write', '-verification', '-metadata', ' sha256'
    doLast {
        println "Executed!"
    }
}

rootDir is root directory of project. rootDir是项目的根目录。

This code gives me error;这段代码给了我错误;

Execution failed for task ':verificationMeta'.
> A problem occurred starting process 'command './gradlew ''

How can I make it?我怎样才能做到?

Are you working on windows machine?您在 windows 机器上工作吗?

We had the problem that on windows machine you have to call gradlew.bat我们遇到的问题是在 windows 机器上你必须调用 gradlew.bat

Example:例子:

task prodRepackage(type: Exec) {
group = "Build"
if (OperatingSystem.current().isWindows()) {
    executable "gradlew.bat"
    args "-Pprod", "bootJar"
} else {
    executable "./gradlew"
    args "-Pprod", "bootJar"
}

} }

As a workaround for the issue, I use.bat or.sh scripts to be executed as the following.作为该问题的解决方法,我使用 .bat 或 .sh 脚本执行如下。

task executeCMD(type:Exec) {
  workingDir '.'
  commandLine 'test.bat'
     doLast {
         println "Executed!"
     }
 }

This, for example, will execute the test.bat script, which only has one line例如,这将执行 test.bat 脚本,该脚本只有一行

./gradlew --write-verification-metadata sha256

if you're using Linux/macOS, you can replace the.bat with.sh.如果您使用的是 Linux/macOS,则可以将 .bat 替换为 .sh。

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

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