Is Gradles `jar` a method?

妖精的绣舞 提交于 2019-12-02 04:24:35

The answer is yes and no. The syntax is syntactic sugar (although in this case it makes things confusing). It is not a function jar that is called, but the function tasks.getByName that gets the name and the closure as a parameter.

On https://docs.gradle.org/current/userguide/more_about_tasks.html it says in part 17.3. Configuring tasks (Example 17.9. Configuring a task - with closure):

This works for any task. Line 3 of the example is just a shortcut for the tasks.getByName() method. It is important to note that if you pass a closure to the getByName() method, this closure is applied to configure the task, not when the task executes.

So you can also write:

version = '1.0'
tasks.getByName "jar", {
    manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart',
                   'Implementation-Version': version
    }
}
RaGe

Probably not a comprehensive answer you're looking for, but some pointers in the right direction:

The internals of the jar task don't seem to be documented in a lot of detail, so you'll have to look through source to get to what you're looking for. The jar task comes from the java plugin here. Which in turn adds a task from this class. This class doesn't do a whole lot beyond extending from Jar.java.

So it would seem the closure:

jar {
        ....
}

calls the constructor here and sets the properties defined in the jar closure. Further Jar class extends from Zip.java, while overriding the extension of the archive file, and adding a manifest property.

--

PS: This still doesn't quite help me understand with jar from your original question, but I like your line of questioning, I'm learning a few things along the way!

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