37

I'm just learning Gradle, coming from both an Ant+Ivy and Maven background and I'm trying to wrap my head around the proper way to publish a release version of my software using gradle. I also need to constantly integrate my projects without having to constantly release independent artifacts.

Ant + Ivy

In the Ant + Ivy world, I can create publishSnapshot and publishRelease targets. These can each use different Ivy resolvers to resolve and publish to my separate snapshot or release repositories in Nexus.

Maven

With Maven, I can define a snapshotRepository or repository in my distributionManagement section to have maven publish to my separate snapshot or release repositories in Nexus.

Gradle

Now enter gradle. How can I achieve this same functionality? I am using ivy style repositories.

Thank you for any insights you can provide.

ANSWER

Thanks to René's answer below, I was finally able to create a workable solution. The crux of the matter was that I needed to constantly integrate across all my projects. To do this I thought that declaring a dependency using version number latest.integration was the only way to pull in the latest version of my libraries, and therefore I needed to use ivy style repositories.

In fact, there are other ways to pull in the latest version of libraries in order to continuously integrate my software across all projects. The solution is to use the uploadArchives exactly as René has listed below (also note you will need to apply plugin: 'maven' for this to work. Make sure your repositories are also maven style, and when declaring a dependency, you can use dynamic version numbers as shown here. In my case, I listed a global version number in my common.gradle and in downstream projects, I used version: version to reference the global version variable. In this way, each artifact in my system has the same version. When it's time to release, I can change this from 1.0-SNAPSHOT to 1.0 and build each one in order.

Community
  • 1
  • 1
dev
  • 2,949
  • 5
  • 37
  • 48

2 Answers2

61

You can configure the snapshot and the release repository in the 'Upload' task (e.g. the uploadArchives) task:

uploadArchives {  
    repositories {  
        mavenDeployer {  
            repository(url: 'http://myCompanyRepo.com:8081/releases') {  
                authentication(userName: 'admin', password: 'password');  
            }  
            snapshotRepository(url: 'http://myCompanyRepo.com:8081/snapshots') {
                authentication(userName: 'admin', password: 'password');  
            }  
        }  
    }  
}

For *-SNAPSHOT versions the snapshotRepository is used. Otherwise the releases repo is used.

JJD
  • 50,076
  • 60
  • 203
  • 339
Rene Groeschke
  • 27,999
  • 10
  • 69
  • 78
  • sorry, I should have clarified that I am using ivy style repositories. i will update the question. – dev May 27 '13 at 16:06
  • 1
    Nexus does not support ivy style repositories. It is m uch better to use a Maven repository format since it is understood by all build tools including Maven, Gradle, Gant, sbt, Leiningen, Ivy and others.. – Manfred Moser May 31 '13 at 20:06
  • I seem to be able to publish with ivy settings to Nexus? I need to publish as ivy in order to use latest.integration. If I switch to a maven repo format in Nexus I won't be able to use this feature. – dev Jun 03 '13 at 19:11
  • i overcame the latest.integration limitation by using dynamic revision numbers which allowed me to use maven repo format and implement your solution above. updated the question with the answer – dev Jul 11 '13 at 18:27
  • Quick point on publishing ivy settings to Nexus. It will resolve the library but not the transient libraries. Something to be aware of. The reason for this is because it expects to find a pom with the library, which lists the dependencies, and there is none- only the ivy.xml – avanderw Mar 07 '14 at 11:46
  • Just re-emphasizing: gradle automatically selects snapshotRespository instead of repository depending on whether `project.version` ends in `-SNAPSHOT`. That's a bit to magic for my liking but for now I'm just glad I can do what I want. – Sridhar Sarnobat Dec 08 '22 at 00:39
58

If you want to use the new maven-publish plugin, you can upload to different repositories using an if statement:

apply plugin: 'maven-publish'

...

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

Reference: maven-publish and setting snapshotRepository and releaseRepository

Paolo Fulgoni
  • 5,208
  • 3
  • 39
  • 55