Publish to maven repository


(Sverre Moe) #1

To use maven plugin or maven-publish plugin to publish artifacts to Nexus Maven Repository.

We are using Nexus 3 as Repository Manager to publish to either releases or snapshots repositories.

A similar topic has previously discussed the same, but for Artifactory and not properly concluded.

I wanted to use the new maven-publish for the reason that maven plugin might one day be gone/deprecated. However the maven-publish has been incubation forever and does not seem quite feature complete, yet.

The maven-publish lacks the ability to distinguish between releases and snapshots like maven plugin does.
Also the maven-publish plugin has a more lines of code than using the maven plugin, requiring publications.

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

task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}

artifacts {
    archives sourcesJar
    archives javadocJar
}

Using the maven plugin to publish the artifacts:

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: "https://nexus.company.com:8443/repository/releases") {
                authentication(userName: "deployment", password: "password")
            }
            snapshotRepository(url: "https://nexus.company.com:8443/repository/snapshots") {
                authentication(userName: "deployment", password: "password")
            }
       }
    }
}

Using maven-publish plugin to publish the artifacts.
The only solution I have found to publish to either releases or snapshots is checking the version for a snapshot to determine the repository URL.

publishing {
    repositories {
        maven {
            url 'https://nexus.company.com:8443/repository/' + publishUrl
            credentials {
                username = 'deployment'
                password = 'password'
            }
        }
    }

    publications {
        api(MavenPublication) {
            from components.java

            artifact sourcesJar {
                classifier "sources"
            }

            artifact javadocJar {
                classifier "javadoc"
            }
        }
    }
}

(Sverre Moe) #2

With Gradle 4.8 it seems the recommended approach is now to use the maven-publish plugin.

The Ivy Publish Plugin and Maven Publish Plugin that have been incubating since Gradle 1.3 are now marked as stable. Both plugins now support signing and publish configuration-wide dependency excludes. The Maven Publish Plugin introduces a new dedicated DSL for customizing the generated POM. In addition, some usage quirks with the publishing extension have been addressed which now behaves like other extension objects. Thus, these plugins are now the preferred option for publishing artifacts to Ivy and Maven repositories, respectively.

This new publishing support is the preferred option for publishing artifacts and will eventually replace publishing via the Upload task.

The Gradle 4.8-RC1 docs have an example how to achieve this:

publishing {
    repositories {
        maven {
            def releasesRepoUrl = "$buildDir/repos/releases"
            def snapshotsRepoUrl = "$buildDir/repos/snapshots"
            url version.endsWith("SNAPSHOT") ? snapshotsRepoUrl : releasesRepoUrl
        }
    }
}