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

时间:2016-06-16 22:44:56

标签: java gradle groovy build-process gradle-plugin

我的公司最近编写了用于vanilla配置的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外,此插件效果很好。当我将它添加到混合中(并编译/部署到我们的本地仓库)时,当我尝试构建使用该插件的项目时,我收到此错误:

$ 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

1 个答案:

答案 0 :(得分:4)

当您在build.gradle脚本中定义任务时,会调用task方法中的一个(总共4个) - 您可以看到第一个here。您也可以看到这些方法都不匹配DSL - 您可以在build.gradle中定义任务。为什么?因为在执行之前,每个build.gradle都会被评估为 DSL转换为适当的方法调用。有关详细信息,请查看this问题。

上述机制在自定义插件中不起作用 - 此处代码按解释而不用翻译。您可以看到heresourcesJar类上定义了Project方法。要在插件中创建任务,您需要调用上述task方法,例如:

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

完全调用this方法(我知道参数顺序不同但这是groovy的工作原理 - 相信我)。

此外,您不需要project.configure(project) {...}project.with {...}就够了。