Cannot add Gradle Task 'Wrapper' as a task with that name already exists.

Answered

Hi,

 

I just created a brand new Gradle project using Intellij Idea community version. 

In my build.gradle I just add a task

task wrapper(type: Wrapper) {
gradleVersion = '3.5'
}

 

So my build.gradle looks like this 

 

When I "refresh all Gradle project" , It shows 

 

When I delete the task , then the error disappears. 

I am wondering, why I cannot add a task wrapper in my build file ? 

 

Thanks.

6 comments
Comment actions Permalink

Please select Settings (Preferences on macOS) | Build, Execution, Deployment | Build Tools | Gradle | Use gradle 'wrapper' task configuration option if you want to use Gradle via your own wrapper task in a build.gradle file. See documentation for Gradle settings about this option.

0
Comment actions Permalink

The root cause for the problem is a change made in gradle 5, see https://docs.gradle.org/current/userguide/upgrading_version_4.html#rel4.8:configure_internal_tasks

So probably IDEA uses a standalone gradle 5 (instead of the wrapper). To re-configure that Andrey's answer is right.

Note that you do not need any wrapper configuration in your build.gradle at all to use the wrapper.

 

1
Comment actions Permalink

Thanks Andrey and Stephen. 

Andrey, when I import a project and on the import wizard I choose Use gradle 'wrapper' task configuration , then all good. 

The imported project will download the gradle previous version 

But if I want to create a new Gradle project , even after I follow your suggestion "select Settings (Preferences on macOS) | Build, Execution, Deployment | Build Tools | Gradle | Use gradle 'wrapper' task configuration option " , I put 

task wrapper(type: Wrapper) {
gradleVersion = '3.5'
}

Intellij will keep complaining cannot add task wrapper. 

 

@Stephen, when we don't put any wrapper configuration in our build.gradle, then when we build it on the CI server and invoke gradlew , how can gradle know which version it should download ?   Thanks.  

0
Comment actions Permalink

@Jimmy: The gradle version that gets downloaded is determined by the values in the file gradle/wrapper/gradle-wrapper.properties

(You should check in the complete "gradle" folder (but *not* the ".gradle" obviously)) plus of course the gradlew and gradlew.bat files.

0
Comment actions Permalink

>Intellij will keep complaining cannot add task wrapper. 

Not sure I understand the steps to reproduce. I get the "can not add task 'wrapper' error" only when I select Settings/Preferences | Build, Execution, Deployment | Build Tools | Gradle | Use local gradle distribution pointing it ot Gradle 5+ version and add wrapper task to the build.gradle script - which is expected behaviour (as Stephen mentioned in the 1st message).

0
Comment actions Permalink

Alternatively you can configure the task in the new way:

wrapper {
    gradleVersion = '4.4'
}

This is recommended in Gradle 4.0+. Source:  Stack Overflow.

1

Please sign in to leave a comment.