构建可执行war包文档(tomcat7-maven-plugin):
- JDK 1.7+
- Maven 3.0 or later
- tomcat7-maven-plugin version 2.1/2.0/2.0-beta-1
0、环境要求
- Unix System\windwos\macOS
- JDK 1.7+
- Maven 3.0 or later
- 我测试的编译环境是:
- macOS10.11.6和windows10
- jdk1.8/jdk1.7
- Maven 3.3.9
- 我的本机开发环境:
- MacOS 10.11.6
- eclipse Mars.2
1、在pom中配置tomcat7-maven-plugin
<project> ... <packaging>war</packaging> ... <build> ... <plugins> ... <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <executions> <execution> <id>tomcat-run</id> <goals> <goal>exec-war-only</goal> </goals> <phase>package</phase> <configuration> <path>demo</path> <!-- optional, needed only if you want to use a preconfigured server.xml file --> <serverXml>src/main/tomcatconf/server.xml</serverXml> <!-- optional values which can be configurable --> <attachArtifactClassifier>default value is exec-war but you can customize</attachArtifactClassifier> <attachArtifactClassifierType>default value is jar</attachArtifactClassifierType> </configuration> </execution> </executions> </plugin> ... </plugins> ... </build> ... </project>
2、执行构建生成jar包
执行man clean install
会在target下生成相应的jar包
如果想要修改jar包名称,可以在
<
configuration
>标签中定义<finalName>标签修改生成的jar包名称
执行构建出来的jar包:
java -jar yourjar
其他参数:
usage: java -jar [path to your exec war jar] -ajpPort <ajpPort> ajp port to use -clientAuth enable client authentication for https -D <arg> key=value -extractDirectory <extractDirectory> path to extract war content, default value: .extract -h,--help help -httpPort <httpPort> http port to use -httpProtocol <httpProtocol> http protocol to use: HTTP/1.1 or org.apache.coyote.http11.Http11Nio Protocol -httpsPort <httpsPort> https port to use -keyAlias <keyAlias> alias from keystore for ssl -loggerName <loggerName> logger to use: slf4j to use slf4j bridge on top of jul -obfuscate <password> obfuscate the password and exit -resetExtract clean previous extract directory -serverXmlPath <serverXmlPath> server.xml to use, optional -X,--debug debug
例如:如果需要改变启动容器的端口号(默认为8080)
java -jar yourjar -httpPort 8081
PS: 在测试过程中使用到的插件版本如下:
2.0、2.0-beta-1、2.1、2.2
都可以正常构建生成jar包,但是在执行启动jar包的过程中,在jdk1.8的环境下:
- 2.1的版本正常,可以正常执行部署,启动运行
- 2.0和2.0-beta-1版本可以正常部署,但是启动tomcat访问中报java.io异常,和jdk执行版本有关,经过多次测试,更换为jdk1.7执行没有可以正常使用。
- 在jdk1.8环境和jdk1.7环境下2.2版本都存在不可以正常创建webapp文件夹,也不可以将应用部署到webapp中,如果手动将jar包的相关内容部署进.extract/再次执行启动命令可以正常使用
综上:可以使用2.1版本的插件。