Add sourcesJar task to custom Gradle plugin

前端 未结 1 2018
别那么骄傲
别那么骄傲 2021-01-13 09:17

My company recently wrote gradle plugin for vanilla configuration (repositories, common dependencies across projects, etc). Overall, this has greatly simplified our build p

相关标签:
1条回答
  • 2021-01-13 10:07

    When you define a task in build.gradle script one (out of 4) of task method is called - you can see the first one here. As you can also see none of these methods matches the DSL - with which - you define a task in build.gradle. Why? Because before execution every build.gradle is evaluated to translate the DSL to appropriate methods invocations. For further details please have a look at this question.

    The mentioned mechanism does not work in a custom plugin - here the code is interpreted as is without translating it. As you can see here there's no sourcesJar method defined on Project class. To create a task inside a plugin you need to invoke on of the mentioned task methods, e.g.:

    task('sourcesJar', type: Jar, dependsOn: classes) {
       classifier = 'sources'
       from sourceSets.main.allSource
    }
    

    which invokes exactly this method (I know the arguments order is different but this is how groovy works - trust me).

    Also you don't need project.configure(project) {...}, project.with {...} will be enough.

    0 讨论(0)
提交回复
热议问题