減小war包體積
阿新 • • 發佈:2020-07-15
https://www.cnblogs.com/manastudent/p/12264408.html
現在使用 Jenkins 自動化部署專案有這樣一個問題,那就是專案使用到的 jar 包比較多,這就導致每次將 war 包上傳到伺服器上的時間就比較長。今天無意間看到一個方法可以大幅減少 war 包大小,提升效率。
方法很簡單就是將第三方 jar 包直接放到 tomcat 的 lib 目錄下,而不是放在專案的 lib 資料夾下,這樣就可以讓一個 war 包顯著的縮小體積,並且不影響專案本身。
在tomcat_home/lib
下新建自定義jar包檔案,比如jsdoctor
,匯入專案所需其他jar包(以後有新增的話,單獨再導一次)
tomcat_home/conf/catalina.properties
在shared.loader
後面加上${catalina.base}/lib/jsdoctor/*.jar
下面是一段我自己編寫的 shell 指令碼,目的是去除 war 包中的第三方 jar 包並且保留專案自己模組的 jar。
# 將 core 和 common 包移動到臨時位置
mv /root/.jenkins/workspace/jsdoctor/jsdoctor-platform-admin/target/jsdoctor-platform-admin-1.0-SNAPSHOT/WEB-INF/lib/jsdoctor-common-1.0-SNAPSHOT.jar /root/.jenkins/workspace/jsdoctor/jsdoctor-platform-admin/target
mv /root/.jenkins/workspace/jsdoctor/jsdoctor-platform-admin/target/jsdoctor-platform-admin-1.0-SNAPSHOT/WEB-INF/lib/jsdoctor-core-1.0-SNAPSHOT.jar /root/.jenkins/workspace/jsdoctor/jsdoctor-platform-admin/target
# 刪除 lib 下所有檔案
rm -rf /root/.jenkins/workspace/jsdoctor/jsdoctor-platform-admin/target/jsdoctor-platform-admin-1.0-SNAPSHOT/WEB-INF/lib/*
# 刪除生成的 war 包
rm -rf /root/.jenkins/workspace/jsdoctor/jsdoctor-platform-admin/target/jsdoctor-platform-admin.war
# 將 core 和 common 包移動回來
mv /root/.jenkins/workspace/jsdoctor/jsdoctor-platform-admin/target/jsdoctor-common-1.0-SNAPSHOT.jar /root/.jenkins/workspace/jsdoctor/jsdoctor-platform-admin/target/jsdoctor-platform-admin-1.0-SNAPSHOT/WEB-INF/lib/
mv /root/.jenkins/workspace/jsdoctor/jsdoctor-platform-admin/target/jsdoctor-core-1.0-SNAPSHOT.jar /root/.jenkins/workspace/jsdoctor/jsdoctor-platform-admin/target/jsdoctor-platform-admin-1.0-SNAPSHOT/WEB-INF/lib/
# 打包
cd /root/.jenkins/workspace/jsdoctor/jsdoctor-platform-admin/target/jsdoctor-platform-admin-1.0-SNAPSHOT
zip -q -r jsdoctor-platform-admin.war ./*
mv jsdoctor-platform-admin.war /root/.jenkins/workspace/jsdoctor/jsdoctor-platform-admin/target