ecliple-tomcat部署maven项目方式之三

来源:互联网 发布:北大青鸟java培训费用 编辑:程序博客网 时间:2024/06/05 02:13

Eclipse创建Maven Web工程并实现Tomcat热部署

首先下载maven工具,解压后在conf/settings.xml文件中添加: 
<localRepository>E:/Workspaces/.m3</localRepository> 
此为maven下载jar及其相关文件的仓库 

第二步,安装eclipse的maven插件M2eclipse: 
http://m2eclipse.sonatype.org/sites/m2e 
安装完成后打开eclipse->window->preferences->maven 
->installations->add->指定maven安装路径 
->user settings->指定maven配置文件settings.xml 

第三步,创建maven项目,properties->project facets->convert to faceted form... 
勾上dynamic web module,点击futher configuration available..., 勾上generate web.xml deployment descriptor,更改webcontent目录为src/main/webapp后点击OK.此时点击项目properties->deployment assembly可以看到指定的webapp路径就是是src/main/webapp目录 

第四步,修改项目pom.xml文件:
 
Xml代码  收藏代码
  1. ...  
  2.   
  3.     <build>  
  4.   
  5.         <sourceDirectory>src/main/java</sourceDirectory>  
  6.         <testSourceDirectory>src/test/java</testSourceDirectory>  
  7.   
  8.         <resources>  
  9.             <resource>  
  10.                 <directory>src/main/resources</directory>  
  11.             </resource>  
  12.         </resources>  
  13.         <testResources>  
  14.             <testResource>  
  15.                 <directory>src/test/resources</directory>  
  16.             </testResource>  
  17.         </testResources>  
  18.   
  19.         <outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>  
  20.         <testOutputDirectory>src/main/webapp/WEB-INF/classes</testOutputDirectory>  
  21.   
  22.         <plugins>  
  23.   
  24.             <plugin>  
  25.                 <artifactId>maven-compiler-plugin</artifactId>  
  26.                 <version>2.3.2</version>  
  27.                 <configuration>  
  28.                     <source>1.6</source>  
  29.                     <target>1.6</target>  
  30.                 </configuration>  
  31.             </plugin>  
  32.   
  33.             <plugin>  
  34.                 <artifactId>maven-resources-plugin</artifactId>  
  35.                 <version>2.5</version>  
  36.                 <executions>  
  37.                     <execution>  
  38.                         <phase>compile</phase>  
  39.                     </execution>  
  40.                 </executions>  
  41.             </plugin>  
  42.   
  43.             <plugin>  
  44.                 <artifactId>maven-dependency-plugin</artifactId>  
  45.                 <version>2.4</version>  
  46.                 <executions>  
  47.                     <execution>  
  48.                         <phase>compile</phase>  
  49.                         <goals>  
  50.                             <goal>copy-dependencies</goal>  
  51.                         </goals>  
  52.                         <configuration>  
  53.                             <outputDirectory>src/main/webapp/WEB-INF/lib</outputDirectory>  
  54.                         </configuration>  
  55.                     </execution>  
  56.                 </executions>  
  57.             </plugin>  
  58.   
  59.             <plugin>  
  60.                 <artifactId>maven-clean-plugin</artifactId>  
  61.                 <version>2.4.1</version>  
  62.                 <configuration>  
  63.                     <filesets>  
  64.                         <fileset>  
  65.                             <directory>src/main/webapp/WEB-INF/lib</directory>  
  66.                             <followSymlinks>false</followSymlinks>  
  67.                         </fileset>  
  68.                     </filesets>  
  69.                 </configuration>  
  70.             </plugin>  
  71.   
  72.         </plugins>  
  73.   
  74.     </build>  


这样修改pom.xml后,删除target目录,在打开cmd: 
在项目根路径下运行: 
mvn eclipse:eclipse 
这样重新生成的classpath会将编译好的java文件和resources中的配置文件指定为src/main/webapp/WEB-INF/classes. 
再运行: 
mvn compile 
之后,mvn会自动编译java文件,copy resources中的文件,并放到classes路径下,并且将项目依赖的jar包copy到lib目录,至此完整的项目形成,全部文件都在webapp目录下. 

最后一步,将项目发布到tomcat上: 
点击eclipse中servers->new->server,全部finish以后双击该server,切换到modules窗口->点击add external module-> 
在document base中browse到项目webapp路径 
在path中输入"/项目名称" 
点击OK后配置全部完成,这样配置的好处不只是热部署,因为eclipse自动编译java文件经常出现问题,在这样的情况下随时可以在项目根路径下用mvn compile命令编译项目
原创粉丝点击