25

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
        }
    }
}
Manfred Moser
  • 29,539
  • 13
  • 92
  • 123
gllambi
  • 648
  • 1
  • 7
  • 18

3 Answers3

10

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.

Sebi
  • 8,323
  • 6
  • 48
  • 76
6

You can use gradle-nexus-plugin

In order to use the plugin, add the lines below and import plugin

buildscript {
     repositories {
         mavenLocal()
         jcenter {
            url "http://jcenter.bintray.com/"
        }
     }
     dependencies {
         classpath 'com.bmuschko:gradle-nexus-plugin:2.3'
     }
 }

apply plugin: 'com.bmuschko.nexus'

Add this section, where you will configure url to deploy

nexus {
     sign = false
     repositoryUrl = 'http://localhost:8081/nexus/content/repositories/releases/'
     snapshotRepositoryUrl = 'http://localhost:8081/nexus/content/repositories/internal-snapshots/'
 }

Note: You must have in ~/.gradle/gradle.properties

nexusUsername = deployment
nexusPassword = deployment123
Eddú Meléndez
  • 6,107
  • 1
  • 26
  • 35
  • 4
    A good answer should include all the relevant information, rather than linking to external resources: "Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline." – Jolta Feb 20 '15 at 20:28
  • Sorry, but that's the same solution I've post. I was searching for something shorter. Maybe a configuration on the mavenDeployer. – gllambi Feb 21 '15 at 19:26
  • Updated. Simple like add the plugin and register your repository. – Eddú Meléndez Feb 27 '15 at 20:17
-6

Nexus for save artifact, not source code.

for upload compiled artifact I do:

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

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: "http://nexus-server:8081/nexus/content/repositories/snapshots") {
                authentication(userName: "admin", password: "secret")
            }
            pom.version = "1.0.0-SNAPSHOT"
            pom.artifactId = "${project.name}"
            pom.groupId = "path.to.artifact"
        }
    }
}

and call upload from console

$ gradle upload

for source code use maven or git repository

Konstantyn
  • 127
  • 1
  • 1
  • 7
  • 1
    I want to upload the sources of the artifact, not to version my source code – gllambi Jul 16 '15 at 02:29
  • 2
    This answer is highly wrong. It is a very handy feature to upload the source code and javadocs belonging to a artifact repository. Your IDE can download those artifacts to show you the source code of a library. Just try it with any third-party OpenSource library like Apache Commons. It is very hand that you can look into their source code without getting their complete repo. – Sebi Aug 17 '15 at 06:53