`
liuguofeng
  • 浏览: 432821 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

MyEclipse+Maven+Tomcat集成

阅读更多

本文用来介绍如何将MyEclipse和Maven、Tomcat集成。在实际项目中,经常会把一个项目分成多个子项目,最简单的,分成两个项目。一个项目是业务逻辑,主要和数据库打交道,另外一个是Web层,主要用来处理页面逻辑。

一、环境准备

1、MyEclipse 8.5

2、Maven2

3、Tomcat6

二、Maven4Eclipse插件设置

为了使MyEclipse里面的Maven工程支持多个子工程,需要设置:

打开Window->Preferences->MyEclipse->Maven4Eclipse->Maven,勾选“Support multiple Maven modules mapped to a single Eclipse workspace project”。















三、Maven项目创建

下面在MyEclipse中创建Maven项目。首先创建一个父项目,然后创建两个子项目。

1、创建父项目tired

1)打开New->Other->MyEclipse->Maven->Maven Project,勾选“Create a simple project(skip archetype selection)”。















2)添加项目信息,如Group Id、ArtifactId、Version。由于本项目是父项目,所以Packaging方式为pom。













3)修改pom.xml文件内容,最主要是配置modules为两个子项目,片段内容如下:

...

<groupId>com.vasilis</groupId>
<artifactId>tired</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>tired-biz</module>
<module>tired-webapp</module>
</modules>

...

2、创建子项目tired-biz

1)打开New->Other->MyEclipse->Maven->Maven Module,勾选“Create a simple project(skip archetype selection)”,输入Module Name为tired-biz,在Parent Project选择创建好的tired项目作为tired-biz的父项目,此时,IDE会自动修改父项目pom和子项目的pom文件。













2)因为Maven插件是07年写的,默认使用JDK1.4编译,需要修改。打开tired-biz项目的属性,Properties->Java Compiler,将版本号改为1.6:

















并且,将Java Build Path->Libraries的JRE修改为JDK1.6:













3、创建子项目tired-webapp

1)打开New->Other->MyEclipse->Maven->Maven Module,Module Name输入为tired-webapp,在Parent Project选择创建好的tired项目作为tired-webapp的父项目:













2)在Catalog里选择Internal,在Filter里输入webapp过滤出创建符合Maven标准的Java Web项目的插件,选择org.apache.maven.archetype:















3)输入Group Id、Artifact Id和版本号:













4)同样,修改tired-webapp的JDK版本,打开项目的属性,Properties->Java Compiler,将版本号改为1.6,并且,将Java Build Path->Libraries的JRE修改为1.6。

4、MyEclipse自动部署

由于这样创建的符合Maven标准的Java Web项目不能被MyEclipse自动编译部署,所以,需要修改tired-webapp目录下的.classpath、.mymetadata、.project三个文件。

1)首先,在tired-webapp目录下,命令行执行mven eclipse:eclipse,用来将pom.xml里的依赖关系变成MyEclipse可识别的<classpathentry>配置。.classpath文件内容类似于:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry excluding="**/*.java" kind="src" path="src/main/resources"/>
<classpathentry kind="var" path="M2_REPO/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar"/>
<classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"/>

...
<classpathentry kind="src" path="/tired-biz"/>
<classpathentry kind="lib" path="D:/Java/jdk1.6.0_20/lib/tools.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.Stand
ardVMType/jdk1.6.0_20"/>
<classpathentry kind="output" path="src/main/webapp/WEB-INF/classes"/>
</classpath>

重要配置项有:

a、<classpathentry kind="src" path="src/main/java"/> ,指定项目的源文件夹目录

b、<classpathentry excluding="**/*.java" kind="src" path="src/main/resources"/> ,项目的源文件夹目录可以多个,这里指定配置文件路径,则编译时会同.class 文件一同复制过去。

c、<classpathentry kind="output" path="src/main/webapp/WEB-INF/classes"/>,指定项目自动编译的输出路径。

d、<classpathentry kind="src" path="/tired-biz"/> ,将 tired-webapp依赖的项目tired-biz的类作为源文件加入进来而不是依赖它的jar包。

2).mymetadata文件内容类似于:

<?xml version="1.0" encoding="UTF-8"?>
<project-module
type="WEB"
name="tired-webapp"
id="myeclipse.1277707093121"
context-root="/"
j2ee-spec="1.4"
archive="tired-webapp.war">
<attributes>
<attribute name="webrootdir" value="src/main/webapp" />
</attributes>
</project-module>

该配置文件用于标示tired-webapp项目为Java Web项目,并指定Web项目的根路径为src/main/webapp。

3).project文件类似于:

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>tired-webapp</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.genuitec.eclipse.j2eedt.core.WebClasspathBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.genuitec.eclipse.j2eedt.core.J2EEProjectValidator</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.genuitec.eclipse.j2eedt.core.DeploymentDescriptorValidator</name
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.genuitec.eclipse.ast.deploy.core.DeploymentBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>com.genuitec.eclipse.ast.deploy.core.deploymentnature</nature>
<nature>com.genuitec.eclipse.j2eedt.core.webnature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
</projectDescription>

该文件被MyEclipse用来自动部署符合Maven标准的Java Web项目到Web容器中。

4、部署项目

将tired-webapp部署到Tomcat环境下:























经过以上的步骤后,该工程会自动部署到Tomcat根目录下。从而更方便的开发和调试。

 

转载http://vasilis.blogcn.com/articles/mmt.html

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics