Nexus制品详解
1.docker安装nexus
1)下载安装
# docker pull sonatype/nexus3 # mkdir -p /opt/nexus/data # chmod 777 -R /opt/nexus/data # docker run -d --name nexus3 -p 8081:8081 --restart always -v /opt/nexus/data:/nexus-data sonatype/nexus3
2)访问验证
访问管理平台:http://ip:8081
默认用户名admin,初始密码在cat /opt/nexus/data/admin.password
2.Nexus介绍
1)默认仓库说明
maven-central:maven中央库,默认从https://repo1.maven.org/maven2/拉取jar
maven-releases:私库发行版jar,初次安装请将Deployment policy设置为Allow redeploy
maven-snapshots:私库快照(调试版本)jar
maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地maven基础配置settings.xml或项目pom.xml中使用
2)Nexus仓库类型介绍
hosted:本地仓库,通常我们会部署自己的构件到这一类型的仓库。比如公司的第二方库。
proxy:代理仓库,它们被用来代理远程的公共仓库,如maven中央仓库。
group:仓库组,用来合并多个hosted/proxy仓库,当你的项目希望在多个repository使用资源时就不需要多次引用了,只需要引用一个group即可。
3.上传制品
有两种上传方式:mvn deploy 和 nexus artifact uploader插件,本文讲解插件上传制品
1)安装nexus artifact uploader插件
2)创建nexus凭证
3)创建maven-hostd仓库
4)nexus.groovy
包含maven和插件上传
pakcage org.devops //获取POM中的坐标 def GetGav(){ //上传制品 def jarName = sh returnStdout: true, script: "cd target;ls *.jar" 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}" println("${pomGroupId}-${pomArtifact}-${pomVersion}-${pomPackaging}") return ["${pomGroupId}","${pomArtifact}","${pomVersion}","${pomPackaging}"] } //Nexus plugin deploy def NexusUpload(){ //use nexus plugin nexusArtifactUploader artifacts: [[artifactId: "${pomArtifact}", classifier: '', file: "${filePath}", type: "${pomPackaging}"]], credentialsId: 'nexus-admin-user', groupId: "${pomGroupId}", nexusUrl: '192.168.164.158:8081', nexusVersion: 'nexus3', protocol: 'http', repository: "${repoName}", version: "${pomVersion}" } //mvn deploy def MavenUpload(){ def mvnHome = tool "M2" sh """ cd target/ ${mvnHome}/bin/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.164.158.8081/repository/maven-hostd """ } def main(uploadType){ GetGav() if ("${uploadType}" == "maven"){ MavenUpload() } else if ("${uploadType}" == "nexus") { env.repoName = "maven-hostd" env.filePath = "target/${jarName}" NexusUpload() } }
5) jenkinsfile调用
stage("Upload Nexus"){ steps{ script{ //上传制品 tools.PrintMes("上传制品","green") nexus.main("nexus") } } }
6)构建测试
4.发布制品
1)安装Maven Artifact ChoiceListProvider插件
2)配置选择制品
3) 测试验证
在CD发布阶段,选择指定制品进行发布
5.制品晋级
1)创建maven-release仓库
2)创建晋级策略
3)nexus.groovy制品晋级
//制品晋级 def ArtifactUpdate(updateType,artifactUrl){ //晋级策略 if ("${updateType}" == "snapshot -> release"){ println("snapshot -> release") //下载原始制品 sh " rm -fr updates && mkdir updates && cd updates && wget ${artifactUrl} && ls -l " //获取artifactID artifactUrl = artifactUrl - "http://192.168.164.158.8081/repository/maven-hostd/" artifactUrl = artifactUrl.split("/").toList() println(artifactUrl.size()) env.jarName = artifactUrl[-1] env.pomVersion = artifactUrl[-2].replace("SNAPSHOT","RELEASE") env.pomArtifact = artifactUrl[-3] pomPackaging = artifactUrl[-1] pomPackaging = pomPackaging.split("\\.").toList()[-1] env.pomPackaging = pomPackaging[-1] env.pomGroupId = artifactUrl[0..-4].join(".") println("${pomGroupId}##${pomArtifact}##${pomVersion}##${pomPackaging}") env.newJarName = "${pomArtifact}-${pomVersion}.${pomPackaging}" //更改名称 sh " cd updates && mv ${jarName} ${newJarName} " //上传制品 env.repoName = "maven-release" env.filePath = "updates/${newJarName}" NexusUpload() } }
4)jenkinsfile调用
stage("Upload Nexus"){ steps{ script{ //上传制品 tools.PrintMes("上传制品","green") nexus.main("nexus") //制品晋级 tools.PrintMes("制品晋级","green") nexus.ArtifactUpdate(updateType,artifactUrl) } } }
5) 构建检查
作者:杰宏唯一
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.