简体   繁体   中英

Upload sources to nexus repository with gradle

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
        }
    }
}

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM