How to publish custom gradle plugin to private nexus

Hi,

I wrote a custom gradle java based plugin for gradle version 4.5.1 and I try to publish it to a private nexus via the new gradle publish plugin.

// gradle plugin configuration 
group = 'com.mycompany.gradle'
gradlePlugin {
  plugins {
    MyGradlePlugin {
      id = 'com.mycompany.gradle.myFirstPlugin'
      implementationClass = 'com.mycompany.gradle.myFirstPlugin.GradlePlugin'
    }
  }
}

// and configuring the plugin task

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
    repositories {
        maven {
            credentials {
                username "$nexusUsername"
                password "$nexusPassword"
            }

            if(project.version.endsWith('-SNAPSHOT')) {
                url "${nexusUrl}${relativePathSnapshots}"
            } else {
                url "${nexusUrl}${relativePathReleases}"
            }
        }
    }
}

During the build I can see that there are 3 poms generated
build/publications/pluginMaven/pom-default.xml
build/publications/mavenJava/pom-default.xml
build/publications/MyPluginNameMarkerMaven/pom-default.xml

Where the pluginMaven and the mavenJava pom are equal.
So during the publish tasks it trys to upload the artefact twice, which is by default disabled for releaseRepositories in our nexus.

So I think I have misconfigured the publish task.
I’m not sure how the java-gradle-plugin and the publish plugin work together and how to configure the pom generation.

Can somebody point me in the right direction?

1 Like

Solved it by myself, this block makes the trouble, just deleting it and it works :wink:

Thank you a lot for this thread. I experienced the same issue, getting this error from our private nexus server:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':artifact-client-gradle-plugin:artifact-client-gradle-plugin:publishPluginMavenPublicationToMavenRepository'.
> Failed to publish publication 'pluginMaven' to repository 'maven'
   > Could not write to resource 'https://example.com/nexus/repository/maven-releases/com/example/tools/some-gradle-plugin/1.0.0/some-gradle-plugin-1.0.0.jar'.
      > Could not PUT 'https://example.com/nexus/repository/maven-releases/com/example/tools/some-gradle-plugin/1.0.0/some-gradle-plugin-1.0.0.jar'. Received status code 400 from server: Repository does not allow updating assets: maven-releases

Similar to you I had a publications bloc in the publishing section to publish the sources of the plugin:

task sourcesJar(type: Jar, dependsOn: classes) {
    classifier = 'sources'
    from sourceSets.main.allSource
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
            artifact sourcesJar
        }
    }
    repositories {
        //...
    }
}

In the snapshots repository I have observed following publications:

  1. The java artifact (group Id: com:example:tools; artifactId: some-gradle-plugin) containing the jar and the sources

  2. Then a plugin artifact (groupId == “pluginId” and artifactId == "pluginId + .gradle.plugin") which is only a pom file referencing the plugin:

  3. A second publication of the java artifact without the sources.

This is possible for snapshots repo, but for release repo, there is a conflict between “1)” and “3)”.


A suggested, removing publications bloc from the publishing bloc, removes the step “1)” from the list above.

This way only “2)” and “3)” are executed during the publish phase and this is possible during a release.


My guess is that using 'java-gradle-plugin' makes some addition to the publication logic.

My question:
How to write the publishing section to upload the java-sources jar during the publication of the plugin?

With newer version of Gradle (I tried 5.6.2) I have seen this message when pushing to a local folder as repository:

> Task :publishPluginMavenPublicationToLocalRepository
Multiple publications with coordinates 'org.example:plugin-maven-publish:0.1-SNAPSHOT' are published to repository 'local'. The publications will overwrite each other!

If instead of a local folder you push to a nexus that is configured to reject overwrites (in case of a release repository it makes sense).


How to write the publishing section to upload the java-sources jar during the publication of the plugin?

I got a reply from Marc Philipp on twitter/github (see this PR in particular):

You should add the artifacts to the pluginMaven publication instead of creating a second one ( mavenJava )

Like this:

publishing {
    // afterEvaluate is necessary because java-gradle-plugin
    // creates its publications in an afterEvaluate callback
    afterEvaluate {
        publications {
            pluginMaven {
                // customize main publications here
                artifact sourcesJar
                artifact javadocJar
                pom {
                    name = "main artifact"
                }
            }
        }
    }
}

How to publish gradle plugin on github package registry