首页 > 解决方案 > 如何将 jar 发布到 NEXUS 存储库

问题描述

我想从我的 Gradle 项目中构建一个 jar 并将其推送到 nexus 存储库。作为其中的一部分,我创建了一个 Jenkins 文件并在 build.gradle 中添加了一个任务“发布”任务。

我的詹金斯文件:

pipeline {
    agent any
        environment {
        NEXUS = credentials('nexus-user')
    }   
    options {
        ansiColor('xterm')
        buildDiscarder logRotator(daysToKeepStr: '30', numToKeepStr: '100')
    }
    triggers { pollSCM('H/5 * * * *') }
    stages {
        stage('Checkout'){
            steps { checkout scm }
        }
        stage('Build') {
            steps { sh "./gradlew assemble" }
        }
        
        stage('deploy') {
             steps {                     
                 sh "gradle -Duser.home=\"$WORKSPACE\" --gradle-user-home=\"$WORKSPACE/.gradle\" -PnexusUsername=$NEXUS_USR -PnexusPassword=$NEXUS_PSW publish"
                 
            }
        }   
        
    } 
}

和 build.gradle

publishing {
    publications{
        maven(MavenPublication){
            artifactId = "testApp"
            from components.java
        }
    }
    repositories {
        maven {
            url = "http://localhost:8081/nexus/content/repositories/${version.endsWith('-SNAPSHOT') ? "snapshots" : "releases"}"
            credentials {
                username = "Dont know how to pass the username here" 
                password = "Dont know how to pass the password here"
            }
        }
    }
}

谁能告诉我如何从 Gradle 获取用户名和密码并在此处设置以将 jar 发布到 nexus。

标签: gradlejenkins-pipelinebuild.gradle

解决方案


答案是:

publishing {
    publications{
        maven(MavenPublication){
            artifactId = "testApp"
            from components.java
        }
    }
    repositories {
        maven {
            url = "http://localhost:8081/nexus/content/repositories/${version.endsWith('-SNAPSHOT') ? "snapshots" : "releases"}"
            credentials {
                username = "nexusUsername" 
                password = "nexusPassword"
            }
        }
    }
}

推荐阅读