I have gradle build script for a multi module project.
For better readability I'd like to extract some methods, but when I do the script fails with an exception:
Cannot add task ':signArchives' as a task with that name already exists.
Full reproducable example: Have an otherwise empty directory, with two files in it:
settings.gradle
include 'eins', 'zwei'
build.gradle
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
allprojects {
apply plugin: 'signing'
}
subprojects {
signing {
sign configurations.archives
}
}
private Object signIt() {
signing {
sign configurations.archives
}
}
In that directory execute the following:
gradle wrapper
gradlew tasks
You'll get a list of available tasks as a result.
Change the build.gradle file to the following
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
allprojects {
apply plugin: 'signing'
}
subprojects {
signIt()
}
private Object signIt() {
signing {
sign configurations.archives
}
}
execute again:
gradlew tasks
Now you (or at least I) get:
> Cannot add task ':signArchives' as a task with that name already exists.
The subproject context is lost in the refactoring.
If you add a println project.name
into the signing closure, you can see that you're signing each subproject once in the first variant, while the root project is signed twice in the second variant.
You can fix it by for instance passing the subproject as a parameter to the signing method:
subprojects {
signIt(project)
}
private Object signIt(project) {
project.with {
signing {
sign configurations.archives
}
}
}