背景
- 日常工作中,deploy时需要发布SNAPSHOT快照包,带-SNAPSHOT后缀
- 上线时,需要发布正式包,不带-SNAPSHOT后缀
经常需要手动加上-SNAPSHOT后缀后deploy,上线还要再改回来,麻烦又容易出错
需求
- api模块deploy时能够鉴别SNAPSHOT版本,如没有-SNAPSHOT后缀则自动加上
环境
- system: macOS 10.12.5
- maven version: 3.5.2
- Java version: 1.8.0_131
- ide: IDEA 2018.1
脚本执行流程
- package打包
- 获取pom文件中的version节点
- 判断version是否符合快照包格式,不是则加上-SNAOSHOT后缀
- 通过versions-maven-plugin工具替换version (自动下载,无需额外配置)
- 发布到远程仓库
- 回滚第四步
完整脚本(需要替换xxx为自己的项目名称)
# API自动打包包,自动加上-SNAOSHOT后缀
#!/bin/bash
echo "packaging..."
mvn clean package -pl xxx-api -Dmaven.test.skip=true >/dev/null 2>&1
if [ $? -ne 0 ];
then
echo "package error!!!!!!!!!!!!!!"
exit
fi
echo "package success"
echo "updating version..."
pomVersion=$(grep version ./xxx-api/target/maven-archiver/pom.properties | awk -F [\=] '{print $2}')
if [[ ${pomVersion} =~ ^.*-SNAPSHOT$ ]]
then
echo "POM VERSION IS SNAPSHOT!"
else
pomVersion=${pomVersion}"-SNAPSHOT"
fi
mvn org.codehaus.mojo:versions-maven-plugin:2.5:set -DgroupId=cn.com.ykse.xxx -DartifactId=xxx-api -DnewVersion=${pomVersion} > /dev/null
echo "update version success"
echo "deploying..."
mvn deploy -pl xxx-api > /dev/null
echo "deploy success!"
echo "reverting version change..."
mvn org.codehaus.mojo:versions-maven-plugin:2.5:revert > /dev/null
echo "revert version change success!"
echo "deploy job success! deploy version is "${pomVersion}
网友评论