ǰλã >> Eclipse >> zz 使用Eclipse构Maven项目 (step-by-step)
  ϸ

zz 使用Eclipse构Maven项目 (step-by-step)

ȶȣ1858   ʱ䣺2016-04-23 00:57:41.0
zz 使用Eclipse构建Maven项目 (step-by-step)

??/p>

 

         Maven这个???理和构建?化工具,越来越?的开发人员使用它来?理项?的jar包?本文仅对Eclipse??何安装?配?使用Maven进?了介绍?完全step by step?/p>

 

如果觉得?对你有用,?移?到:http://download.csdn.net/detail/qjyong/5582071 下载PDF版,http://download.csdn.net/detail/qjyong/5582087 下载配?示例项目源代码??/span>

1.   Eclipse-jee的安?/h1>

进入eclipse官网的下载页?http://www.eclipse.org/downloads/,?下图?示:

 

根据你的操作系统具体情况选择相应的版??我这里选择的是“Eclipse IDE for JavaEE Developers”的Windows 32 Bit版本。下载到?磁盘即可。把下载完成的?eclipse-jee-juno-SR2-win32.zip”解压缩到本地?盘指定目录下。我这里?压缩在D:盘根?下??/p>

进入解压缩后的目录中,双击?eclipse.exe”即??Eclipse的工作界??(?Eclipse正常运?的前提是在系统变量中存在名为“JAVA_HOME”的变量,?为JDK的安装目录)?/p>

2.   Maven的安装和配置

2.1.  安?

进入Maven官网的下载页?http://maven.apache.org/download.cgi,?下图?示:

 

选择当前?新版?“apache-maven-3.0.4-bin.zip”,下载到本地,解压缩到?磁盘D:下??/p>

 

2.2.  配置

进入Maven安??下的conf子目录中,打?settings.xml进?配置??/p>

2.2.1.   ?默?的本地仓库位?/h3>

Maven默?的本地仓库位?当前用户工作?下的?m2/repository”,使用过程??录里的文件会比较多,占用空间越来越大。一??换到其它磁盘?下??下配?就把默?的本地仓库更改到D: /.m2/repository这个?(这个?结构?要自己创建好)?/p>

<localRepository>d:/.m2/repository</localRepository>

2.2.2.   ?默?的中?库镜?/h3>

Maven默?的中?库里的文件不全?所以,都需要自行添加其它的镜像地址。在settings.xml文件??lt;mirrors>”标签里添加如下内??/p>

     <mirror>

       <id>ibiblio</id>

       <mirrorOf>central</mirrorOf>

       <name>Human Readable Name for this Mirror.</name>

       <url>http://mirrors.ibiblio.org/maven2/</url>

     </mirror>

 

 

保存?做的?,同时还?要把这个settings.xml文件复制?份到“D:/.m2”目录下?/p>

3.   Eclipse中m2eclipse插件的安?/h1>

Eclipse默?不支持Maven。需要给它添加m2eclipse插件。下面是具体的操作?骤??/p>

3.1.  插件安?

依?选择Eclipse菜单栏的“Help”菜单à?Instance NewSoftWare”菜单项,就会弹出?下?话窗口:

 

选择“Add..”按?又会弹出如下对话框:

 

         这个对话框就?于添加一?件地?的?在“Name”?应的输入框里输入该操作的??识名。在“Location”?应的输入框里输入这个插件的安装地?。注:Maven的Eclipse插件地址为:http://download.eclipse.org/technology/m2e/releases。输入后,?下图?示:

 

输入完成后,点击右下角的“OK”按?就会弹出如下对话框:

 

这里?要?择想?安?的插件的详细内?。?中“Maven Integration for Eclipse”前面的复??。?下图?示:

 

选择完成后,点击右下方的“Next>”按?行安装?安装成功后,会提示?要重启Eclipse来生效这次配??重?后,这个Eclipse就可以使用m2eclipse插件功能了??/p>

3.2.  配置

插件安?好之后,还需要进行一些配?能??始使用Maven的功能??/p>

3.2.1.   设置m2eclipse插件?关联的Maven程序

依?选择Eclipse菜单栏的“Window”à?Preferences”菜单项,就会弹出?下?话??/p>

 

在这??话?左边的????“Maven”目录结点,并?择“Installations”子节点,?下图?示:

 

这里?要关联上对应的Maven安?程序。具体操作是,在右边面板??择“Add…?按?会弹出?下?择Maven安??的?择对话框:

 

通过这个对话框来选择Maven的具体安装目录??择好之后就点击“确定?按???下图?示:

 

3.2.2.   设置?义的?仓库

