Why does :jar Task does not refresh jar file after changing configuration destination directory

    jar {
        into ('lib') {
                       from configurations.compile.collect {zipTree it}

After changing the (‘lib’) to (‘lib2’) command jar tvf <filepath>.jar shows that jar is not updated. Why i am forced to call clean? How it works? Can i show to jar task, that content should be updated without :clean?

Looking at the code for the Jar task and its hierarchy,
the jar output is declared as the final “.jar” file created by the archive.

Since it was not changed by your new configuration, the jar task is seen as UP-TO-DATE.

You can set outputs.upToDateWhen{false} to always run the task.
You can maybe find something more subtle to detect when the task is or is not up to date, but I can’t think about anything that is not dodgy / not too complicated, rather than always rebuilding your jar.

The two key things to realize is that Gradle only runs tasks when it believes either the declared inputs or outputs of the task are out of date. This is different from other build systems, which either have nothing like this, or just rudimentary logic. The other thing to realize is that the build script itself is not one of the declared inputs of tasks. It would be amazingly nice if tasks could note that certain parts of the build script should be its dependencies, but that’s not done right now.