将sourcesJar任务添加到自定义Gradle插件 [英] Add sourcesJar task to custom Gradle plugin

查看:1163
本文介绍了将sourcesJar任务添加到自定义Gradle插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的公司最近为香草配置编写了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实现插件< Project> {

void apply(Project project){

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

project.configure(项目){
println('将Java属性应用于:'+ project.name)

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

version = date

//使用本地回购
存储库{
maven {
url$ externalDependenciesRepo
}
maven {
url$ internalDependenciesRepo
}
}

uploadArchives {
repositories {
mavenDeployer {
//部署到内部仓库
repository(url:$ i nternalDependenciesRepo)
}
}
}

//通用依赖项
依赖项{
编译组:'log4j',名称:' log4j',版本:'1.2.17'
编译组:'org.slf4j',名称:'slf4j-log4j12',版本:'1.6.6'
编译组:'org.slf4j' ,名称:'slf4j-api',版本:'1.6.6'
testCompilejunit:junit:$ junit_version
}

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

任务sourcesJar(类型:Jar,dependsOn:类){
classifier ='sources'
from sourceSets.main.allSource
}

artifacts {
archives sourcesJar
}
}
}
}

这个插件效果很好,除了 sourcesJar 。当我将其添加到组合中(并编译/部署到本地回购)时,当我尝试构建使用该插件的项目时,出现此错误:

  $ gradle:myProject:clean -x测试
将Java属性应用到:myProject

FAILURE:构建失败,出现异常。

*其中:
构建文件'C:\Users\me\Documents\code\root\myProject\build.gradle'行:1

*出错:
评估项目':myProject'时发生问题。
>无法应用插件[id'customjava']
>无法为参数找到方法sourcesJar()[{type = class org.gradle.api.tasks.bundling.Jar,dependsOn = task':analytics-isr:classes'},com.mycompany.plugins.OurJavaPlugin $ _apply_closure1 $ _closure6 @ 4c1d59cd]在项目':myProject'上。

*尝试:
使用--stacktrace选项运行以获取堆栈跟踪。使用--info或--debug选项运行以获取更多日志输出。

BUILD FAILED

总时间:3.352秒


build.gradle 脚本中定义一个任务时,任务中的一个(满分为4) / code>方法被调用 - 你可以看到第一个这里。正如你也可以看到,这些方法都不匹配DSL--你可以在 build.gradle 中定义一个任务。为什么?因为在执行之前,每个 build.gradle 都会被计算为将这个DSL翻译成合适的方法调用。有关详细信息,请查看问题。



所提到的机制在自定义插件中无效 - 这里的代码被解释为,没有翻译 它。正如您在这里所见,没有<$ c在项目类中定义的$ c> sourcesJar 方法。要在插件中创建任务,您需要调用上面提到的任务方法,例如:

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

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

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


My company recently wrote gradle plugin for vanilla configuration (repositories, common dependencies across projects, etc). Overall, this has greatly simplified our build process and uncovered a few inconsistencies across projects. We recently tried to add a sourcesJar task to the plugin and it's not working.

Here's the broken plugin:

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

This plugin works great, except for the sourcesJar. When I add that into the mix (and compile/deploy to our local repo) I get this error when I try to build a project that uses the plugin:

$ 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

解决方案

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.

这篇关于将sourcesJar任务添加到自定义Gradle插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