Gradle release and publish not working - always showing UP-TO-DATE

212 Views Asked by At

I'm trying to set up two projects with Gradle to perform releases and publish the results to a Nexus repository server. Both are failing with a similar problem - the release step is working, and the "SNAPSHOT" version is replaced by the released version, and committed and pushed to git, but the publish step is not doing anything / not running.

The setup I'm trying to achieve is:

  • Local (snapshot) builds should be published to local Maven cache.
  • Release (CI) builds should be published to our Nexus repository server.

My build.gradle:

plugins {
  id 'java'
  id 'net.researchgate.release' version '2.8.1'
  id 'maven-publish'
}

group 'com.something'

repositories {
  mavenLocal()
  mavenCentral()
  maven {
      url = uri('http://nexus:8080/repository/myrepo/')
      allowInsecureProtocol = true
  }
}

java {
  withSourcesJar()
}

publishing {
  publications {
    maven(MavenPublication) {
        from components.java
    }
  }
}

// publish every build to local maven, to enable local testing
publishToMavenLocal.dependsOn(check)
build.dependsOn(publishToMavenLocal)
// publish release builds to Nexus
afterReleaseBuild.dependsOn publish

Sample log output:

Task :release
Task :CPA-GJAP-JOB1:CPA-GJAP-JOB1:check
Task :CPA-GJAP-JOB1:CPA-GJAP-JOB1:generateMetadataFileForMavenPublication
Task :CPA-GJAP-JOB1:CPA-GJAP-JOB1:generatePomFileForMavenPublication
Task :CPA-GJAP-JOB1:CPA-GJAP-JOB1:publishMavenPublicationToMavenLocal
Task :CPA-GJAP-JOB1:CPA-GJAP-JOB1:publishToMavenLocal
Task :CPA-GJAP-JOB1:CPA-GJAP-JOB1:build
Task :CPA-GJAP-JOB1:CPA-GJAP-JOB1:publish UP-TO-DATE
Task :CPA-GJAP-JOB1:CPA-GJAP-JOB1:afterReleaseBuild UP-TO-DATE
Task :CPA-GJAP-JOB1:runBuildTasks
Task :CPA-GJAP-JOB1:preTagCommit
Task :CPA-GJAP-JOB1:createReleaseTag
Task :CPA-GJAP-JOB1:checkoutMergeFromReleaseBranch SKIPPED
Task :CPA-GJAP-JOB1:updateVersion
Task :CPA-GJAP-JOB1:commitNewVersion
Task :compileJava UP-TO-DATE
Task :processResources NO-SOURCE
Task :classes UP-TO-DATE
Task :jar
Task :sourcesJar
Task :assemble
Task :compileTestJava UP-TO-DATE
Task :processTestResources UP-TO-DATE
Task :testClasses UP-TO-DATE
Task :test UP-TO-DATE
Task :check UP-TO-DATE
Task :generateMetadataFileForMavenPublication
Task :generatePomFileForMavenPublication
Task :publishMavenPublicationToMavenLocal
Task :publishToMavenLocal
Task :build

The publish task appears to show as UP-TO-DATE. What am I missing?

Gradle version: 7.4

1

There are 1 best solutions below

0
On BEST ANSWER

A huge thank you to Björn Kautler who answered this question over on the Gradle forum:

https://discuss.gradle.org/t/release-plugin-publish-not-publishing-released-artifacts/46088

I actually had three problems!

  1. I did not realize that the nexus repository details appear in two places! The repository for publication appears in the publishing block, entirely separate from the base "repositories" section, which lists repos where we can get dependencies.
  2. You need logic to set the repo URL to either snapshot or release version.
  3. Missing the login credentials for the repository. These should not be part of the build.gradle file, but rather on the machine, and accessed via the credentials() method.

With these changes, the fixed working config was:

publishing {
  publications {
    maven(MavenPublication) {
        from components.java
    }
  }

  repositories {
    // define our repo for publishing.
    // Note that you do not need to include maven local here to enable publishing of snapshots
    // to the local repo
    maven {
        name = "myRepo"
        def releasesRepoUrl = "http://myRepoURL:8080/repository/releases"
        def snapshotsRepoUrl = "http://myRepoURL:8080/repository/snapshots"
        url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
        allowInsecureProtocol = true
        // credentials are not stored in the project, put them in gradle.properties on your build server
        credentials(PasswordCredentials)
    }
  }
}