Why does extracting this method not work in gradle?

Jens Schauder

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.
Steinar

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
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does this setter method not work?

From Dev

Why does the applicationWillResignActive method not work?

From Dev

Why does setattr does not work as a method?

From Dev

Why does this method not work without static?

From Dev

Binding an event to a method, why does it work in UWP?

From Dev

Why does my image preloading method work?

From Dev

Why does my disableButtons method not work?

From Dev

Why does passing a method as an argument (&block) not work?

From Dev

Why does the java substring method work like this?

From Dev

Main method with generic parameter; why does it work?

From Dev

Why does a string method not work in home controller?

From Dev

Why does this method not work without static?

From Dev

Why does indexOf method of ArrayList work?

From Dev

Why does my bubble sort method not work?

From Dev

Why does method level validation not work in Spring?

From Dev

Why __add__ magic method does not work?

From Dev

Why does not work the use of an extension method in the same extension method?

From Dev

Why this anonymous method does not work while the lambda does?

From Dev

Why UpdateModel() binding of IList does not work, but as a method parameter it does?

From Dev

Why this anonymous method does not work while the lambda does?

From Dev

What does Float::INFINITY do, and why does it work for this sorting method?

From Dev

Why does case matter in gradle task method names?

From Dev

Extracting text using sed does not work as expected

From Dev

Why does passing a variable by reference not work when invoking a reflective method?

From Dev

Why does it work to return an int in a method which should return an object?

From Dev

Why does my custom equals method (doubles and integers) not work?

From Dev

Why does this extension method work with generics but not a set base type?

From Dev

Override hashCode-method in Java: Why does it work?

From Dev

Why does my delegate not work when passed in as a parameter to a method?

Related Related

  1. 1

    Why does this setter method not work?

  2. 2

    Why does the applicationWillResignActive method not work?

  3. 3

    Why does setattr does not work as a method?

  4. 4

    Why does this method not work without static?

  5. 5

    Binding an event to a method, why does it work in UWP?

  6. 6

    Why does my image preloading method work?

  7. 7

    Why does my disableButtons method not work?

  8. 8

    Why does passing a method as an argument (&block) not work?

  9. 9

    Why does the java substring method work like this?

  10. 10

    Main method with generic parameter; why does it work?

  11. 11

    Why does a string method not work in home controller?

  12. 12

    Why does this method not work without static?

  13. 13

    Why does indexOf method of ArrayList work?

  14. 14

    Why does my bubble sort method not work?

  15. 15

    Why does method level validation not work in Spring?

  16. 16

    Why __add__ magic method does not work?

  17. 17

    Why does not work the use of an extension method in the same extension method?

  18. 18

    Why this anonymous method does not work while the lambda does?

  19. 19

    Why UpdateModel() binding of IList does not work, but as a method parameter it does?

  20. 20

    Why this anonymous method does not work while the lambda does?

  21. 21

    What does Float::INFINITY do, and why does it work for this sorting method?

  22. 22

    Why does case matter in gradle task method names?

  23. 23

    Extracting text using sed does not work as expected

  24. 24

    Why does passing a variable by reference not work when invoking a reflective method?

  25. 25

    Why does it work to return an int in a method which should return an object?

  26. 26

    Why does my custom equals method (doubles and integers) not work?

  27. 27

    Why does this extension method work with generics but not a set base type?

  28. 28

    Override hashCode-method in Java: Why does it work?

  29. 29

    Why does my delegate not work when passed in as a parameter to a method?

HotTag

Archive