I want to publish my plugin in my local Maven repository, and it does not work. The plugin looks published, but is not found after publishing. This happens even with the “hello-world” plugin generated by gradle init
.
What am I missing?
(I know that someone will want to mark this question as duplicate. I have seen many similar questions, and things suggested there are likely already in my code (see below), especially the things in settings.gradle.)
-
I use gradle 6.2.1 (not latest, but there is an existing project where I am going to use the plugin). Ubuntu 22.04.3 LTS jammy.
-
I let gradle create a plugin for me:
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
The plugin builds, tests run ok.
- Now, I add publishing to the generated plugin:
settings.gradle:
rootProject.name = 'greeting'
(otherwise a directory with the project’s name will be created in the repository. In fact, sometimes 3 directories may be created, different parts of the code seem to get the directory name from different structures. Below you see that group id
is specified in two different places.)
build.gradle (my changes are marked with //!!!
):
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
}
}
}
- I publish:
./gradlew clean
./gradlew build
./gradlew publishToMavenLocal
I get:
~/.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
- I let gradle create a Java project for me,
~/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
- I try to use my plugin:
settings.gradle:
+pluginManagement {
+ repositories {
+ mavenLocal()
+ gradlePluginPortal()
+ }
+}
+
rootProject.name = 'useplugin'
build.gradle:
id 'application'
+ id 'com.foo.bar.greeting' version '1.1'
}
and it (./gradlew tasks
) does not work:
* 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
My guess is that something is wrong with IDs/names, no idea what.
What am I missing?
How do I publish and use my plugin?
The problem was that version
also had to be specified in two places (like group
, but there was an error message for group, and no message for version).
This is why I saw these “unspecified” in the repository, it was the missing version.
plugins {
...
id 'maven-publish'
}
group 'com.foo.bar'
version '1.1' //!!!!!!!!!
repositories { ... }
~/.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
| |-- 1.1
| | `-- com.foo.bar.greeting.gradle.plugin-1.1.pom
| `-- maven-metadata-local.xml
`-- maven-metadata-local.xml
5 directories, 6 files
Note that with the above fix in place, this is a complete working example.
UPD as to specifying group-artifact-version in multiple places (artifact name in settings.gradle
, group and version in the beginning of build.gradle
, and all three in publishing {...}
), which is bad, because publishing breaks if the values are different, you may consider something like:
publishing {
publications {
maven(MavenPublication) {
groupId = project.group
artifactId = project.name
version = project.version
from components.java
// self-checking comment
assert project.group == 'com.foo.bar'
assert project.name == 'greeting'
assert project.version == '1.1'
}
}
}