选择如上图?话窗口左边菜单树 “Maven”节点下的?UserSettings”子节点,它默?的配?件是“C:\Documents and Settings\csdn\.m2\settings.xml”,如下图所示:

这边?要修改为我们?义仓库位?的settings.xml文件。具体操作是,点击?Browse…?按?选择上一步??的本地仓库目录下的settings.xml文件,?下图?示:

在上图中选择D:/.m2?下的“settings.xml”文件,点击“打?”按???刚才的?择,就会回到?下图的?话窗口中?/p>

在上图中点击右下角的“OK”按?完成?有的配置??/p>

4.   使用Maven来构建Web项目

4.1.  创建Maven的web项目

m2eclispe插件?认提供了?些Maven样板项目,可以使用这些样板来创建符合我们要求的项??具体操作?下?骤:

在eclipse菜单栏中选择“File”à?News”à?Other”菜单项,就会打?如下对话窗口,在窗口??择“Maven”节点下“Maven Project”条?如下图所示:

点击“Next>”按?进入如下窗口?/p>

再?选择“Next >”按?进入如下窗口?/p>

在这?口中选择“maven-archetype-webapp”来创建?个Maven的web项目,再选择“Next>”按?进入如下图窗口:

在上图窗口的“Group Id”中输入项目的基?名,在?Artifact Id”中输入项目名,其它输入框不用理会?点击?Finish”按?完成项目的整?建过程??/p>

之后,Maven就会根据项目的配??仓库下载相应的文件和jar包?完成后,就有?下项?录结构:

如上图所示,“pom.xml”文件就???maven配置文件。打?此文件,并在?lt;build>”标签内添加如下配置?/p>

<defaultGoal>install</defaultGoal>

<plugins>

         <plugin>

                   <groupId>org.apache.maven.plugins</groupId>

                   <artifactId>maven-surefire-plugin</artifactId>

                   <configuration>

                            <skip>true</skip>

                   </configuration>

         </plugin>

 

         <plugin>

                   <groupId>org.apache.maven.plugins</groupId>

                   <artifactId>maven-compiler-plugin</artifactId>

                   <version>2.5.1</version>

                   <configuration>

                            <source>1.6</source>

                            <target>1.6</target>

                            <encoding>UTF-8</encoding>

                   </configuration>

         </plugin>

</plugins>

这?配置???lt;defaultGoal>”标签指定maven的默认操作是“install”;“maven-surefire-plugin”插件配?“忽略编译项?的测试运行?;“maven-compiler-plugin”插件指定了?些编译时的信????/p>

4.2.  使用Maven添加项目依赖?/h2>

Java Web项目??要添加ServletAPI、JSP API以及JSTL的jar包,这可以直接在pom.xml文件?加了。?下pom.xml文件?添加了Servlet-API 2.5版本、JSP-API2.1版本、JSTL 1.2版本?/p>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

         <modelVersion>4.0.0</modelVersion>

         <groupId>com.qiujy</groupId>

         <artifactId>testweb_svn</artifactId>

         <packaging>war</packaging>

         <version>0.0.1-SNAPSHOT</version>

         <name>testweb_svn Maven Webapp</name>

         <url>http://maven.apache.org</url>

         <dependencies>

                   <dependency>

                            <groupId>javax.servlet</groupId>

                            <artifactId>servlet-api</artifactId>

                            <version>2.5</version>

                            <scope>provided</scope>

                   </dependency>

                   <dependency>

                            <groupId>javax.servlet.jsp</groupId>

                            <artifactId>jsp-api</artifactId>

                            <version>2.1</version>

                            <scope>provided</scope>

                   </dependency>

                   <dependency>

                            <groupId>javax.servlet</groupId>

                            <artifactId>jstl</artifactId>

                            <version>1.2</version>

                   </dependency>

         </dependencies>

         <build>

                   <finalName>testweb_svn</finalName>

                   <defaultGoal>install</defaultGoal>

                   <plugins>

                            <plugin>

                                     <groupId>org.apache.maven.plugins</groupId>

                                     <artifactId>maven-surefire-plugin</artifactId>

                                     <configuration>

                                               <skip>true</skip>

                                     </configuration>

                            </plugin>

                   </plugins>

         </build>

</project>

这里说明?下:对Servlet和JSP的API包,在编译web项目时需要使?而把web项目打包部署到web容器(?Tomcat)中运?时就不需要了。因此,这里把它?<scope>配置为provided?/p>

4.3.  把Maven项目?成Eclipse工程

为了便于在Eclipse?发JavaWeb项目,我?要把Maven项目的目录结构转换成Eclipse工程的目录结构,具体按?下操作?骤进行??/p>

