marius bardan
marius bardan

Reputation: 5122

Why run 'gradle clean build' instead of 'gradle build'?

Why would I run gradle clean build instead of gradle build?

From what I understand, Gradle can detect source changes and update the final artifacts if needed. So why would I still need to clean?

Upvotes: 122

Views: 241497

Answers (5)

Dmitriy Pichugin
Dmitriy Pichugin

Reputation: 418

if you want to have single task "clean and build" you may create task

task rebuild() {
  dependsOn   clean
  finalizedBy build
}

and call it

gradlew rebuild

Upvotes: 2

Dagguh
Dagguh

Reputation: 41

You don't need to run the clean task. Gradle will track task dependencies and clean appropriate parts for you. Here's an example Gradle project I created to show that the accepted answer is incorrect.

If custom tasks don't track their dependencies well (they're bugged), then clean is a workaround.

Upvotes: 4

Sudip Bhandari
Sudip Bhandari

Reputation: 2285

It removes the build directory. (Build contains the output of the gradle operation) gradle clean demo

Upvotes: 71

Adrian Liu
Adrian Liu

Reputation: 385

Other build tools like buck will detect that some tests are removed and won't run them without the needs to run clean target. I think this is pitfall of gradle.

Upvotes: 1

Amnon Shochot
Amnon Shochot

Reputation: 9386

The clean task is defined by the java plugin and it simply removes the buildDir folder, thus cleaning everything including leftovers from previous builds which are no longer relevant. Not doing so may result in an unclean build which may be broken due to build artifacts produced by previous builds.

As an example assume that your build contains several tests that were failed and you decided that these are obsolete thus needs to be removed. Without cleaning the test results (using cleanTest task) or the build entirely (by running the clean task) you'll get stuck with the failed tests results which will cause your build to fail. Similar side effects can happen also with resources/classes removed from the sources but remained in the build folder that was not cleaned.

Upvotes: 123

Related Questions