前言
最近針對java項目的部署方式進(jìn)行整理,jenkins/tomcat/windows工具/linux腳本/web部署平臺等等
發(fā)現(xiàn)war包通過tomcat部署比較繁瑣,等待時間長,配置規(guī)則復(fù)雜對于小白很不友好,也難以接入到自定義的部署工具/平臺中
之前開發(fā)的Jar包部署平臺是servlet開發(fā)通過嵌入式tomcat部署,借此打開思路
能否基于嵌入式tomcat做一個war包啟動器,通過代碼的方式開啟tomcat容器來部署war包
源碼地址:https://gitee.com/code2roc/jar-manage/tree/master/waragent
借此啟動器可以將war包部署集成到自己的工具平臺中,將啟動器的jar包按普通方式部署即可
方案
tomcat啟動一般需要幾個基本參數(shù)設(shè)置
- war包路徑
- 端口
- 映射路由
Tomcat tomcat = new Tomcat();
tomcat.setPort(port);
StandardContext ctx = (StandardContext) tomcat.
addWebapp(contextPath, catalinaBase + File.separator + "webapps" + File.separator + name + ".war");
tomcat9啟動還需要指定cookie處理策略,否則無法識別
CookieProcessor cookieProcessor = new LegacyCookieProcessor();
ctx.setCookieProcessor(cookieProcessor);
后續(xù)實際使用中還涉及到了啟動jvm參數(shù)設(shè)置及jar包掃描跳過的配置
tomcat.getEngine().setJvmRoute(jvmStartCommand);
StandardJarScanner jarScanner = new StandardJarScanner();
StandardJarScanFilter jarScanFilter = new StandardJarScanFilter();
jarScanFilter.setTldSkip(skipScan);
jarScanFilter.setPluggabilitySkip(skipScan);
jarScanner.setJarScanFilter(jarScanFilter);
ctx.setJarScanner(jarScanner)
打包
原來預(yù)想把maven項目打到一個jar包方便調(diào)用,但是打包插件會把依賴jar包中的class文件進(jìn)行合并
嵌入式tomcat依賴的jar包有相同包名的,導(dǎo)致class文件覆蓋,websocket相關(guān)內(nèi)容報錯
所以把依賴jar包打入到同級lib文件夾中,和waranaget.jar一起拷貝使用
<build>
<finalName>${artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.code2roc.waragent.Application</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!-- 拷貝依賴的jar包到lib目錄 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
使用
java -jar waragent.jar "啟動參數(shù)"
啟動參數(shù)一定要用雙引號包含,這樣才能正確解析
定義:"$appname★$warFilePath★$port★$contextPath★$jvmParam★$skipScan"
示例:"testwar★D:\testwar.war★9091★/testwar★-Xms3072m -Xmx3072m -Djava.awt.headless=true★xxx*.jar"文章來源:http://www.zghlxwxcb.cn/news/detail-514795.html
內(nèi)部使用★分開,一共6個參數(shù),最后一個參數(shù)可省略,其余必填文章來源地址http://www.zghlxwxcb.cn/news/detail-514795.html
- 參數(shù)1:應(yīng)用名稱
- 參數(shù)2:war包絕對路徑
- 參數(shù)3:端口號
- 參數(shù)4:映射路由(contextPath)
- 參數(shù)5:jvm啟動參數(shù),主要指定內(nèi)存大小
- 參數(shù)6:啟動掃描跳過jar包名稱,多個使用,分開
到了這里,關(guān)于基于嵌入式Tomcat的War包啟動器的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!