我想在gradle build
不执行单元测试的情况下执行。我试过:
$ gradle -Dskip.tests build
这似乎没有任何作用。我可以使用其他命令吗?
尝试:
gradle assemble
要列出项目的所有可用任务,请尝试:
gradle tasks
更新:
起初这似乎不是最正确的答案,但请仔细阅读gradle tasks
输出或文档。
Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
您可以将以下行添加到build.gradle
,**/*
排除所有测试。
test {
exclude '**/*'
}
接受的答案是正确的。
OTOH,我之前解决此问题的方法是将以下内容添加到所有项目中:
test.onlyIf { ! Boolean.getBoolean('skip.tests') }
运行构建,-Dskip.tests=true
所有测试任务都将被跳过。
您可以排除任务
gradle build --exclude-task test
gradle 中的每一个动作都是一个task
,所以也是test
。task
并且要从 gradle 运行中排除一个,您可以使用该选项--exclude-task
或它的简写-x
,后跟需要排除的任务名称。例子:
gradle build -x test
-x
对于所有需要排除的任务,应重复该选项。
如果您的build.gradle
文件中有针对不同类型测试的不同任务,那么您需要跳过所有执行测试的任务。假设您有一个test
执行单元测试的任务和一个testFunctional
执行功能测试的任务。在这种情况下,您可以排除所有测试,如下所示:
gradle build -x test -x testFunctional
使用-x test
跳过测试执行,但这也排除了测试代码编译。
gradle build -x test
在我们的例子中,我们有一个 CI/CD 流程,其中一个目标是编译,下一个目标是测试(构建 -> 测试)。
因此,对于我们的第一个Build
目标,我们希望确保整个项目编译良好。为此,我们使用了:
./gradlew build testClasses -x test
在下一个目标上,我们只需执行测试。
在项目中禁用测试任务的不同方法是:
tasks.withType(Test) {enabled = false}
如果您想在一个项目(或一组项目)中禁用测试,有时需要这种行为。
这种方式适用于所有类型的测试任务,而不仅仅是java“测试”。而且,这种方式是安全的。这就是我的意思让我们说:你有一组不同语言的项目:如果我们尝试在 main 中添加这种记录build.gradle
:
subprojects{
.......
tests.enabled=false
.......
}
如果我们没有称为测试的任务,我们将在项目中失败
要从 gradle 中排除任何任务,请使用-x
命令行选项。请参阅下面的示例
task compile << {
println 'task compile'
}
task compileTest(dependsOn: compile) << {
println 'compile test'
}
task runningTest(dependsOn: compileTest) << {
println 'running test'
}
task dist(dependsOn:[runningTest, compileTest, compile]) << {
println 'running distribution job'
}
输出:gradle -q dist -x runningTest
task compile
compile test
running distribution job
希望这会给你基本的
在Java 插件中:
$ gradle tasks
Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
testClasses - Assembles test classes.
Verification tasks
------------------
test - Runs the unit tests.
Gradle build without test 你有两个选择:
$ gradle assemble
$ gradle build -x test
但如果你想编译测试:
$ gradle assemble testClasses
$ gradle testClasses
请试试这个:
gradlew -DskipTests=true build