无需测试即可构建 Gradle
我想执行 gradle build 而不执行单元测试。我尝试过:
gradle -Dskip.tests build
这似乎没有任何作用。我还可以使用其他命令吗?
I want to execute gradle build
without executing the unit tests. I tried:
gradle -Dskip.tests build
That doesn't seem to do anything. Is there some other command I could use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
您应该使用排除任何任务的
-x
命令行参数。尝试:
更新:
彼得评论中的链接已更改。这是图表 docs.gradle.org/current/userguide/java_plugin.html" rel="noreferrer">Gradle 用户指南
You should use the
-x
command line argument which excludes any task.Try:
Update:
The link in Peter's comment changed. Here is the diagram from the Gradle user's guide
尝试:
要列出项目的所有可用任务,请尝试:
更新:
乍一看这可能不是最正确的答案,但请仔细阅读
gradletask
输出或文档。Try:
To list all available tasks for your project, try:
UPDATE:
This may not seem the most correct answer at first, but read carefully
gradle tasks
output or docs.您可以将以下行添加到
build.gradle
中,**/*
排除所有测试。You can add the following lines to
build.gradle
,**/*
excludes all the tests.接受的答案是正确的。
OTOH,我之前解决此问题的方法是将以下内容添加到所有项目中:
使用
-Dskip.tests=true
运行构建,所有测试任务都将被跳过。The accepted answer is the correct one.
OTOH, the way I previously solved this was to add the following to all projects:
Run the build with
-Dskip.tests=true
and all test tasks will be skipped.使用
-x test
跳过测试执行,但这也排除了测试代码编译。在我们的例子中,我们有一个 CI/CD 流程,其中一个目标是编译,下一个目标是测试(构建 -> 测试)。
因此,对于我们的第一个
Build
目标,我们希望确保整个项目编译良好。为此,我们使用了:对于下一个目标,我们只需执行测试:
Using
-x test
skip test execution but this also exclude test code compilation.In our case, we have a CI/CD process where one goal is compilation and next goal is testing (Build -> Test).
So, for our first
Build
goal we wanted to ensure that the whole project compiles well. For this we have used:On the next goal we simply execute tests:
gradle 中的每个操作都是一个
task
,test
也是如此。要从 gradle 运行中排除task
,您可以使用选项--exclude-task
或其简写-x
后跟任务名称需要排除哪些。示例:对于所有需要排除的任务,应重复使用
-x
选项。如果您的
build.gradle
文件中针对不同类型的测试有不同的任务,那么您需要跳过所有执行测试的任务。假设您有一个执行单元测试的任务test
和一个执行功能测试的任务testFunctional
。在这种情况下,您可以排除所有测试,如下所示:Every action in gradle is a
task
, and so istest
. And to exclude atask
from gradle run, you can use the option--exclude-task
or it's shorthand-x
followed by the task name which needs to be excluded. Example:The
-x
option should be repeated for all the tasks that needs to be excluded.If you have different tasks for different type of tests in your
build.gradle
file, then you need to skip all those tasks that executes test. Say you have a tasktest
which executes unit-tests and a tasktestFunctional
which executes functional-tests. In this case, you can exclude all tests like below:您可以排除任务
https://docs.gradle.org/current/userguide /command_line_interface.html#sec:command_line_executing_tasks
You can exclude tasks
https://docs.gradle.org/current/userguide/command_line_interface.html#sec:command_line_executing_tasks
禁用项目中的测试任务的不同方法是:
如果您想禁用一个项目(或一组项目)中的测试,有时需要此行为。
这种方式适用于所有类型的测试任务,而不仅仅是 java“测试”。而且,这种方式很安全。这就是我的意思
假设:您有一组不同语言的项目:
如果我们尝试在主
build.gradle
中添加这种记录:如果我们没有名为tests的任务,我们将在项目中失败
the different way to disable test tasks in the project is:
this behavior needed sometimes if you want to disable tests in one of a project(or the group of projects).
This way working for the all kind of test task, not just a java 'tests'. Also, this way is safe. Here's what I mean
let's say: you have a set of projects in different languages:
if we try to add this kind of record in main
build.gradle
:we will fail in a project when if we have no task called tests
参考
要从 gradle 中排除任何任务,请使用
-x< /code> 命令行选项。请参阅下面的示例
输出:
gradle -q dist -x runningTest
希望这能为您提供基本的信息
Reference
To exclude any task from gradle use
-x
command-line option. See the below exampleOutput of:
gradle -q dist -x runningTest
Hope this would give you the basic
在 The Java Plugin:
Gradle build without test 你有两个选择:
但是如果你想要编译测试:
In The Java Plugin:
Gradle build without test you have two options:
but if you want compile test:
这是 Kotlin DSL (build.gradle.kts) 的解决方案:
像这样执行构建:
要包含正在运行的测试:
This is a solution for Kotlin DSL (build.gradle.kts):
Execute the build like this:
To include running tests:
请尝试以下操作:
gradlew -DskipTests=true build
Please try this:
gradlew -DskipTests=true build