我想在本地 Maven 存储库中发布我的插件,但它不起作用。该插件看似已发布,但发布后却找不到。即使使用由
gradle init
生成的“hello-world”插件也会发生这种情况。
我错过了什么?
(我知道有人会想将这个问题标记为重复。我见过很多类似的问题,并且建议的内容可能已经在我的代码中(见下文),尤其是 settings.gradle 中的内容。)
我使用 gradle 6.2.1(不是最新的,但有一个现有项目我将在其中使用该插件)。 Ubuntu 22.04.3 LTS jammy.
我让gradle为我创建一个插件:
mkdir foobarplugin
cd foobarplugin/
~/some/existing-project/gradlew init
> Task :wrapper
Select type of project to generate:
1: basic
2: application
3: library
4: Gradle plugin
Enter selection (default: basic) [1..4] 4
Select implementation language:
1: Groovy
2: Java
3: Kotlin
Enter selection (default: Java) [1..3] 1
Select build script DSL:
1: Groovy
2: Kotlin
Enter selection (default: Groovy) [1..2] 1
Project name (default: foobarplugin): FooBar
> Task :init
Source package (default: FooBar): com.foo.bar
插件构建,测试运行正常。
设置.gradle:
rootProject.name = 'greeting'
(否则会在存储库中创建一个以项目名称命名的目录。实际上,有时可能会创建3个目录,代码的不同部分似乎从不同的结构中获取目录名称。下面您可以看到指定了
group id
在两个不同的地方。)
build.gradle(我的更改标有
//!!!
):
plugins {
// Apply the Java Gradle plugin development plugin to add support for developing Gradle plugins
id 'java-gradle-plugin'
// Apply the Groovy plugin to add support for Groovy
id 'groovy'
id 'maven-publish' //!!!
}
group 'com.foo.bar' //!!! (otherwise publishToMavenLocal fails with "Invalid publication 'pluginMaven': groupId cannot be empty.")
repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
dependencies {
// Use the awesome Spock testing and specification framework
testImplementation 'org.spockframework:spock-core:1.3-groovy-2.5'
}
gradlePlugin {
// Define the plugin
plugins {
greeting {
id = 'com.foo.bar.greeting'
implementationClass = 'com.foo.bar.FooBarPlugin'
}
}
}
// Add a source set for the functional test suite
sourceSets {
functionalTest {
}
}
gradlePlugin.testSourceSets(sourceSets.functionalTest)
configurations.functionalTestImplementation.extendsFrom(configurations.testImplementation)
// Add a task to run the functional tests
task functionalTest(type: Test) {
testClassesDirs = sourceSets.functionalTest.output.classesDirs
classpath = sourceSets.functionalTest.runtimeClasspath
}
check {
// Run the functional tests as part of `check`
dependsOn(tasks.functionalTest)
}
publishing { //!!!
publications {
maven(MavenPublication) {
groupId = 'com.foo.bar'
artifactId = 'greeting'
version = '1.1'
from components.java
}
}
}
./gradlew clean
./gradlew build
./gradlew publishToMavenLocal
我得到:
~/.m2/repository/com/foo$ tree --charset ASCII
.
`-- bar
`-- greeting
|-- 1.1
| |-- greeting-1.1.jar
| |-- greeting-1.1.module
| `-- greeting-1.1.pom
|-- com.foo.bar.greeting.gradle.plugin
| |-- maven-metadata-local.xml
| `-- unspecified
| `-- com.foo.bar.greeting.gradle.plugin-unspecified.pom
|-- maven-metadata-local.xml
`-- unspecified
|-- greeting-unspecified.jar
|-- greeting-unspecified.module
`-- greeting-unspecified.pom
6 directories, 9 files
~/stackoverflow/useplugin$ ../foobarplugin/gradlew init
> Task :wrapper
Select type of project to generate:
1: basic
2: application
3: library
4: Gradle plugin
Enter selection (default: basic) [1..4] 2
Select implementation language:
1: C++
2: Groovy
3: Java
4: Kotlin
5: Swift
Enter selection (default: Java) [1..5] 3
Select build script DSL:
1: Groovy
2: Kotlin
Enter selection (default: Groovy) [1..2] 1
Select test framework:
1: JUnit 4
2: TestNG
3: Spock
4: JUnit Jupiter
Enter selection (default: JUnit 4) [1..4] 4
Project name (default: useplugin):
> Task :init
Source package (default: useplugin): com.stackoverflow.use
Get more help with your project: https://docs.gradle.org/6.2.1/userguide/tutorial_java_projects.html
BUILD SUCCESSFUL in 57s
2 actionable tasks: 2 executed
+pluginManagement {
+ repositories {
+ mavenLocal()
+ gradlePluginPortal()
+ }
+}
+
rootProject.name = 'useplugin'
构建.gradle:
id 'application'
+ id 'com.foo.bar.greeting' version '1.1'
}
并且它(
./gradlew tasks
)不起作用:
* What went wrong:
Plugin [id: 'com.foo.bar.greeting', version: '1.1'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'com.foo.bar.greeting:com.foo.bar.greeting.gradle.plugin:1.1')
Searched in the following repositories:
MavenLocal(file:/home/***/.m2/repository/)
Gradle Central Plugin Repository
我的猜测是 ID/姓名有问题,不知道是什么。
我错过了什么? 我如何发布和使用我的插件?
问题在于
version
还必须在两个地方指定(如 group
,但组有错误消息,版本没有消息)。
这就是为什么我在存储库中看到这些“未指定”的原因,它是缺少的版本。
plugins {
...
id 'maven-publish'
}
group 'com.foo.bar'
version '1.1' //!!!!!!!!!
repositories { ... }