在jenkins中安装插件:

Nexus Artifact Uploader

在流水线语法中:复制语句到pipeline中。

image.png

groovy脚本使用jenkins插件上传制品到Nexus:

#!groovy

@Library('jenkinslib@master') _

def tools = new org.devops.tools();

pipeline{
	agent{
		node{
			label "master"
		}
	}
	
	stages{
		stage("CheckOut"){
			steps{
				script{
					tools.PrintMes("获取代码","green")
					
					checkout([
						$class: 'GitSCM', branches: [[name: '*/master']], 
						doGenerateSubmoduleConfigurations: false, extensions: [], 
						submoduleCfg: [], 
						userRemoteConfigs: [
							[
								credentialsId: '398170a9-72a6-410b-8293-334687f152da', 
								url: 'http://192.168.0.26/qiyang/solo.git'
							]
						]
					])
				}
			}
		}
		
		stage("Build"){
			steps{
				script{
					tools.PrintMes("编译","green")
					
					mvnHome = tool "M2"
					sh "${mvnHome}/bin/mvn clean package -Dmaven.test.skip=true"
				}
			}
		}
		
		stage("NexusUpload"){
			steps{
				script{
					tools.PrintMes("NexusUpload","green")
					
					def jarName = sh returnStdout: true, script: "cd target;ls *.war"
					env.jarName = jarName - "\n"
					def pom = readMavenPom file: 'pom.xml'
					env.pomVersion = "${pom.version}"
					env.pomArtifact = "${pom.artifactId}"
					env.pomPackaging = "${pom.packaging}"
					env.pomGroupId = "${pom.groupId}"
					
					tools.PrintMes("--------------------","green")
					println("${env.jarName}-${pomGroupId}-${pomArtifact}-${pomVersion}-${pomPackaging}")
					
					nexusArtifactUploader artifacts: [[
						artifactId: "${pomArtifact}", 
						classifier: '', 
						file: "target/${env.jarName}", 
						type: "${pomPackaging}"
					]],
					credentialsId: '048ecb1f-89b4-40bb-b92a-feea291ce8db',
					groupId: "${pomGroupId}", 
					nexusUrl: '192.168.0.26:8081', 
					nexusVersion: 'nexus3', 
					protocol: 'http', 
					repository: "maven-releases", 
					version: "${pomVersion}"
				}
			}
		}
	}
	
}

使用maven上传插件:

www.scriptjc.com/article/1196