使用Maven发布web项目,一般使用JETTY容器,因为tomcat不能热部署,必须打包然后发布,增加工作量,而jetty可以热部署。
1、Maven插件安装
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.7.v20160115</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webApp>
<contextPath>/account</contextPath>
</webApp>
<httpConnector>
<port>8787</port>
</httpConnector>
</configuration>
</plugin>
然后重新编译一下,就可以使用jetty插件了。使用mvn jetty:run就可以运行maven项目了。
注意:如果是聚合形式项目,jetty一般配置再parent pom pluginManagement 中,子类web项目需要声明继承关系,另外 因为子类继承parent jetty的配置,如有更改建议重新编译打包,未避免打包,可以将具体configuration放到子pom中,只重新编译子项目即可。
scanIntervalSeconds。扫描进行热部署的间隔时间。
jetty插件的运行
jetty:run
jetty:run命令会直接使用源代码运行web程序,不将其打包成war文件。jetty插件会保证在运行前所有的类和资源都是最新的。如果你修改了代码,插件会自动重新部署。
The run goal runs on a webapp that does not have to be built into a WAR. Instead, Jetty deploys the webapp from its sources. It looks for the constituent parts of a webapp in the Maven default project locations, although you can override these in the plugin configuration. For example, by default it looks for: resources in ${project.basedir}/src/main/webapp classes in ${project.build.outputDirectory} web.xml in ${project.basedir}/src/main/webapp/WEB-INF/ The plugin automatically ensures the classes are rebuilt and up-to-date before deployment. If you change the source of a class and your IDE automatically compiles it in the background, the plugin picks up the changed class.
jetty:run-war
run-war会先将程序打包成war文件然后再将其部署。如果有代码修改,会重新打包war文件并部署。