读书人

Eclipse创办Maven Web工程并实现Tomca

发布时间: 2012-07-24 17:47:58 作者: rapoo

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文件:

...<build><sourceDirectory>src/main/java</sourceDirectory><testSourceDirectory>src/test/java</testSourceDirectory><resources><resource><directory>src/main/resources</directory></resource></resources><testResources><testResource><directory>src/test/resources</directory></testResource></testResources><outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory><testOutputDirectory>src/main/webapp/WEB-INF/classes</testOutputDirectory><plugins><plugin><artifactId>maven-compiler-plugin</artifactId><version>2.3.2</version><configuration><source>1.6</source><target>1.6</target></configuration></plugin><plugin><artifactId>maven-resources-plugin</artifactId><version>2.5</version><executions><execution><phase>compile</phase></execution></executions></plugin><plugin><artifactId>maven-dependency-plugin</artifactId><version>2.4</version><executions><execution><phase>compile</phase><goals><goal>copy-dependencies</goal></goals><configuration><outputDirectory>src/main/webapp/WEB-INF/lib</outputDirectory></configuration></execution></executions></plugin><plugin><artifactId>maven-clean-plugin</artifactId><version>2.4.1</version><configuration><filesets><fileset><directory>src/main/webapp/WEB-INF/lib</directory><followSymlinks>false</followSymlinks></fileset></filesets></configuration></plugin></plugins></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命令编译项目

读书人网 >Web前端

热点推荐