How to deploy a snapshot artifact to a Maven repository and produce valid maven-metadata.xml using Gradle?

2k views Asked by At

I have the following build.gradle script:

apply plugin: 'java'
apply plugin: 'maven'

compileJava {
    targetCompatibility = "1.8"
    sourceCompatibility = "1.8"
    options.encoding = 'UTF-8'
}

ext.username = System.getProperty('username') != null ? System.getProperty('username') : mavenUser
ext.password = System.getProperty('password') != null ? System.getProperty('password') : mavenPassword

description = "Example project that shows how to use Gradle to build and deploy artifacts to Nexus"

configurations {
    deployerJars
}

repositories {
    maven { url "http://nexus:8081/nexus/content/groups/my-group/" }
    mavenCentral()
}

dependencies {
    deployerJars "org.apache.maven.wagon:wagon-http:2.2"
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

// Gradle build Configuration to publish artifacts to maven artifact manager
uploadArchives {
    repositories.mavenDeployer {
            configuration = configurations.deployerJars
            repository(url: "http://nexus:8081/nexus/content/repositories/my-snapshots/") {
                authentication(userName: username, password: password)
            }
            pom.groupId = "com.foo.bar"
            pom.artifactId = "my-gradle-app"
            pom.version = "1.0-SNAPSHOT"
    }
}

// ------------------------------------------------------
// EXAMPLE: custom tasks for creating source/javadoc jars
task sourcesJar(type: Jar, dependsOn: classes) {
    classifier = 'sources'
    from sourceSets.main.allSource
}

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

// add javadoc/source jar tasks as artifacts
artifacts {
    archives sourcesJar
    archives javadocJar
}

When I execute the following, the build and deployment succeeds:

$ gradle clean upload -Dusername=myUsername -Dpassword=myPassword

The produced maven-metadata.xml file looks like this:

<metadata>
    <groupId>com.foo.bar</groupId>
    <artifactId>my-gradle-app</artifactId>
    <version>1.0-SNAPSHOT</version>
    <versioning>
        <snapshot>
            <timestamp>20170906.143113</timestamp>
            <buildNumber>1</buildNumber>
        </snapshot>
        <lastUpdated>20170906143113</lastUpdated>
    </versioning>
</metadata>

These are the files that have actually been deployed to Nexus:

my-gradle-app-1.0-20170906.143113-1-javadoc.jar
my-gradle-app-1.0-20170906.143113-1-javadoc.jar.md5
my-gradle-app-1.0-20170906.143113-1-javadoc.jar.sha1
my-gradle-app-1.0-20170906.143113-1-sources.jar
my-gradle-app-1.0-20170906.143113-1-sources.jar.md5
my-gradle-app-1.0-20170906.143113-1-sources.jar.sha1
my-gradle-app-1.0-20170906.143113-1.jar
my-gradle-app-1.0-20170906.143113-1.jar.md5
my-gradle-app-1.0-20170906.143113-1.jar.sha1
my-gradle-app-1.0-20170906.143113-1.pom
my-gradle-app-1.0-20170906.143113-1.pom.md5
my-gradle-app-1.0-20170906.143113-1.pom.sha1

Coming from a Maven background, I am expecting to see the following in the maven-metadata.xml file:

<metadata>
    <groupId>com.foo.bar</groupId>
    <artifactId>my-gradle-app</artifactId>
    <version>1.0-SNAPSHOT</version>
    <versioning>
        <snapshot>
            <timestamp>20170906.143113</timestamp>
            <buildNumber>1</buildNumber>
        </snapshot>
        <lastUpdated>20170906143113</lastUpdated>
    </versioning>
    <snapshotVersions>
        <snapshotVersion>
            <classifier>javadoc</classifier>
            <extension>jar</extension>
            <value>1.0-20170906.143113-1</value>
            <updated>20170906140847</updated>
        </snapshotVersion>
        <snapshotVersion>
            <classifier>sources</classifier>
            <extension>jar</extension>
            <value>1.0-20170906.143113-1</value>
            <updated>20170906140847</updated>
        </snapshotVersion>
        <snapshotVersion>
            <extension>jar</extension>
            <value>1.0-20170906.143113-1</value>
            <updated>20170906140844</updated>
        </snapshotVersion>
        <snapshotVersion>
            <extension>pom</extension>
            <value>1.0-20170906.143113-1</value>
            <updated>20170906140847</updated>
        </snapshotVersion>
    </snapshotVersions>
</metadata>

If I re-run the deployment, I would be expecting the maven-metadata.xml file to no only have the <snapshot/>'s <timestamp/> and <buildNumber> to be updated, but also to get new <snapshotVersion/> entries appended. What's happening instead is, the whole file is regenerated and there's just one <snapshotVersion/> and only the <snapshot/>'s <timestamp/> and <buildNumber> are correctly updated.

What am I missing here? Why isn't the maven-metadata.xml being produced properly? Is there a plugin that I am missing? Is this a bug in Gradle?

In addition, I have raised issue #2882 in the Gradle issue tracker in hope to figure out whether this is a bug, incomplete feature, or a mis-configuration from our side.

I have also tried the maven-publish plugin, but the produced maven-metadata.xml file is still the same.

0

There are 0 answers