fbl*_*fbl 5 java groovy build-process gradle 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
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这个插件效果很好,除了sourcesJar
.当我将它添加到混合中(并编译/部署到我们的本地repo)时,当我尝试构建使用该插件的项目时,我收到此错误:
$ 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
Run Code Online (Sandbox Code Playgroud)
当您在build.gradle
脚本中定义任务时,task
将调用一个(共 4 个)方法 - 您可以在此处看到第一个。正如您还看到的,这些方法都与 DSL 不匹配 - 您在build.gradle
. 为什么?因为在执行之前,每个都会build.gradle
被评估以将DSL转换为适当的方法调用。有关更多详细信息,请查看此问题。
提到的机制在自定义插件中不起作用 - 这里代码被解释为不翻译它。正如你在这里看到的,没有sourcesJar
在Project
类上定义方法。要在插件中创建任务,您需要调用上述task
方法,例如:
task('sourcesJar', type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
Run Code Online (Sandbox Code Playgroud)
它完全调用了这个方法(我知道参数顺序是不同的,但这就是 groovy 的工作原理 - 相信我)。
你也不需要project.configure(project) {...}
,project.with {...}
就足够了。
归档时间: |
|
查看次数: |
1459 次 |
最近记录: |