Maven-publish and setting snapshotRepository and releaseRepository

I am updating our build scripts to use the “maven-publish” plugin instead of the “maven” plugin. Previously we did:

apply plugin: 'maven'
  repositories {
  maven {
    url "http://internal.repo"
  }
}
  uploadArchives {
  repositories {
    mavenDeployer {
      snapshotRepository(url: "http://internal.repo/libs-snapshot-local") {
        authentication(userName: 'admin', password: 'password')
      }
      repository(url: "http://internal.repo/libs-release-local") {
        authentication(userName: 'admin', password: 'password')
      }
    }
  }
}

The updated version looks like this:

apply plugin: 'maven-publish'
  repositories {
  maven {
    url "http://internal.repo"
  }
}
  publishing {
  publications {
    mavenJava(MavenPublication) {
      from components.java
    }
  }
  repositories {
    maven {
      credentials {
        username "admin"
        password "password"
      }
      url
"http://internal.repo/libs-snapshot-local"
    }
  }
}

But there is no “snapshotRepository” element. So how do I setup snapshot/release repositories when using the maven-publish plugin?

You can set a different repository URL based on the project version (if-statement).

Do it directly in a script works but when I try to move it to my common plugin:

void setup(Project project) {
      project.publishing {
      publications {
        mavenJava(MavenPublication) { from components.java }
      }
      repositories {
        maven {
          credentials {
            username "admin"
            password "password"
          }
            if(project.version.endsWith('-SNAPSHOT')) {
            url "http://my/artifactory/libs-snapshot-local"
          } else {
            url "http://my/artifactory/libs-release-local"
          }
        }
      }
    }

I get:

* What went wrong:
A problem occurred configuring the 'publishing' extension
> Could not find property 'components' on org.gradle.api.publish.maven.internal.publication.DefaultMavenPublication_Decorated@145a25f3.

where component.java is:

SoftwareComponentContainer org.gradle.api.Project.getComponents()

Do I miss a dependency to find the component when calling it on a plugin?

1 Like

It’s ‘project.components.java’. Or you wrap the whole method body with ‘project.configure(project) { … }’ to get the same ‘project’ scope as in a build script.