安装nexus插件Nexus Artifact Uploader:

image.png

image.png

image.png

screenshot-20241009-182555.png

使用命令上传:

# 配置文件中配置好nexus仓库的密码
vim /usr/local/apache-maven-3.9.9/conf/settings.xml

<servers>
    <server>
        <id>maven-hostd</id>
        <username>admin</username>
        <password>123456</password>
    </server>
</servers>


# groovy脚本
mvn = tool "M3"
sh "${mvn} -v"
sh "${mvn} ${buildShell}"
sh "pwd; ls -al"
sh "cd target; ls *.jar"
def jarName = sh returnStdout: true, script: "cd target; ls *.jar"
jarName = jarName - "\n"

def pom = readMavenPom file: 'pom.xml' // jenkins需要安装 Pipeline Utility Steps 插件
pomVersion = "${pom.version}"
pomArtifact = "${pom.artifactId}"
pomPackaging = "${pom.packaging}"
pomGroupId = "${pom.groupId}"

println("jarName:+${jarName}+pomVersion:+${pomVersion}+pomArtifact:+${pomArtifact}+pomPackaging:+${pomPackaging}+pomGroupId:+${pomGroupId}+++++++++")

sh """
	cd target/
	${mvn} deploy:deploy-file -Dmaven.test.skip=true\
		-Dfile=${jarName}\
		-DgroupId=${pomGroupId} \
		-DartifactId=${pomArtifact} \
		-Dversion=${pomVersion} \
		-Dpackaging=${pomPackaging} \
		-DrepositoryId=maven-hostd \
		-Durl=http://192.168.0.62:8081/repository/maven-hostd
"""

使用插件上传:

def jarName = sh returnStdout: true, script: "cd target; ls *.jar"
jarName = jarName - "\n"
def pom = readMavenPom file: 'pom.xml' // jenkins需要安装 Pipeline Utility Steps 插件
pomVersion = "${pom.version}"
pomArtifact = "${pom.artifactId}"
pomPackaging = "${pom.packaging}"
pomGroupId = "${pom.groupId}"

println("jarName:+${jarName}+pomVersion:+${pomVersion}+pomArtifact:+${pomArtifact}+pomPackaging:+${pomPackaging}+pomGroupId:+${pomGroupId}")

def repoName = "maven-hostd"
def filePath = "target/${jarName}"
nexusArtifactUploader artifacts: [[
	artifactId: "${pomArtifact}",
	classifier: '',
	file: "${filePath}",
	type: "${pomPackaging}"
	]],
credentialsId: '56e89133-3ed3-4d5a-a5c9-13c1bb343af3',
groupId: "${pomGroupId}",
nexusUrl: '192.168.0.62:8081',
nexusVersion: 'nexus3',
protocol: 'http',
repository: "${repoName}",
version: "${pomVersion}"

简单示例:

#!groovy

@Library('jenkinslibrary@main') _

def build = new org.devops.build()
def deploy = new org.devops.deploy()
def tools = new org.devops.tools()
def gitlab = new org.devops.gitlab()
def toemail = new org.devops.toemail()
def sonar = new org.devops.sonarqube()
def sonarapi = new org.devops.sonarapi()
def artifactory = new org.devops.artifactory()


def runOpts
//env
String buildType = "${env.buildType}"
String buildShell = "${env.buildShell}"
String deployHosts = "${env.deployHosts}"

String srcUrl = "${env.srcUrl}"
String branchName = "${env.branchName}"


pipeline {
    agent any

    stages {

        stage("GetCode"){
            steps{
                script{
                    tools.PrintMes(">>>>>>>>>>获取代码>>>>>>>>>>","green")
                    checkout([$class: 'GitSCM', branches: [[name: "${branchName}"]], 
                                      doGenerateSubmoduleConfigurations: false, 
                                      extensions: [], 
                                      submoduleCfg: [], 
                                      userRemoteConfigs: [[credentialsId: '9b5a7ba1-affb-480f-b4e3-4993e3b37841', url: "${srcUrl}"]]])
                }
            }
        }

        stage("maven and upload jar"){
            steps{
                script{
                    tools.PrintMes(">>>>>>>>>>Maven编译>>>>>>>>>>","green")
                    mvn = tool "M3"
                    sh "${mvn} -v"
                    sh "${mvn} ${buildShell}"
                    sh "pwd; ls -al"
                    sh "cd target; ls *.jar"
                    // def jarName = sh returnStdout: true, script: "cd target; ls *.jar"
                    // jarName = jarName - "\n"
                    def jarName = "hgb-utils-0.3.jar"

                    // jenkins需要安装 Pipeline Utility Steps 插件
                    def pom = readMavenPom file: 'pom.xml'
                    pomVersion = "${pom.version}"
                    pomArtifact = "${pom.artifactId}"
                    pomPackaging = "${pom.packaging}"
                    pomGroupId = "${pom.groupId}"

                    println("jarName:+${jarName}+pomVersion:+${pomVersion}+pomArtifact:+${pomArtifact}+pomPackaging:+${pomPackaging}+pomGroupId:+${pomGroupId}")

                    // sh """
                    //     cd target/
                    //     ${mvn} deploy:deploy-file -Dmaven.test.skip=true\
                    //         -Dfile=${jarName}\
                    //         -DgroupId=${pomGroupId} \
                    //         -DartifactId=${pomArtifact} \
                    //         -Dversion=${pomVersion} \
                    //         -Dpackaging=${pomPackaging} \
                    //         -DrepositoryId=maven-hostd \
                    //         -Durl=http://192.168.0.62:8081/repository/maven-hostd
                    // """

                    def repoName = "maven-hostd"
                    def filePath = "target/${jarName}"
                    nexusArtifactUploader artifacts: [[
                        artifactId: "${pomArtifact}", 
                        classifier: '', 
                        file: "${filePath}", 
                        type: "${pomPackaging}"
                        ]], 
                    credentialsId: '56e89133-3ed3-4d5a-a5c9-13c1bb343af3', 
                    groupId: "${pomGroupId}", 
                    nexusUrl: '192.168.0.62:8081', 
                    nexusVersion: 'nexus3', 
                    protocol: 'http', 
                    repository: "${repoName}", 
                    version: "${pomVersion}"
                }
            }
        }
    }
}