选中项目在右?单中选择“Properties”,弹出如下图所示?话窗口:

选中左边功能树中的?ProjectFacets”节点,然后在?应的右边面板?击?Convert to faceted from…?这?链接,右边面板就会切换成如下图所示:

在?Project Facet”中进?相应配置,?下图?示:

配置好之后,选择窗口右下角的“OK”按?保存配置。这时,这个Maven项目就会?成Eclipse的web工程?结构?/p>

如果还需要直接在Eclipse?行这个web工程,还?要把Maven依赖包添加到????

还需要添加部署到Tomcat时的依赖包,如下图:

点击“Finish”来完成后,对话框?下图?示:

之后点击“OK”按?完成这些设置?/p>

4.4.  把web.xml?成相应版?/h2>

由于Maven?“maven-archetype-webapp”样板项?“web.xml”是2.3版本的,我们?要替换成新版??把Eclipse的web工程webContent/WEB-INF?下的“web.xml”文件?制替换掉“src/main/webapp/WEB-INF/”目录下的web.xml文件?/p>

注意:由于Maven项目编译和发布时,它?把?src”目录下的资源压缩到?个war包中。?使用Eclipse-JEE?发web项目时,都是把页面资源(html/css/js/img/JSP)放?WebContent?下?所以,在使用Maven来编译和发布项目前,?要把WebContent?下的资源复制到?src/main/webapp/”目录下?/p>

5.   使用Maven编译和发布项?/h1>

在项?的?pom.xml”文件上点击右键,在弹出的菜单中选择“Run AS”à?Maveninstall”来编译和生成项???下图?示:

         在编译和生成过程??在?Console”面板中?看到Maven编译过程的信?如果编译和生成成功,?在?Console”面板看到?BUILD SUCCESS”字样??/p>

         另?,在?pom.xml文件?相关jar包信?,可能由于仓库中不存?就会出现?些错?这时,项?译就?存在?些问题??时可通过“Maven clean”?项来清除编译,然后再修改pom.xml到??况?再通过“Maven install”来编译和生成??/p>

6.   示例项目代码

 

7.   附文?/h1>

如下?个SpringMVC3+Spring3+Hibernate4的web项目?pom.xml文件?/p>

