怎样在构建过程中使用Gradle运行控制台命令?

huangapple go评论87阅读模式
英文:

How can I run a console command in Gradle upon build?

问题

task buildRustDebug(type: Exec) {
    workingDir './native'
    commandLine 'cargo', 'build'
}

task buildRustRelease(type: Exec) {
    workingDir './native'
    commandLine 'cargo', 'build', '--release'
}

task buildRust() {
    dependsOn 'buildRustDebug'
    dependsOn 'buildRustRelease'
}

gradle.projectsEvaluated {
    build.dependsOn buildRust
}
英文:

I'm currently working on a project that has a native component in Rust and a Java component. The main build system is Gradle, and I want Gradle to invoke the Rust build system (Cargo) upon build, every time, to ensure that the proper shared libraries exist for JNI to take place, and for those libraries to be recompiled as needed. After research, I came up with this:

task buildRustDebug(type:Exec){
    workingDir './native'
    commandLine 'cargo', 'build'
}

task buildRustRelease(type:Exec){
    workingDir './native'
    commandLine 'cargo', 'build', '--release'
}

task buildRust(){
    dependsOn 'buildRustDebug'
    dependsOn 'buildRustRelease'
}

build dependsOn 'buildRust'

This all works fine, except for the last line that establishes a dependency with the build task. This line, when present, gives the following error when I attempt to run any task:

Build file '<censored>' line: 23

A problem occurred evaluating root project '<censored>'.
> Could not get unknown property 'dependsOn' for root project '<censored>' of type org.gradle.api.Project.

It looks like somehow the build task doesn't exist until after the code in my main file runs. How can I accomplish what I want? Note that this code is in the root project of a multi-project Gradle build.

答案1

得分: 1

你在问题上收到的评论是正确的。但只是为了给你一些背景(并且使得可以将问题标记为已解决):

Gradle在脚本语言方面使用Groovy或Kotlin。你正在使用的是Groovy。

当你写下dependsOn 'buildRustDebug'时,你正在调用一个名为 dependsOn 的方法,参数是字符串 buildRustDebug。这是有效的,因为在Groovy中,方法括号是可选的。所以这与 dependsOn('buildRustDebug') 是相同的。

当你写下build dependsOn 'buildRust'时,语法是错误的。你想在 build 对象上调用 dependsOn 方法并传递一个参数。可以尝试这样:

build.dependsOn 'buildRust' // 或者
build.dependsOn('buildRust')

注意方法调用之前的点号。

另一件事是,build 是依赖树中的“最外层”任务之一,它本身依赖于其他任务,比如 compileJavatest。如果你想确保在组装应用程序或对其进行测试之前先构建和准备好你的Rust项目,可能需要将依赖关系向下移动一点。否则你可能会冒着 buildRust 任务是最后一个执行的风险,然而它应该是其中一个最早执行的任务。

英文:

The comment you got on the question is correct. But just to give you some context (and make it possible to mark the question as solved):

Gradle uses either Groovy or Kotlin for the scripting language. You are using Groovy.

When you write dependsOn 'buildRustDebug', you are calling a method called dependsOn with the String argument 'buildRustDebug'. This works because in Groovy, method parenthesis are optional. So it is the same as dependsOn('buildRustDebug').

When you write build dependsOn 'buildRust', it is syntactically incorrect. You want to call the dependsOn method on the build object with an argument. Try this instead:

build.dependsOn 'buildRust' // Or
build.dependsOn('buildRust')

Notice the dot before the method call.

Another thing is that build is one of the "outermost" tasks in the dependency trees, and it itself depends on other tasks like compileJava and test. If you want to make sure your Rust project is built and available before assembling your application or testing it, you probably need to move the dependency down the tree a bit. Otherwise you risk that the buildRust task is the very last thing it does, whereas it should probably be one of the first things it does.

huangapple
  • 本文由 发表于 2020年8月27日 10:29:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63608261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定