Whats the meaning of “with” in gradle

非 Y 不嫁゛ 提交于 2019-12-20 02:56:33

问题


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


来源:https://stackoverflow.com/questions/35888820/whats-the-meaning-of-with-in-gradle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!