Whats the meaning of “with” in gradle

2019-02-25 03:45发布

问题:

In the answer https://stackoverflow.com/a/35879150 there is a with in the last line:

task gen (type: Jar) {
    description "Generates JAR without version number."
    archiveName = filename + ".jar"
    manifest {attributes 'Main-Class': mainFile}
    with jar
}

What is the exact meaning, and where is it documented? I couldn't find it in the gradle documentation and the with in groovy (http://groovy-lang.org/style-guide.html#_using_with_for_repeated_operations_on_the_same_bean) seems to be different.

回答1:

In your case you are calling the with()-method of the Jar class. (see the very bottom of the Jar DSL documentation and the Jar API documentation)

Adds the given specs as a child of this spec.

So, it's not the with()-method of Groovy.



回答2:

With with in this case you seem to call the closure named jar:

jar {
    baseName filename
    manifest {
        attributes 'Main-Class': mainFile
    }
}

task gen (type: Jar) {
    //....
    with jar
}

in run-time (when the builder is called) it'll be converted to:

task gen (type: Jar) {
    //....

  jar {
    baseName filename
    manifest {
        attributes 'Main-Class': mainFile
    }
  }
}