第一种方式:使用jetty-maven-plugin插件部署web应用
首先 配置maven的conf目录下的settings.xml文件
在其中配置:
<pluginGroups>
<pluginGroup>org.mortbay.jetty</pluginGroup>
</pluginGroups>
因为此插件不是官方插件,如果不配置,默认是搜"org.apache.maven.plugins" and "org.codehaus.mojo",所以配置此插件的groupId,这样就可以使用简化的前缀去调用了,例如mvn jetty:run
pom.xml文件中配置:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>7.1.6.v20100715</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webAppConfig>
<contextPath>/test</contextPath>
</webAppConfig>
</configuration>
</plugin>
配置完成后 ,通过命令:mvn jetty:run 就可以启动应用了,scanIntervalSeconds配置每10miao扫描一次,如果修改了jsp html css js java类,插件都会自动更新修改内容到应用中去。
默认端口8080,如果需要更改端口可以使用命令mvn jetty:run -Djetty.port =8099
启动成功后就可以通过 http://localhost:8099/test 访问应用了。
第二种部署方法
通过cargo.maven2.plugin插件实现自动化部署
部署到本地:
配置pom.xml
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.1.3</version>
<configuration>
<!-- Container configuration -->
<container>
<containerId>tomcat7x</containerId>
<home>F:\apache-tomcat-7.0.65</home>
</container>
<configuration>
<type>standalone</type>
<home>${project.build.directory}/tomcat7x</home>
<properties>
<cargo.host>localhost</cargo.host>
<cargo.servlet.port>8080</cargo.servlet.port>
</properties>
</configuration>
</configuration>
</plugin>
配置settings.xml
跟第一种是一样的道理,增加
<pluginGroups>
<pluginGroup>org.codehaus.cargo</pluginGroup>
</pluginGroups>
现在可以通过命令启动了
mvn clean package cargo:run
这种部署方式关键是在
<configuration>
<type>standalone</type>
<home>${project.build.directory}/tomcat7x</home>
<properties>
<cargo.host>localhost</cargo.host>
<cargo.servlet.port>8080</cargo.servlet.port>
</properties>
</configuration>
配置成standalone, 会将tomcat拷贝至${project.build.directory} 即target的tomcat7x目录下 可以修改成existing 并将home修改为tomcat的目录 跟<container>中配置的一样F:\apache-tomcat-7.0.65 ,这种方式就会直接在tomcat目下部署,而不会拷贝 详细配置见下屏
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.1.3</version>
<configuration>
<!-- Container configuration -->
<container>
<containerId>tomcat7x</containerId>
<home>F:\apache-tomcat-7.0.65</home>
</container>
<configuration>
<type>existing</type>
<home>F:\apache-tomcat-7.0.65</home>
</configuration>
</configuration>
</plugin>
注意:启动命令run可以用start, 如 mvn clean package cargo:start
只不过此时你会看到编译成功会失败BUILD success 或者build fail ,却不能访问页面进行测试,测试的话还得用run。
上述命令中的clean是清除上次的部署,package是打成war包 ,如果直接这样mvn cargo:run 会报
Failed to execute goal org.codehaus.cargo:cargo-maven2-plugin:1.1.3:run (default-cli) on project jettyplugin: Execution default-cli of goal org.codehaus.cargo:cargo-maven2-plugin:1.1.3:run failed: Failed to create deployable with implementation class org.codehaus.cargo.container.tomcat.TomcatWAR for the parameters (container [id = [tomcat7x]], deployable type [war]). InvocationTargetException: Failed to parse Tomcat WAR file in [F:\workSpace\jettyplugin\target\jettyplugin.war]: Failed to find file [F:\workSpace\jettyplugin\target\jettyplugin.war]: F:\workSpace\jettyplugin\target\jettyplugin.war (系统找不到指定的文件。) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
所以 在命令中加入了打包的命令package ,cargo官网上是用的verify ,都是default声明周期中的一个阶段,命令如下
mvn clean verify org.codehaus.cargo:cargo-maven2-plugin:run
//CARGO can be directly run on any existing Maven2 Java EE project (WAR, EAR or other) by running: 此命令就无需配置pom.xml和settings.xml了