Gradle build without tests

JavaGradle

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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.

Solution 3 - Java

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

test {
    exclude '**/*'
}

Solution 4 - Java

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.

Solution 5 - Java

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

Solution 6 - Java

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

Solution 7 - Java

Solution 8 - Java

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

Solution 9 - Java

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

Solution 10 - Java

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

Solution 11 - Java

Please try this:

gradlew -DskipTests=true build

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaveView Question on Stackoverflow
Solution 1 - Javac_makerView Answer on Stackoverflow
Solution 2 - JavaEmil SitView Answer on Stackoverflow
Solution 3 - JavaGuisong HeView Answer on Stackoverflow
Solution 4 - JavaDavid ResnickView Answer on Stackoverflow
Solution 5 - JavaSahil ChhabraView Answer on Stackoverflow
Solution 6 - JavaFederico PiazzaView Answer on Stackoverflow
Solution 7 - JavaPehmoleluView Answer on Stackoverflow
Solution 8 - JavaSergey YakovlevView Answer on Stackoverflow
Solution 9 - JavaSuganthan Madhavan PillaiView Answer on Stackoverflow
Solution 10 - JavaArnau Sistach ReinosoView Answer on Stackoverflow
Solution 11 - JavaSarath Babu PolavarapuView Answer on Stackoverflow