mavengradlegradle-pluginpublishing

Gradle how to publish a gradle-plugin to maven central


Hi I have a java project, which contains a submodule which is a gradle plugin. I want to publish this module to maven central.

I used this two plugins in my build.gradle:

plugins {
  id 'java-gradle-plugin'
  id 'maven-publish'
}

and my publishing block looks something like:

publishing {
    publications {
        javaLibrary(MavenPublication) {
            from components.java
            artifact sourcesJar
            artifact javadocJar

            artifactId = project.archivesBaseName

            pom {
                name = artifactId
                description = "..."

                url = 
                licenses {
                    license {
    
                    }
                }
                developers {
                   ...
                }
                scm {
                   ...
                }
                issueManagement {
                   ... 
                }
                ciManagement {
                   ...
                }
            }
        }
    }

    repositories { maven { url = "some local repo" } }
}

I noticed that when I build this module, the generated pom-default.xml is what I expected, but when I run gradle publishToMavenLocal and manually checked the pom.xml file in the .m2 folder, all the metadata like name description licenses are gone!

I also noticed in the .m2 folder there are 2 artifacts that are related to this single plugin, I think it's somewhat related with https://docs.gradle.org/current/userguide/plugins.html#sec:plugin_markers but I don't fully understand the meaning. Both these 2 artifacts' pom are missing the pom metadata as I described above.

Could some gradle expert help me here: how to keep the metadata in the published pom?


Solution

  • You should not need to manually define a MavenPublication for your plugin submodule. The java-gradle-plugin reacts to the application of the maven-publish plugin and automatically configures/creates publications for the plugin artifacts. See this line.

    You are correct for the (2) artifacts produced. One is the plugin marker (single pom.xml) and the other is the actual plugin JAR artifact.

    As for POM customization, Gradle seemingly provides its own the POM irrespective of any POM customization(s) you have defined: https://github.com/gradle/gradle/issues/17022