Does that even work? It's the wrapper which executes this, it can't change its own version
Yes it works, it just does not do what you think it does.
It configures the
wrapper
task which version to put into
gradle/wrapper/gradle-wrapper.properties
, it does not influence the current invocation of the wrapper.
And because of that you also always have to call the
wrapper
task twice for a proper update, because the templates for the 4 wrapper files are taken from the currently running Gradle version. When you use the
wrapper
task to update the Gradle version, you effectively just change the version in the properties file (unless your wrapper files were from an even older Gradle version). Running the
wrapper
task again then update the wrapper files from the new version now already being used.
Configuring the wrapper task in the build script has imho only one use-case. If you are disallowed to check in the wrapper files to VCS by some guidelines, then you can define there the version and wrapper checksum and so on, so that you can generate the wrapper files consistently after checkout of the project with some other projects wrapper or an installed Gradle version.