最近针对java项目的部署方式进行整理,jenkins/tomcat/windows工具/linux脚本/web部署平台等等
发现war包通过tomcat部署比较繁琐,等待时间长,配置规则复杂对于小白很不友好,也难以接入到自定义的部署工具/平台中
之前开发的Jar包部署平台是servlet开发通过嵌入式tomcat部署,借此打开思路
能否基于嵌入式tomcat做一个war包启动器,通过代码的方式开启tomcat容器来部署war包
源码地址:https://gitee.com/code2roc/jar-manage/tree/master/waragent
借此启动器可以将war包部署集成到自己的工具平台中,将启动器的jar包按普通方式部署即可
tomcat启动一般需要几个基本参数设置
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);
后续实际使用中还涉及到了启动jvm参数设置及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)
原来预想把maven项目打到一个jar包方便调用,但是打包插件会把依赖jar包中的class文件进行合并
嵌入式tomcat依赖的jar包有相同包名的,导致class文件覆盖,websocket相关内容报错
所以把依赖jar包打入到同级lib文件夹中,和waranaget.jar一起拷贝使用
<build>
<finalName>${artifactId}finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-jar-pluginartifactId>
<configuration>
<archive>
<manifest>
<addClasspath>trueaddClasspath>
<classpathPrefix>lib/classpathPrefix>
<mainClass>com.code2roc.waragent.ApplicationmainClass>
manifest>
archive>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-dependency-pluginartifactId>
<executions>
<execution>
<id>copyid>
<phase>packagephase>
<goals>
<goal>copy-dependenciesgoal>
goals>
<configuration>
<outputDirectory>
${project.build.directory}/lib
outputDirectory>
configuration>
execution>
executions>
plugin>
plugins>
build>
java -jar waragent.jar “启动参数”
启动参数一定要用双引号包含,这样才能正确解析
定义:“ a p p n a m e ★ appname★ appname★warFilePath★ p o r t ★ port★ port★contextPath★ j v m P a r a m ★ jvmParam★ jvmParam★skipScan”
示例:“testwar★D:\testwar.war★9091★/testwar★-Xms3072m -Xmx3072m -Djava.awt.headless=true★xxx*.jar”
内部使用★分开,一共6个参数,最后一个参数可省略,其余必填
我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=foa7rm2zkofi