将 sourcesJar 任务添加到自定义 Gradle 插件

Add sourcesJar task to custom Gradle plugin

我公司最近编写了 gradle 普通配置插件(存储库、跨项目的公共依赖项等)。总的来说,这大大简化了我们的构建过程,并发现了项目之间的一些不一致之处。我们最近尝试向插件添加一个 sourcesJar 任务,但没有成功。

这是损坏的插件:

package com.mycompany.plugins

import org.gradle.api.Project
import org.gradle.api.Plugin
import org.gradle.api.tasks.bundling.Jar

class OurJavaPlugin implements Plugin<Project> {

    void apply(Project project) {

        def date = com.mycompany.util.UtilityFunctions.getDate()

        project.configure(project) {
            println('Applying Java properties to: ' + project.name)

            apply plugin: 'java'
            apply plugin: 'maven'
            apply plugin: 'idea'
            apply plugin: 'eclipse'

            version = date

            // Use the local repos
            repositories {
                maven {
                    url "$externalDependenciesRepo"
                }
                maven {
                    url "$internalDependenciesRepo"
                }
            }

            uploadArchives {
                repositories {
                    mavenDeployer {
                        // Deploy to internal repo
                        repository(url: "$internalDependenciesRepo")
                    }
                }
            }

            // Common dependencies
            dependencies {
                compile group: 'log4j', name: 'log4j', version:'1.2.17'
                compile group: 'org.slf4j', name: 'slf4j-log4j12', version:'1.6.6'
                compile group: 'org.slf4j', name: 'slf4j-api', version:'1.6.6'
                testCompile "junit:junit:$junit_version"
            }

            eclipse.project {
              natures 'org.springsource.ide.eclipse.gradle.core.nature'
            }

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

            artifacts {
                archives sourcesJar
            }
        }
    }
}

这个插件很好用,除了 sourcesJar。当我将其添加到组合中(并将 compile/deploy 添加到我们的本地存储库中)时,当我尝试构建使用该插件的项目时出现此错误:

$ gradle :myProject:clean -x Test
Applying Java properties to: myProject

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Users\me\Documents\code\root\myProject\build.gradle' line: 1

* What went wrong:
A problem occurred evaluating project ':myProject'.
> Failed to apply plugin [id 'customjava']
   > Could not find method sourcesJar() for arguments [{type=class org.gradle.api.tasks.bundling.Jar, dependsOn=task ':analytics-isr:classes'}, com.mycompany.plugins.OurJavaPlugin $_apply_closure1$_closure6@4c1d59cd] on project ':myProject'.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 3.352 secs

当您在 build.gradle 脚本中定义任务时,task 方法中的一个(共 4 个)被调用 - 您可以看到第一个 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 问题。

上述机制在自定义插件中不起作用 - 这里的代码被解释为原样,没有翻译它。如您所见,here Project class 上没有定义 sourcesJar 方法。要在插件中创建任务,您需要调用上述 task 方法之一,例如:

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

恰好调用 this 方法(我知道参数顺序不同,但这就是 groovy 的工作方式 - 相信我)。

也不需要project.configure(project) {...}project.with {...}就够了