无需测试即可构建 Gradle

发布于 2024-10-10 05:02:40 字数 159 浏览 0 评论 0原文

我想执行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

云朵有点甜 2024-10-17 05:02:40

您应该使用排除任何任务的 -x 命令行参数。

尝试:

gradle build -x test 

更新:

彼得评论中的链接已更改。这是图表 docs.gradle.org/current/userguide/java_plugin.html" rel="noreferrer">Gradle 用户指南

You should use the -x command line argument which excludes any task.

Try:

gradle build -x test 

Update:

The link in Peter's comment changed. Here is the diagram from the Gradle user's guide

耀眼的星火 2024-10-17 05:02:40

尝试:

gradle assemble

要列出项目的所有可用任务,请尝试:

gradle tasks

更新:

乍一看这可能不是最正确的答案,但请仔细阅读 gradletask 输出或文档。

Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.

Try:

gradle assemble

To list all available tasks for your project, try:

gradle tasks

UPDATE:

This may not seem the most correct answer at first, but read carefully gradle tasks output or docs.

Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
So要识趣 2024-10-17 05:02:40

您可以将以下行添加到 build.gradle 中,**/* 排除所有测试。

test {
    exclude '**/*'
}

You can add the following lines to build.gradle, **/* excludes all the tests.

test {
    exclude '**/*'
}
夕色琉璃 2024-10-17 05:02:40

接受的答案是正确的。

OTOH,我之前解决此问题的方法是将以下内容添加到所有项目中:

test.onlyIf { ! Boolean.getBoolean('skip.tests') }

使用 -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:

test.onlyIf { ! Boolean.getBoolean('skip.tests') }

Run the build with -Dskip.tests=true and all test tasks will be skipped.

千紇 2024-10-17 05:02:40

使用 -x test 跳过测试执行,但这也排除了测试代码编译。

gradle build -x test 

在我们的例子中,我们有一个 CI/CD 流程,其中一个目标是编译,下一个目标是测试(构建 -> 测试)。

因此,对于我们的第一个 Build 目标,我们希望确保整个项目编译良好。为此,我们使用了:

./gradlew build testClasses -x test

对于下一个目标,我们只需执行测试:

./gradlew test

Using -x test skip test execution but this also exclude test code compilation.

gradle build -x test 

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:

./gradlew build testClasses -x test

On the next goal we simply execute tests:

./gradlew test
静谧幽蓝 2024-10-17 05:02:40

gradle 中的每个操作都是一个 tasktest 也是如此。要从 gradle 运行中排除 task,您可以使用选项 --exclude-task 或其简写 -x 后跟任务名称需要排除哪些。示例:

gradle build -x test

对于所有需要排除的任务,应重复使用 -x 选项。

如果您的 build.gradle 文件中针对不同类型的测试有不同的任务,那么您需要跳过所有执行测试的任务。假设您有一个执行单元测试的任务 test 和一个执行功能测试的任务 testFunctional 。在这种情况下,您可以排除所有测试,如下所示:

gradle build -x test -x testFunctional

Every action in gradle is a task, and so is test. And to exclude a task 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:

gradle build -x test

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 task test which executes unit-tests and a task testFunctional which executes functional-tests. In this case, you can exclude all tests like below:

gradle build -x test -x testFunctional
jJeQQOZ5 2024-10-17 05:02:40

禁用项目中的测试任务的不同方法是:

tasks.withType(Test) {enabled = false}

如果您想禁用一个项目(或一组项目)中的测试,有时需要此行为。

这种方式适用于所有类型的测试任务,而不仅仅是 java“测试”。而且,这种方式很安全。这就是我的意思
假设:您有一组不同语言的项目:
如果我们尝试在主build.gradle中添加这种记录:

 subprojects{
 .......
 tests.enabled=false
 .......
}

如果我们没有名为tests的任务,我们将在项目中失败

the different way to disable test tasks in the project is:

tasks.withType(Test) {enabled = false}

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:

 subprojects{
 .......
 tests.enabled=false
 .......
}

we will fail in a project when if we have no task called tests

游魂 2024-10-17 05:02:40

参考

要从 gradle 中排除任何任务,请使用 -x< /code> 命令行选项。请参阅下面的示例

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

希望这能为您提供基本的信息

Reference

To exclude any task from gradle use -x command-line option. See the below example

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'
}

Output of: gradle -q dist -x runningTest

task compile
compile test
running distribution job

Hope this would give you the basic

懵少女 2024-10-17 05:02:40

The Java Plugin:

$ 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

In The Java Plugin:

$ 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 you have two options:

$ gradle assemble
$ gradle build -x test

but if you want compile test:

$ gradle assemble testClasses
$ gradle testClasses
傾旎 2024-10-17 05:02:40

这是 Kotlin DSL (build.gradle.kts) 的解决方案:

tasks.withType<Test> { // OR tasks.test {
    onlyIf {
        properties["areTestsEnabled"].toString().toBoolean()
    }
}

像这样执行构建:

./gradlew build

要包含正在运行的测试:

./gradlew build -PareTestsEnabled=true

This is a solution for Kotlin DSL (build.gradle.kts):

tasks.withType<Test> { // OR tasks.test {
    onlyIf {
        properties["areTestsEnabled"].toString().toBoolean()
    }
}

Execute the build like this:

./gradlew build

To include running tests:

./gradlew build -PareTestsEnabled=true
反目相谮 2024-10-17 05:02:40

请尝试以下操作:

gradlew -DskipTests=true build

Please try this:

gradlew -DskipTests=true build

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文