Whats the meaning of “with” in gradle

2019-02-25 03:46发布

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.

2条回答
该账号已被封号
2楼-- · 2019-02-25 04:08

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
    }
  }
}
查看更多
狗以群分
3楼-- · 2019-02-25 04:22

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.

查看更多
登录 后发表回答