0

Does use of the gradle version in the wrapper task eliminate the need to use gradlew?

wrapper {
    gradleVersion = '4.10.2'
}

task foo {
    println "...bar"
}

The wrapper seems to be automatically executed as part of the "Configure project" phase and, if so, seems to be a "cleaner" way to manage this (i.e., less files to deal with, embedded in the actual gradle build script, etc.).

New to Gradle, just trying to figure things out...

Add...

I added the "foo" task to clarify my point of confusion.

1 - I am executing the script using "gradle foo"

2 - In the process, I note that the "wrapper" task is automatically invoked

My point-of-confusion is that it seemed that including the wrapper entry would force the right version of Gradle to be used (i.e., why else would the wrapper task be automatically invoked). On inspection, it appears that including the version in the wrapper task is another way to get the version into Gradle's properties file (vice providing it on the command line when generating the wrapper).

Live-and-learn, thanks for dealing with this question.

SoCal
  • 801
  • 1
  • 10
  • 23
  • If you didn't use gradlew, how would you run gradle? https://docs.gradle.org/current/userguide/gradle_wrapper.html, https://docs.gradle.org/current/dsl/org.gradle.api.tasks.wrapper.Wrapper.html – JB Nizet Nov 08 '18 at 17:10
  • you should read some documentation about wrapper, and this SO answer : https://stackoverflow.com/a/44861408/6899896 – M.Ricciuti Nov 08 '18 at 18:09
  • there is a default task called `wrapper` which initializes or configures the wrapper in your current project: the `wrapper { gradleVersion = '4.10.2' }`configuration block is used to define the version of the wrapper you want. – M.Ricciuti Nov 08 '18 at 18:17
  • " I am executing the script using "gradle foo" 2 - In the process, I note that the "wrapper" task is automatically invoked" can you share some logs/trace of this behavior? because it does not seem normal. `wrapper` task execution should not be triggered automatically. are you running gradle in commandline or in an IDEA plugin ? (eclipse or IntelliJ maybe?) – M.Ricciuti Nov 08 '18 at 21:19
  • I am running Gradle from the command line. I stuffed a println in there to see what's being invoked, and it printed under the "> Configure project :" heading. With more "playing around", this appears to be because the println statement was not inside a doLast{} block. – SoCal Nov 08 '18 at 21:29

1 Answers1

0

I am executing the script using "gradle foo" 2 - In the process, I note that the "wrapper" task is automatically invoked

No, it's not. If it was, then the following script would print "hello":

wrapper {
    gradleVersion = '4.10.2'
    doLast {
        println 'hello'
    }
}

task foo {
    println "...bar"
}

What you see there is the configuration of the wrapper task, which would be invoked if you executed gradle wrapper, and would install (or upgrade) the wrapper for the version 4.10.2 of gradle in the project.

This task configuration is not needed anymore. As indicated in the documentation, you can invoke the wrapper task and specify the version directly on the command line with gradle wrapper --gradle-version 4.10.2.

Note that, in your script, the line println "...bar" is executed when the task foo is configured, not when it's executed. Whatever tak you execute, you will always see bar... printed in the console.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255