Jetty和Tomcat运行Maven Web项目几种方法

来源:互联网 发布:故障报修系统asp源码 编辑:程序博客网 时间:2024/04/28 13:48

  目录

   1.简介

   2.采用JettyTomcat运行Maven Web项目

  

 1.简介

    上一篇我们介绍了用Eclipse创建了Maven Web项目,接下来我们介绍怎么运行Maven Web项目方便我们开发和调试,这里使用的是Maven插件的形式引用进来的,这样耦合性比较低,我们需要什么样的工具就引入对应的插件就可以用。


 2.采用JettyTomcat运行Maven Web项目

     1)采用Jetty运行Maven Web项目

      第一步:安装Jetty插件到Eclipse上,首先Eclipse help->install new software 中 Location : http://run-jetty-run.googlecode.com/svn/trunk/updatesite/ 

把选项勾上,然后等待它  下载安装,完成之后重启 eclipse 即可。

    


 第二步:Maven项目POM.XMl  添加Jetty的插件jetty-maven-plugin,我们要运行test-maven-console项目,所以要在这个项目POM.XML中添加

         

<plugins>  <plugin>        <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-compiler-plugin</artifactId>            <configuration>            <source>${version.jdk}</source>                <target>${version.jdk}</target>                <showWarnings>true</showWarnings><compilerArguments><verbose /><bootclasspath>${java.home}\lib\rt.jar;${java.home}\lib\jce.jar</bootclasspath></compilerArguments>                            </configuration>     </plugin>           <plugin><!-- clean  -Djetty.port=9090 jetty:run -->          <groupId>org.mortbay.jetty</groupId>          <artifactId>jetty-maven-plugin</artifactId>          <configuration>            <reload>automatic</reload>            <scanIntervalSeconds>10</scanIntervalSeconds>            <systemProperties>            </systemProperties>            <useTestClasspath>true</useTestClasspath>            <webAppConfig>              <contextPath>/</contextPath>            </webAppConfig>          </configuration>        </plugin><pre name="code" class="java" style="font-size: 18px;"></plugins>

说明:  maven-compiler-plugin这个插件是编译插件

     第三步:配置运行端口等。点击test-maven-console项目右击Run As -->Run Configurations  然后点击Jetty Webapp右击New 配置Jetty版本和Port端口(

不至于端口冲突

      

点击RUN运行看后台Console有没有报错,如果没报错就代表运行成功

   

我们也可以以DUG形式启动,方便我们对项目进行跟踪

  

2.采用Tomcat运行Maven Web项目

  第一步:Eclipse这边不需要引入Tomcat插件,直接在要运行的项目中引入Tomcat插件就可以了,test-maven-console项目添加Tomcat的插件tomcat6-maven-plugin (到2.0版本tomcat-maven-plugin现在已拆分成tomcat7-maven-plugintomcat6-maven-plugin了,而groupId也由org.codehaus.mojo改为org.apache.tomcat.maven。)我们这边使用的是tomcat6-maven-plugin 

    test-maven-console的POM.XML中加入

      

    <!-- tomcat运行 clean tomcat6:run -->        <plugin>        <groupId>org.apache.tomcat.maven</groupId>          <artifactId>tomcat6-maven-plugin</artifactId>          <version>2.2</version>          <configuration><!-- http port -->      <port>9090</port>      <!-- application path always starts with /-->      <path>/</path>      <uriEncoding>UTF-8</uriEncoding>      <systemProperties>              <java.io.tmpdir>${project.build.directory}</java.io.tmpdir>                 </systemProperties>      </configuration>           </plugin>              <plugin><!-- clean cargo:redeploy -->          <groupId>org.codehaus.cargo</groupId>          <artifactId>cargo-maven2-plugin</artifactId>          <configuration>            <container>              <containerId>tomcat6x</containerId>              <type>remote</type>            </container>            <configuration>              <type>runtime</type>            </configuration>          </configuration>        </plugin>

      说明:

     1.port端口号

      2.path以/ 这样在访问时,就不用加入项目名

 第三步:配置运行,点击test-maven-console项目Run As-->Run Configurations   然后点击Maven Build右击New 在Goals 输入tomcat6:run   然后点击run运行,查看Console有没有报错。

    




这时后台没报错,说明能正常运行

  

Maven 提供的插件很多

   

                    <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><version>2.6</version></plugin><!-- install插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-install-plugin</artifactId><version>2.4</version></plugin><!-- clean插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-clean-plugin</artifactId><version>2.5</version></plugin>

   

比较常用到的插件


 总结一下:

     运行Maven Web 项目比较常用的是这两种运行,Tomcat运行Maven Web 项目还可以采用另外一种,我们平常比较少用,这里就不具体详细的介绍,

采用的是tomcat:redeploy命令,把Maven Web 项目发布到外部已启动的Tomcat进行测试

  在使用Maven 引入JAR 包时,有时会报找不到,这里我在maven使用过程中遇到的问题(依赖jar文件下载失败等)都有介绍,希望能帮助。


      

      

    

2 0
原创粉丝点击