<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.qiujy</groupId>

    <artifactId>site2013</artifactId>

    <packaging>war</packaging>

    <version>0.0.1-SNAPSHOT</version>

    <name>site2013 Maven Webapp</name>

    <url>http://maven.apache.org</url>

 

    <properties>

       <spring.version>3.2.3.RELEASE</spring.version>

       <hibernate.version>4.2.2.Final</hibernate.version>

       <slf4j.version>1.7.5</slf4j.version>

       <jackson.version>2.2.2</jackson.version>

    </properties>

 

    <dependencies>

       <!-- servlet/jsp api -->

       <dependency>

           <groupId>javax.servlet</groupId>

           <artifactId>servlet-api</artifactId>

           <version>2.5</version>

           <scope>provided</scope>

       </dependency>

       <dependency>

           <groupId>javax.servlet.jsp</groupId>

           <artifactId>jsp-api</artifactId>

           <version>2.1</version>

           <scope>provided</scope>

       </dependency>

 

       <!-- jstl -->

       <dependency>

           <groupId>javax.servlet</groupId>

           <artifactId>jstl</artifactId>

           <version>1.2</version>

       </dependency>

       <!-- javaxmail

       <dependency>

           <groupId>javax.mail</groupId>

           <artifactId>mail</artifactId>

           <version>1.4.5</version>

        </dependency>-->

 

       <!-- slf4j -->

       <dependency>

           <groupId>org.slf4j</groupId>

           <artifactId>slf4j-log4j12</artifactId>

           <version>${slf4j.version}</version>

       </dependency>

 

       <!-- junit -->

       <dependency>

           <groupId>junit</groupId>

           <artifactId>junit</artifactId>

           <version>4.8.2</version>

       </dependency>

 

       <!-- proxool <dependency> <groupId>proxool</groupId> <artifactId>proxool</artifactId>

           <version>0.8.3</version> </dependency> -->

 

       <!-- mysql driver -->

       <dependency>

           <groupId>mysql</groupId>

           <artifactId>mysql-connector-java</artifactId>

           <version>5.1.22</version>

       </dependency>

 

       <!-- sql server数据库驱?/span> <dependency> <groupId>net.sourceforge.jtds</groupId>

           <artifactId>jtds</artifactId> <version>1.3.0</version> </dependency> -->

 

       <!-- oracle数据库驱?maven仓库?ojdbc下载??/span>oracle的官方授权,先在网上找一?/span>ojdbc14.jar包,上传到本地的maven仓库

           mvn install:install-file -Dfile=D:/resources/ojdbc/ojdbc14.jar -DgroupId=com.oracle

           -DartifactId=ojdbc14 -Dversion=10.2.0.1.0 -Dpackaging=jar <dependency> <groupId>com.oracle</groupId>

           <artifactId>ojdbc14</artifactId> <version>10.2.0.4.0</version> </dependency> -->

 

       <!-- fileupload -->

       <dependency>

           <groupId>commons-fileupload</groupId>

           <artifactId>commons-fileupload</artifactId>

           <version>1.2.2</version>

       </dependency>

       <!-- io -->

       <dependency>

           <groupId>commons-io</groupId>

           <artifactId>commons-io</artifactId>

           <version>2.4</version>

       </dependency>

 

       <!-- c3p0 -->

       <dependency>

           <groupId>c3p0</groupId>

           <artifactId>c3p0</artifactId>

           <version>0.9.1.2</version>

       </dependency>

 

       <!-- struts2 <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId>

           <version>${struts.version}</version> <exclusions> <exclusion> <artifactId>javassist</artifactId>

           <groupId>javassist</groupId> </exclusion> </exclusions> </dependency> <dependency>

           <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId>

           <version>${struts.version}</version> <exclusions> <exclusion> <artifactId>spring-core</artifactId>

           <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> -->

 

       <!-- spring3 -->

       <dependency>

           <groupId>org.springframework</groupId>

           <artifactId>spring-beans</artifactId>

           <version>${spring.version}</version>

       </dependency>

 

       <dependency>

           <groupId>org.springframework</groupId>

           <artifactId>spring-context</artifactId>

           <version>${spring.version}</version>

       </dependency>

 

       <!-- spring?要的aspectj依赖?/span> -->

       <dependency>

           <groupId>org.aspectj</groupId>

           <artifactId>aspectjweaver</artifactId>

           <version>1.7.2</version>

       </dependency>

 

       <dependency>

           <groupId>org.springframework</groupId>

           <artifactId>spring-tx</artifactId>

           <version>${spring.version}</version>

       </dependency>

       <dependency>

           <groupId>org.springframework</groupId>

           <artifactId>spring-jdbc</artifactId>

           <version>${spring.version}</version>

       </dependency>

       <dependency>

           <groupId>org.springframework</groupId>

           <artifactId>spring-orm</artifactId>

           <version>${spring.version}</version>

       </dependency>

 

       <dependency>

           <groupId>org.springframework</groupId>

           <artifactId>spring-context-support</artifactId>

           <version>${spring.version}</version>

       </dependency>

 

       <!-- spring web -->

       <dependency>

           <groupId>org.springframework</groupId>

           <artifactId>spring-web</artifactId>

           <version>${spring.version}</version>

       </dependency>

 

       <!-- spring mvc -->

       <dependency>

           <groupId>org.springframework</groupId>

           <artifactId>spring-webmvc</artifactId>

           <version>${spring.version}</version>

       </dependency>

 

       <!-- <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId>

           <version>2.1.6</version> </dependency> -->

 

       <!-- hibernate4 -->

       <dependency>

           <groupId>org.hibernate</groupId>

           <artifactId>hibernate-core</artifactId>

           <version>${hibernate.version}</version>

           <exclusions>

              <exclusion>

                  <groupId>org.slf4j</groupId>

                  <artifactId>slf4j-api</artifactId>

              </exclusion>

           </exclusions>

       </dependency>

       <dependency>

           <groupId>org.hibernate</groupId>

           <artifactId>hibernate-ehcache</artifactId>

           <version>${hibernate.version}</version>

       </dependency>

 

       <dependency>

           <groupId>com.fasterxml.jackson.core</groupId>

           <artifactId>jackson-databind</artifactId>

           <version>${jackson.version}</version>

       </dependency>

    </dependencies>

 

    <!-- ========================================================== -->

    <build>

       <finalName>site2013</finalName>

       <defaultGoal>install</defaultGoal>

       <plugins>

           <plugin>

              <groupId>org.apache.maven.plugins</groupId>

              <artifactId>maven-surefire-plugin</artifactId>

              <configuration>

                  <skip>true</skip>

              </configuration>

           </plugin>

 

           <plugin>

              <groupId>org.apache.maven.plugins</groupId>

              <artifactId>maven-compiler-plugin</artifactId>

              <version>2.5.1</version>

              <configuration>

                  <source>1.6</source>

                  <target>1.6</target>

                  <encoding>UTF-8</encoding>

              </configuration>

           </plugin>

       </plugins>

    </build>

</project>