具有依赖关系的Gradle自定义插件jar [英] Gradle custom plugin jar with dependencies
问题描述
我正在尝试为自定义gradle插件构建一个jar,以供其他gradle项目使用。我正在用Java编写插件。我遇到了一个问题,包括jar中的依赖项。如果我使用下面的 build.gradle
I'm trying to build a jar for a custom gradle plugin to be used by other gradle projects. I'm using java to write the plugin. I'm having a problem including dependencies in my jar. If I build the jar using the below build.gradle
plugins {
id 'groovy'
}
repositories{
mavenCentral()
}
dependencies {
compile gradleApi()
compile localGroovy()
compile 'com.google.guava:guava:27.0-jre'
testCompile 'junit:junit:4.12'
//compile 'org.apache.commons:commons-lang3:3.8.1'
}
group = 'com.mine'
version = '1.0'
在将插件应用于项目时,我收到了番石榴类的NoClassDefFound异常。如果我在 build.gradle
I get a NoClassDefFound exception for guava classes when applying the plugin on a project. If I include a task to create a jar with dependencies like below in the build.gradle
jar {
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it)}
}
}
它说未找到带有ID my-plugin的插件
。如何在gradle插件jar中包含依赖项?
It says Plugin with Id 'my-plugin' not found
. How do I include dependencies in a gradle plugin jar?
推荐答案
您的插件项目应配置为独立的插件项目,然后发布到一个Maven存储库,这将使依赖关系解析工作;在此处,有专门的文档,特别是以下部分:< a href = https://docs.gradle.org/current/userguide/custom_plugins.html#sec:using_the_java_gradle_plugin_development_plugin rel = nofollow noreferrer>使用Gradle插件开发插件
Your plugin project should be configured as a standalone Plugin project and then published to a maven repository, which will make dependencies resolution work; there is good documentation about writing custom plugin here, specially the following part : using Gradle plugin development plugin
在此处的Gradle示例中还有一个编写/发布/使用自定义插件的好例子: https://github.com/gradle/gradle/tree/master/subprojects/docs/src/samples/plugins (请参阅两个子项目发布
和消费
)
There is also a good example of writing/publishing/consuming a custom Plugin in the Gradle examples here : https://github.com/gradle/gradle/tree/master/subprojects/docs/src/samples/plugins (see the two subprojects publishing
and consuming
)
这是一个可行的方法依赖于外部库(例如,公共语言)的插件的示例:
And here is a working example with a plugin that has dependency on external library (commons-lang for example):
插件项目
b uild.gradle
plugins {
id 'java-gradle-plugin'
id 'groovy'
id 'maven-publish'
}
group 'org.gradle.sample.plugin'
version '0.1'
// pugin metadata configuration
gradlePlugin {
plugins {
myplugin {
id = "org.gradle.sample.plugin.myplugin"
implementationClass = "org.gradle.sample.plugin.MyPlugin"
}
}
}
// publish to local maven repo for testing
publishing {
repositories {
maven {
url "../repos/maven-repo"
}
}
}
// repo for dependences resolution
repositories{
jcenter()
}
// dependencies of this plugin
dependencies {
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.8.1'
}
插件实现: src / main / groovy / org / grad le / sample / plugin / MyPLugin.groovy
package org.gradle.sample.plugin
import org.apache.commons.lang3.StringUtils
import org.gradle.api.Plugin
import org.gradle.api.Project
class MyPlugin implements Plugin<Project> {
@Override
void apply(final Project project) {
println "Applying custom plugin... "
project.tasks.create('testPlugin'){
doLast{
println " custom plugin task executing."
println "Result: " + StringUtils.capitalize("stringtotest")
}
}
}
}
构建并发布此插件。/gradlew publish
:插件jar和插件标记伪像将会发布到 ../ repos / maven-repo
Build and publish this plugin ./gradlew publish
: the plugin jar and "plugin marker artefacts" will be published to local maven repo in ../repos/maven-repo
消费者项目
build.gradle
plugins {
id 'java'
// import/apply your custom plugin
id 'org.gradle.sample.plugin.myplugin' version '0.1'
}
group 'org.gradle.sample.plugin'
version '0.1'
repositories{
maven {
url "../repos/maven-repo"
}
jcenter()
}
要测试插件,尝试执行插件任务 testPlugin
To test the plugin, try to execute the plugin task testPlugin
> Task :testPlugin
custom plugin task executing.
Result: Stringtotest
这篇关于具有依赖关系的Gradle自定义插件jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!