gradle plugin not found after publishToMavenLocal

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.)

  1. 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.

  2. 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.

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

  1. 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'
        }
    }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật