Upload sources to nexus repository with gradle

gllambi picture gllambi · Feb 20, 2015 · Viewed 23.8k times · Source

I successfully uploaded my jars to a nexus repository using the maven plugin for gradle but it didn't upload the sources. This is my configuration:

uploadArchives {
    repositories{
        mavenDeployer {
            repository(url: "http://...") {
                 authentication(userName: "user", password: "myPassword")
            }
        }
    }
}

I searched and found that I can add the sources by adding a new task.

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

artifacts {
     archives sourcesJar
}

This works fine but I think there must be a better solution by configuring the maven plugin, something like uploadSource = true like this:

uploadArchives {
    repositories{
        mavenDeployer {
            repository(url: "http://...") {
                 authentication(userName: "user", password: "myPassword")
            }
            uploadSources = true
        }
    }
}

Answer

Sebi picture Sebi · Aug 17, 2015

There is no better solution than what you described yourself. The gradle maven plugin is uploading all artifacts generated in the current project. That's why you have to explicitly create a "sources" artifact.

The situation also doesn't change when using the new maven-publish plugin. Here, you also need to explicitly define additional artifacts:

task sourceJar(type: Jar) {
    from sourceSets.main.allJava
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java

            artifact sourceJar {
                classifier "sources"
            }
        }
    }
}

The reason is that gradle is more about being a general build tool and not bound to pure Java projects.