110

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?

marius bardan
  • 4,962
  • 4
  • 29
  • 32
  • 3
    To make sure errors you get are not from wrong indexing by gradle. Or if you added new libraries which change every dependency. – Tschallacka Mar 13 '15 at 09:31
  • "gradel clean build" helps to remove all dependencies which generated while last build and get new dependencies from server and build your project. Thanks – Naitik Mar 13 '15 at 09:32
  • 6
    @Naitik, that's not true. – Opal Mar 13 '15 at 09:41
  • so then, if I just change the code and want a new build, a 'gradle build' should be enough? And only if I change the dependencies should I run 'gradle clean build'? – marius bardan Mar 13 '15 at 09:43
  • 1
    I think this is becoming a more interesting question now with incremental builds. [Bazel](http://bazel.io/faq.html) says _"Since Bazel does not need clean builds for correctness, the CI system should not be configured to clean before starting a build/test run."_. I'd like to know if Gradle is getting closer to this goal, too. – mkobit Feb 22 '16 at 21:33
  • Just `./gradlew cleanBuild` – A.Casanova Apr 13 '23 at 10:49

5 Answers5

115

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.

Amnon Shochot
  • 8,998
  • 4
  • 24
  • 30
  • thanks for answering. I'm aware what clean does, but I don't know a specific case of when it is useful. Can you provide such an example, for better understanding? Thanks. – marius bardan Mar 13 '15 at 11:41
  • Sure. 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. – Amnon Shochot Mar 13 '15 at 12:03
  • Makes sense. Can you edit your answer to include this example so I can mark it as correct? – marius bardan Mar 13 '15 at 12:41
  • 1
    I thought if you deleted test classes, that made the up-to-date check for test fail, which would result in running the tests again anyway. – Hakanai May 02 '19 at 06:46
  • 2
    Basically: in a perfect world `clean` would never be necessary, but there are changes (mostly removals) that Gradle can't (reliably) detect. In those cases `clean` is the easy escape hatch. – Joachim Sauer Oct 06 '20 at 12:10
66

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

Sudip Bhandari
  • 2,165
  • 1
  • 27
  • 26
1

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.

Adrian Liu
  • 385
  • 3
  • 13
  • 2
    This is mostly inaccurate, Gradle has input and output tracking and almost all tasks can handle this case. – mkobit Oct 26 '16 at 02:35
  • Gradle merely allows for more in-depth control over tasks and their input/output. However assumptions about when a task is up-to-date and when it will be executed are easily made - this contributes to the perception of Gradle as some kind of dark magic. – Koenigsberg Mar 25 '19 at 13:29
1

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.

Dagguh
  • 11
  • 3
0

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