目录

   1.简介

Jetty和Tomcat运行Maven Web项目

  

  1.简介

Maven插件的形式引用进来的,这样耦合性比较低,我们需要什么样的工具就引入对应的插件就可以用。


Jetty和Tomcat运行Maven Web项目

     1)采用Jetty运行Maven Web项目

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

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

    

java中使用Jetty来启动项目 jetty启动web项目_jar


第二步: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端口(

不至于端口冲突)

      

java中使用Jetty来启动项目 jetty启动web项目_maven_02

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

   

java中使用Jetty来启动项目 jetty启动web项目_java_03

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

  

2.采用Tomcat运行Maven Web项目

Tomcat插件,直接在要运行的项目中引入Tomcat插件就可以了,test-maven-console项目添加Tomcat的插件tomcat6-maven-plugin (到2.0版本tomcat-maven-plugin现在已拆分成tomcat7-maven-plugin和tomcat6-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有没有报错。

    

java中使用Jetty来启动项目 jetty启动web项目_nexus_04


java中使用Jetty来启动项目 jetty启动web项目_maven_05


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

  

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:redeploy命令,把Maven Web 项目发布到外部已启动的Tomcat进行测试

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