使用maven-wagon插件實作自動化部署
wagon-maven-plugin
在專案開發程序中,經常需要部署代碼到開發環境,每天可能有好多次,每次都需要mvn clean install & package,然后上傳到服務器,實際上這些繁瑣的步驟可以通過一個Maven插件wagon-maven-plugin來自動完成
使用步驟
1.匯入依賴
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>wagon-maven-plugin</artifactId>
<version>1.0</version>
</dependency>
2.在打包專案pom檔案buid中配置
<build>
<finalName>ka-admin</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>wagon-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<!-- 需要部署的檔案 : target下生成的包 -->
<fromFile>target/${pack-name}.jar</fromFile>
<!-- 服務器中上傳包部署目錄 -->
<url>scp://服務器賬號:密碼@服務器IP/其它路徑</url>
<!-- 服務器執行的命令集合 -->
<commands>
<!-- 上傳后執行服務器腳本 可以直接寫指令,也可以寫要運行得腳本路徑 -->
<command>sudo sh /data/deploy/ka-admin/deploy.sh</command>
</commands>
<displayCommandOutputs>true</displayCommandOutputs>
</configuration>
</plugin>
</plugins>
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh</artifactId>
<version>2.10</version>
</extension>
</extensions>
</build>
其中
1.<formFile>為target下的jar包 全路徑名稱 如 target/ka-admin.jar
2.<url>為包上傳路徑 格式為 scp://服務器賬號:密碼@服務器IP/其它路徑
3.<commands>為上傳完成后需要在服務器執行的指令,可以直接寫指令,也可以寫在服務器中需要執行的shell腳本,一個<command>為一條指令,可以配置多條依次執行
其它配置固定
執行服務器腳本
示例
# 步驟
# 1、把舊包備份到bak目錄
# 2、新包移動到作業區
# 3、殺死行程
# 4、啟動服務
# 指定啟動環境 以 java為例
activeProfile="test"
fileName="ka-admin"
logName="cataline.log"
#專案根路徑 作業區
basePath="/data/deploy/${fileName}"
#包上傳路徑
uploadPath="${basePath}/data/upload"
#備份包路徑
bakPath="${basePath}/data/bak"
baseJar="${basePath}/${fileName}.jar"
uploadJar="${uploadPath}/${fileName}.jar"
bakJar="${bakPath}/${fileName}.jar"
# 判斷備份路徑是否存在,不存在創建目錄
if [ ! -d "$uploadPath" ]; then
echo "mkdir ${uploadPath}"
mkdir -p "${uploadPath}"
fi
if [ -d "$baseJar" ]; then
mv -f ${baseJar} ${bakJar}
fi
# 移動上傳包到作業區
if [ -d "$uploadJar" ]; then
mv -f ${uploadJar} ${baseJar}
fi
# 查看啟動行程
pid=`ps -ef|grep ${fileName}.jar | grep -v grep | awk '{print $2}'`
echo "舊行程id:${pid}"
echo "e"
# 殺死行程
kill -9 $pid
# 重啟行程
if [ -e "$baseJar" ]; then
echo "開始啟動此程式..."
source /etc/profile nohup java -jar ${fileName}.jar > ${logName} 2>&1 &
tail -f ${logName}
pIdNew=`ps -ef|grep ${fileName}.jar | grep -v grep | awk '{print $2}'`
echo "${fileName} 啟動成功,pil:${pIdNew}..."
else
echo "${fileName}.jar 不存在,請檢查..."
fi
執行打包上傳命令
mvn clean package wagon:upload-single wagon:sshexec
注意
專案部署后可能會無法啟動,我碰到過是找不到主類
解決辦法:
在buidl中maven打包插件中加入配置指定主類
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.androidserver.server.Main(主類全路徑)</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
windows復制腳本到linux不能執行,需要修改格式為unix
修改腳本格式命令:
vim xxx.sh
后
:set ff=unix
學習記錄,可能會有錯誤,歡迎指正
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/179306.html
標籤:其他
上一篇:(1)linux入門必會命令
下一篇:防火墻簡單實驗
