在Eclipse中使用Maven 2.x指南

1.Maven 2 Repository

Eclipse需要知道Maven的本地仓库的路径。所以,类路径变量M2_REPO必须设置。执行以下命令:

mvn -Declipse.workspace=<path-to-eclipse-workspace> eclipse:add-maven-repo

 

你也可以在eclipse内定义一个新的classpath变量:从菜单条,选择Window>Preferences,选择java>Build Path>Classpath变量页。进入eclipse,导入workspace下面的Sample Project。由于缺少MAVEN_REPO变量,Eclipse提示编译失败。在Eclipse的Preferences – Java - Build Path - Classpath Variables中添加名为MAVEN_REPO的变量,指向D:/maven/local/repository。

如果想让eclipse支持直接运行maven的goal,可以安装mavenide插件。

 

2.把Maven作为外部工具

可能需要从eclipse执行一些maven goals,这可以通过配置外部装载器实现。为eclise添加一个变量,指向本地maven的可执行文件(mvn.bat/mvn)。从菜单条,选择Window>Preferences,选择Run/Debug>String Substitution,增加一个新的变量,比如maven_exec。当你设置新的外部装载器(丛菜单条,选择Run>External Tools,选择Program),你可以在位置字段引用刚才定义的maven_exec。进一步,引用project_log作为工作目录,指定所选的maven goals作为参数,比如eclipse:eclipse。更多的信息请参考eclipse的帮助。

 

问题:如果插件可以产生变量和runner可能会好一点。

3.一个简单例子

如果你有一个单模块的简单java项目,使用eclipse将十分简单。为了从你的POM产生eclipse项目文件,执行以下命令:

mvn eclipse:eclipse

如果你已经使用eclipse创建或者检出了项目,你只需在workspace中刷新项目。否则,你必须将项目导入eclipse工作空间(从菜单条选择File>Import>Existing Projects into Workspace)。在后面的例子,项目(目录)不应在你的workspace,因为eclipse可能导致错误,特别是你将eclipse作为scm客户端时。

 

4.多模块项目

Due to the workspace idea many eclipse users are used to a flat layout and therefore want to keep this structure, which is possible but not recommended. Actually, the only reason for a flat multiple module project layout is the possibility to checkout and edit the parent POM without checking out the whole project. The following sample shows how to handle maven multiple module projects with eclipse while keeping the recommended hierachical project layout.

 

假设eclipse是你的SCM客户端,这个例子将告诉你如何设置一个新的多模块项目。

1.     设置一个新的叫做step-by-step的workspace,并且象上面所述增加M2_REPO类路径。

2.     打开命令行shell,并且切换到新建的工作空间(workspace)目录。

3.     从命令行使用archetype插件新建一个maven项目

mvn archetype:create -DgroupId=guide.ide.eclipse -DartifactId=guide-ide-eclipse

 

 

 

4.     step-by-step工作空间,使用eclipse创建一个新的简单项目guide-ide-eclipse(从菜单条,选择File>New>Project,选择Simple>Project)。Eclipse会为你的guide-ide-eclipse项目创建一个简单的.project文件,你应该能够看到一个pom.xml文件。

5.     删除src文件夹,并且打开pom.xml文件,将父项目的packaging改为pom。

<packaging>pom</packaging>

问题:mvn eclipse:eclipse也许应该为pom packaging类型产生一个简单的.project文件。

 

6.     从命令行切换到guide-ide-eclipse项目路径,并且创建一些模块:

cd guide-ide-eclipse

mvn archetype:create -DgroupId=guide.ide.eclipse -DartifactId=guide-ide-eclipse-site

mvn archetype:create -DgroupId=guide.ide.eclipse.core -DartifactId=guide-ide-eclipse-core

mvn archetype:create -DgroupId=guide.ide.eclipse.module1 -DartifactId=guide-ide-eclipse-module1

 

 

7.     将新建的模块添加到父pom中。

  <modules>

    <module>guide-ide-eclipse-site</module>

    <module>guide-ide-eclipse-core</module>

    <module>guide-ide-eclipse-module1</module>

  </modules>

 

8.     在新建的模块的POMs中添加parent。

  <parent>

        <groupId>guide.ide.eclipse</groupId>

        <artifactId>guide-ide-eclipse</artifactId>

        <version>1.0-SNAPSHOT</version>

  </parent>

 

9.     添加模块间依赖:

    <dependency>

      <groupId>guide.ide.eclipse.core</groupId>

      <artifactId>guide-ide-eclipse-core</artifactId>

      <version>1.0-SNAPSHOT</version>

    </dependency>

 

10.  把项目安装到本地仓库,并且产生eclipse文件:

mvn install

mvn eclipse:eclipse

 

11.  使用eclipse的团队开发支持(从context菜单选择Team>Share Project)检入项目。记住,不要检入产生的eclipse文件。如果使用CVS,你因该在每个模块有一个.cvsignore文件,内容如下:

target

.classpath

.project

.wtpmodules

即使是父项目,也应该有.cvsignore文件。当你从repository检出项目时,Eclipse自动生成新的简单的.project文件。

 

问题:插件应该可选的产生.cvsignore文件。

 

现在,你有不同的处理选项。如果你同时工作于所有模块,你应该具有eclipse的项目依赖,而不是二进制依赖,你可以设置一个新的workspace,并且从step-by-step/guide-ide-eclipse中导入。记住,你必须先删除父项目的.project文件。结果和从命令行检出整个项目相同,运行mvn eclipse:eclipse并且最后把项目导入到你的eclipse工作空间。两种方式都可以使用eclipse同步你对项目的修改。

 

对于有许多人参与的大项目而言,检出所有模块并且保持更新是一件相当乏味的工作。特别是你只对其中某(几)个模块感兴趣。这种情况,使用二进制依赖更合适。只要在eclipse上检出你需要工作的模块,然后为每个模块运行mvn eclipse:eclipse。当然所有用到的artifacts在你的maven仓库中必须可用。

 

扁平项目布局:

有可能将父POM从自己的目录搬移到和模块相同级别的目录。

1、guide-ide-eclipse下创建一个名为guide-ide-eclipse-project的新目录,并且将父POM搬移到这个目录。

2、对父POM修改模块引用:

  <modules>

    <module>../guide-ide-eclipse-site</module>

    <module>../guide-ide-eclipse-core</module>

    <module>../guide-ide-eclipse-module1</module>

  </modules>

 

问题:发布(releasee)插件不支持扁平结构。

 

Introduction to the Build Lifecycle(1)

8.构建生命周期介绍

8.1.构建生命周期基础

Maven 2是以构建生命周期为中心概念的。这意味着构建和分发一个特定的artifact是被清晰定义的。

 

项目有生命周期(lifecycle),生命周期中有一系列阶段,每个阶段都和一组goals绑定,goals由插件提供。

 

对于构建项目的人来说,只要学习一些命令就可以来构建所有的Maven项目,并且POM保证他们能获得期望的结果。

 

对于一个项目来说,会可能使用以下公共生命周期阶段:

Ø       Validate – 验证项目正确性,以及所有必要的信息是否齐备;

Ø       Compile – 编译项目源代码;

Ø       Test – 使用单元测试框架测试编译过的源代码。这些测试无需代码被打包或者部署;

Ø       Package – 将编译过的代码打包,如JAR

Ø       Integration-test – 为了集成测试能够进行,处理和部署应用到合时的环境;

Ø       Verify – 检查和检验package是否有效并且符合质量要求;

Ø       Install – package安装到本地仓库,供本地其他项目使用(作为依赖项);

Ø       Deploy – 在集成或者发布环境中,将最终的package拷贝到远程仓库,供其它开发人员或者项目共享。

 

记住,对于每个阶段,所有它之前的生命周期阶段都会执行,所以,你在命令行只要指定最后的那个就行。比如:

mvn install

如果执行这个命令的话,会编译、测试、打包、校验和安装package到本地仓库。

 

同时,同样的命令可以在多模块场景中使用。比如:

mvn clean install

这个命令会遍历所有的子项目,对每个子项目执行cleaninstall(包括所有install之前的阶段)。

 

8.2.设置项目以使用构建生命周期

使用构建生命周期是比较简单的,但当你为一个项目创建一个Maven 构建时,如何将任务分配给那些构建阶段呢?

8.2.1.Packaging

首先,是设置项目的packaging,默认为jar,所以不管你是否指定,packaging都会发生。每个packaging包含一系列绑定到特定阶段的goals。比如,对于JAR,会有以下goals绑定到生命周期:

阶段

Goal

Process-resources

Resources:resources

Compile

Compiler:compile

Precess-test-resources

Resources:testResources

Test-compile

Compiler:testCompile

Test

Surefire:test

Package

Jar:jar

Install

Install:install

Deploy

Deploy:deploy

这是标准的一组绑定;然而,有些打包处理不同,比如,一个项目仅仅有元数据,那么只要绑定installdeploy阶段。

 

记住,对于一些packaging类型,你需要在pom.xml文件的build节包含特定的plugin。一个例子就是Plexus插件,它提供了plexus-applicationplexus-service packaging

 

8.2.2.插件

第二种对阶段增加goals的方法就是在项目中配置插件。插件包含每个goal绑定到哪个阶段的信息。记住,仅仅增加插件还不够,你必须同时指定你需要的goals

 

这些配置的goals将会加入到由packaging绑定到生命周期的goals。如果绑定到特定阶段的goal超过一个,那么来自packaginggoal会先执行,然后执行在POM配置的goal。可以通过executions元素控制特定goals的顺序。

 

比如,Modello插件将modello:java绑定到generate-souces阶段,使用Modello插件从模型产生源代码,你必须将以下内容加入到POMplugins节:

...

 <plugin>

   <groupId>org.codehaus.modello</groupId>

   <artifactId>modello-maven-plugin</artifactId>

   <executions>

     <execution>

       <configuration>

         <model>maven.mdo</model>

         <modelVersion>4.0.0</modelVersion>

       </configuration>

       <goals>

         <goal>java</goal>

       </goals>

     </execution>

   </executions>

 </plugin>

...

你可能会觉得奇怪,那儿为什么会有executions元素,这可以让你在需要时,使用不同的配置多次运行相同的goal。不同的execution可以给于不同的id,这样当用于继承或者应用profiles时,你就可以控制goal配置是否合并或者变成附加的execution中。

 

当有多个executions匹配一个阶段(phase)时,他们以POM中指定的顺序执行,来自继承的executions首先运行。

 

对于modello:java,它只对generate-source阶段(phase)有意义。不过某些goals可以用于多个阶段,并且没有默认值,对于这种情况,你可以(必须)自己指定阶段(phase)。比如,假设有一个goal touch:timestamp,用于将当前时间显示到文件中,你需要在process-test-resources阶段用它来指出测试是何时开始的,可以像下面那样配置:

...

 <plugin>

   <groupId>com.mycompany.example</groupId>

   <artifactId>touch-maven-plugin</artifactId>

   <executions>

     <execution>

       <phase>process-test-resources</phase>

       <configuration>

         <file>${project.output.directory}/timestamp.txt</file>

       </configuration>

       <goals>

         <goal>timestamp</goal>

       </goals>

     </execution>

   </executions>

 </plugin>

...

 

Maven 2 介绍(5)

7.7.6.创建WEB应用程序

Now we want to use this library in a Web application. For simplicity, our Web application will consist of a JavaServer Pages (JSP) file that directly invokes the HotelModel class. First, we create a new Web application project using the archetype plug-in:

现在,要将此类库应用到一个web应用程序。为了简单,我们的web应用程序将由一个jsp文件直接调用HotelModel类。首先,我们使用archetype插件创建一个新的web应用项目:

mvn archetype:create -DgroupId=com.javaworld.hotels -
DartifactId=HotelWebapp -Dpackagename=com.javaworld.hotels -
DarchetypeArtifactId=maven-archetype-webapp

 

 

Next, we need to include our business logic JAR in this application. All we need to do is to add a dependency to the new pom.xml, pointing to our HotelDatabase component:

接着,我们需要将业务逻辑JAR包含进此web应用程序。我们所需做的事情就是加入一个依赖项到此新的pom.xml文件,并指向我们的HotelDatabase组件:

    <dependency>
      <groupId>com.javaworld.hotels</groupId>
      <artifactId>HotelDatabase</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

Now we implement the main (and only) JSP page. It simply lists the available cities and, if a city is chosen, lists the corresponding hotels:

现在,我们实现主要的JSP页面。它仅简单的列出所有可用的城市,如果选择了某个城市,就列出相应的宾馆:

<html>
<body>
<h2>Hotel database tutorial application</h2>
<%@ page import="
   java.util.List,
    com.javaworld.hotels.businessobjects.Hotel,
    com.javaworld.hotels.model.HotelModel"
%>
<%
   HotelModel model = new HotelModel();

   String[] cityList = model.findAvailableCities();

   String selectedCity = request.getParameter("city");
   List<Hotel> hotelList = model.findHotelsByCity(selectedCity);
%>

   <h3>Choose a destination</h3>
   <form action="index.jsp" method="get">
     Please choose a city:
      <SELECT name="city">
         <OPTION value="">---Any city---</OPTION>
         <%
         for(String cityName : cityList){
         %>
          <OPTION value="<%=cityName%>"><%=cityName%></OPTION>
         <%
         }
         %>
      </SELECT>
      <BUTTON type="submit">GO</BUTTON>
   </form>
   <% if (hotelList.size() > 0) { %>
      <h3>Available hotels in <%=selectedCity%> </h3>
      <table border="1">
           <tr>
             <th>Name</th>
             <th>Address</th>
             <th>City</th>
             <th>Stars</th>
            </tr>
         <%
         for(Hotel hotel : hotelList){
         %>
           <tr>
             <td><%=hotel.getName()%></td>
             <td><%=hotel.getAddress()%></td>
             <td><%=hotel.getCity()%></td>
             <td><%=hotel.getStars()%> stars</td>
            </tr>
         <%
         }
         %>
       </table>
    <%}%>
</body>
</html>

 

 

Now run mvn install from the HotelWebapp directory; this will compile, bundle, and deploy the HotelWebapp.war file to your local repository (you can also find it in the target directory if you need to). Now you can deploy this war file to your favorite application server and see what you get (see Figure 4).

 

现在,从HotelWebapp目录运行mvn install命令,这将编译、打包并且部署HotelWebapp.war文件到你的本地仓库(你也可以从target目录找到它)。现在,你可以把此war文件部署到你熟悉的应用服务器,并且可以看到如下的页面:

 

Figure 4. The tutorial application in action

7.7.7.使用插件

Maven 2 comes with an ever-increasing number of plug-ins that add extra functions to your build process with little effort. To use a plug-in, you bind it to a lifecycle phase. Maven will then figure out when (and how) to use it. Some plug-ins are already used by Maven behind the scenes, so you just have to declare them in the plugins section of your pom.xml file. The following plug-in, for example, is used to compile with J2SE 1.5 source code:

 

Maven 2含有乙一组数量不断增长的插件,用来为你的构建过程增加额外的功能。为了使用插件,你需要将它绑定到生命周期阶段。Maven将指出何时(以及如何)使用它们。一些组件,Maven在背后已经使用了,所以你只要在pom.xml文件的plugins节声明它们。以下插件,用来编译J2SE 1.5的源代码:

...
   <build>
      ...
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
               <source>1.5</source>
               <target>1.5</target>
            </configuration>
         </plugin>
      </plugins>
   </build>

 

 

 In other cases, you bind the plug-in to a lifecycle phase so that Maven will know when to use it. In the following example, we want to run some standard Ant tasks. To do this, we bind the maven-antrun-plugin to the generate-sources phase, and add the Ant tasks between the tasks tags, as shown here:

另外,你可以将插件和某个生命周期阶段绑定,这样,Maven就会知道何时使用它们。下面的例子,我们要运行一些标准的Ant任务。为了达到这个目的,我们将maven-antrun-plugin插件绑定到generate-sources阶段,并且把ant任务插入到tasks标记处,如下所示:

...
  <build>
    ...
    <plugins>
       <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <executions>
            <execution>
              <phase>generate-sources</phase>
              <configuration>            
                <tasks>
                  <!-- Ant tasks go here -->
                </tasks>
              </configuration>
              <goals>
                <goal>run</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
     </plugins>    
  </build>

 

 

7.7.4.使用Maven2进行单元测试

Now let's test the application. A few simple test classes can be found in the source code. Unit testing is (or should be!) an important part of any enterprise Java application. Maven completely integrates unit testing into the development lifecycle. To run all your unit tests, you invoke the test lifecycle phase:

现在来测试应用程序,在源代码中可以找到一些简单测试类。单元测试是(或者应该是)任何企业java应用程序的重要组成部分。Maven将单元测试完全集成进了开发生命周期。要运行所有的单元测试,可以调用test生命周期阶段:

mvn test

 

 

If you want to run only one test, you can use the test parameter:

如果你只想运行某个单元测试,可以使用test参数指定:

mvn test -Dtest=HotelModelTest

 

 

A nice feature of Maven 2 is its use of regular expressions and the test parameter to control the tests you want to run. If you want to run only one test, you just indicate the name of the test class:

Maven 2的一个特性是test参数使用规则表达式来运行测试,如果只想运行某个测试,你只要指出测试类的名称就可以了:

mvn test -Dtest=HotelModelTest

 

 

If you want to run only a subset of the unit tests, you can use a standard regular expression. For example, to test all ModelTest classes:

如果你想运行一组单元测试,可以使用标准的规则表达式,比如,为了测试所有的ModelTest类:

mvn test -Dtest=*ModelTest

 

 

7.7.5.构建和部署JAR

Once you're happy with the tests, you can build and deploy your new JAR. The install command compiles, tests, and bundles your classes into a jar file and deploys it to your local Maven 2 repository, where it can be seen by other projects:

一旦完成了单元测试,就可以构建和部署新的JARInstall命令编译、测试和将所有的类文件打包成一个jar文件,并把此jar文件部署到你的本地Maven 2仓库中。

mvn install

 

 Maven 2 介绍(3)

7.7.应用实例

Now that we have seen a few of the basic notions used in Maven 2, let's see how it works in the real world. The rest of this tutorial examines how we would use Maven 2 on a simple Java Enterprise Edition project. The demo application involves an imaginary (and simplified) hotel database system. To demonstrate how Maven handles dependencies between projects and components, this application will be built using two components (see Figure 3):

 

现在,让我们来看一些Maven 2中的基本标记,看看它们如何工作。以下部分用Maven 2来开发一个简单的java企业级应用项目。示例应用程序涉及到虚构的(简化的)宾馆数据库系统。为了介绍Maven如何处理项目以及组件之间的依赖,这个应用使用两个组件(子项目)。

  • A business logic component: HotelDatabase.jar
  • A Web application component: HotelWebApp.war

 

Figure 3. The tutorial application architecture involves two simple components: a JAR (HotelDatabase.jar) and a WAR (HotelWebapp.war)

You can download the source code to follow along with the tutorial in Resources.

 

7.7.1.设置项目环境

We start by configuring your work environment. In real-world projects, you will often need to define and configure environment or user-specific parameters that should not be distributed to all users. If you are behind a firewall with a proxy, for example, you need to configure the proxy settings so that Maven can download JARs from repositories on the Web. For Maven 1 users, the build.properties and project.properties files do this job. In Maven 2, they have been replaced by a settings.xml file, which goes in the $HOME/.m2 directory. Here is an example:

 

我们从配置工作环境起步。在真实项目中,你经常需要定义和配置环境或者用户指定的参数,这些环境和参数不和其他用户共享。如果你使用防火墙,那么你需要配置代理服务器,让Maven可以从远程仓库下载JARs。对于Maven 1的用户,由build.propertiesproject.properties文件来做这项工作,而在Maven 2中,则由$HOME/.m2目录下的settings.xml文件来做这项工作:

<?xml version="1.0" encoding="UTF-8"?>
<settings>
  <proxies>
    <proxy>
      <active/>
      <protocol>http</protocol>
      <username>scott</username>
      <password>tiger</password>
      <port>8080</port>
      <host>my.proxy.url</host>
      <id/>
    </proxy>
  </proxies>
</settings>

 

7.7.2.使用archetype插件创建新项目

The next step is to create a new Maven 2 project template for the business logic component. Maven 2 provides the archetype plug-in, which builds an empty Maven 2-compatible project directory structure. This plug-in proves convenient for getting a basic project environment up and running quickly. The default archetype model will produce a JAR library project. Several other artifact types are available for other specific project types, including Web applications, Maven plug-ins, and others.

接下来,就是为业务逻辑组件创建一个新的Maven 2项目模板。Maven 2提供了archetype插件,用来创建空的、兼容Maven 2的项目目录结构。这个插件为快速建立基本项目环境提供了方便。默认的archetype模型将产生JAR库项目。其他一些artifact类型可用来创建其他指定的项目类型,包括WEB应用程序,Maven插件等等。

 

Run the following command to set up your HotelDatabase.jar project:

运行以下命令来创建你的HoteDatabase.jar项目:

mvn archetype:create -DgroupId=com.javaworld.hotels -
   DartifactId=HotelDatabase -Dpackagename=com.javaworld.hotels

Now you have a brand new Maven 2 project directory structure. Switch to the HotelDatabase directory to continue the tutorial.

现在,你有了一个新的Maven 2项目目录结构,切换到HotelDatabase目录,继续此向导。

7.7.3.实现业务逻辑

Now we implement the business logic. The Hotel class is a simple JavaBean. The HotelModel class implements two services: the findAvailableCities() method, which lists available cities, and the findHotelsByCity() method, which lists all hotels in a given city. A simple, memory-based implementation of the HotelModel class is presented here:

现在,我们实现业务逻辑,Hotel类是一个简单的JavaBean。而HotelModel类实现两个服务:findAvailabelCities()方法,列出所有能够获得的城市;

findHotelsByCity()方法,则依据给定的城市获取宾馆列表。

以下是一个简单的,基于内存实现的HotelModel类:

package com.javaworld.hotels.model;

import java.util.ArrayList;
import java.util.List;

import com.javaworld.hotels.businessobjects.Hotel;

public class HotelModel {

   /**
    * The list of all known cities in the database.
    */
   private static String[] cities =
   {
      "Paris",
      "London",
   };
  
   /**
    * The list of all hotels in the database.
    */
   private static Hotel[] hotels = {
      new Hotel("Hotel Latin","Quartier latin","Paris",3),
      new Hotel("Hotel Etoile","Place de l'Etoile","Paris",4),
      new Hotel("Hotel Vendome","Place Vendome","Paris",5),
      new Hotel("Hotel Hilton","Trafalgar Square","London",4),
      new Hotel("Hotel Ibis","The City","London",3),
   };
  
   /**
    * Returns the hotels in a given city.
    * @param city the name of the city
    * @return a list of Hotel objects
    */
   public List<Hotel> findHotelsByCity(String city){
      List<Hotel> hotelsFound = new ArrayList<Hotel>();
      
      for(Hotel hotel : hotels) {
         if (hotel.getCity().equalsIgnoreCase(city)) {
            hotelsFound.add(hotel);
         }
      }      
      return hotelsFound;
   }  
  
   /**
    * Returns the list of cities in the database which have a hotel.
    * @return a list of city names
    */
   public String[] findAvailableCities() {
      return cities;
   }
}

 

 

7.3.项目生命周期

Project lifecycles are central to Maven 2. Most developers are familiar with the notion of build phases such as compile, test, and deploy. Ant has targets with names like those. In Maven 1, corresponding plug-ins are called directly. To compile Java source code, for instance, the java plug-in is used:

 

项目生命周期是Maven 2的中心。大多数开发人员熟悉构建阶段的一些标记,比如compile,test,以及deployAnttargets的名称也和这个类似。在Maven 1中,相应的插件是直接调用的,为了编译Jave源代码,使用java插件:

$maven java:compile

In Maven 2, this notion is standardized into a set of well-known and well-defined lifecycle phases (see Figure 2). Instead of invoking plug-ins, the Maven 2 developer invokes a lifecycle phase:

Maven 2中,这些标记被标准化到一组众所周知的、明确的生命周期阶段(参见图2)。在Maven 2中,开发人员直接调用生命周期阶段,而不是调用插件:

$mvn compile.

 

 

 

Figure 2. Maven 2 lifecycle phases

Some of the more useful Maven 2 lifecycle phases are the following: 以下是一些有用的Maven 2生命周期阶段:

  • generate-sources: Generates any extra source code needed for the application, which is generally accomplished using the appropriate plug-ins 。生成额外的应用程序所需的源代码,通常由相应插件获得。
  • compile: Compiles the project source code 。编译项目源代码。
  • test-compile: Compiles the project unit tests 。编译项目单元测试源代码。
  • test: Runs the unit tests (typically using JUnit) in the src/test directory。运行src/test目录下的单元测试 (通常使用junit)。
  • package: Packages the compiled code in its distributable format (JAR, WAR, etc.) 。以分发的格式(jar,war等等)打包。
  • integration-test: Processes and deploys the package if necessary into an environment where integration tests can be run 。集成测试。
  • install: Installs the package into the local repository for use as a dependency in other projects on your local machine 。安装包到本地仓库,作为你本机的其他项目的依赖项。
  • deploy: Done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects。在集成和发布环境下使用,拷贝最终的包到远程仓库,分享给其他开发人员和项目。

Many other lifecycle phases are available. See Resources for more details.

These phases illustrate the benefits of the recommended practices encouraged by Maven 2: once a developer is familiar with the main Maven 2 lifecycle phases, he should feel at ease with the lifecycle phases of any Maven project.

还有许多其他生命周期阶段。这些阶段阐明了使用Maven 2推荐的开发实践的好处:一旦一个开发人员熟悉了主要的Maven 2生命周期阶段,他就会易于接受其他Maven项目的生命周期阶段。

 

The lifecycle phase invokes the plug-ins it needs to do the job. Invoking a lifecycle phase automatically invokes any previous lifecycle phases as well. Since the lifecycle phases are limited in number, easy to understand, and well organized, becoming familiar with the lifecycle of a new Maven 2 project is easy.

生命周期阶段调用它所需的插件来完成任务。调用一个生命周期阶段自动调用所有在它之前的生命周期阶段。由于生命周期阶段在数量上有限,易于理解,组织良好,理解一个使用Maven 2的新项目的生命周期是相对容易的事情。

 

7.4.及物依赖Transitive dependencies

One of the highlights of Maven 2 is transitive dependency management. If you have ever used a tool like urpmi on a Linux box, you'll know what transitive dependencies are. With Maven 1, you have to declare each and every JAR that will be needed, directly or indirectly, by your application. For example, can you list the JARs needed by a Hibernate application? With Maven 2, you don't have to. You just tell Maven which libraries you need, and Maven will take care of the libraries that your libraries need (and so on).

Maven 2的一大亮点就是及物依赖管理。如果你使用过象Linux平台上的urpmi的工具,你会知道什么是及物依赖。在Maven 1中,你必须声明你所需要的每个应用程序直接或者间接需要的JAR。比如,Hibernate应用程序所需的JARs就有很多。在Maven 2中,你不必如此,你只要告诉Maven你所需要的库,Maven会照顾到你的库所需要的库。

 

Suppose you want to use Hibernate in your project. You would simply add a new dependency to the dependencies section in pom.xml, as follows:

假设你在项目中需要使用Hibernate,你只要简单的在pom.xml文件的dependencies节中加入一个新的dependency,如下所示:

<dependency>
      <groupId>hibernate</groupId>
      <artifactId>hibernate</artifactId>
      <version>3.0.3</version>
      <scope>compile</scope>
   </dependency>

And that's it! You don't have to hunt around to know in which other JARs (and in which versions) you need to run Hibernate 3.0.3; Maven will do it for you!

这就够了!你不必知道运行Hibernate 3.0.3需要哪些JARs,以及它们的版本,Maven会为你做这些工作!

 

The XML structure for dependencies in Maven 2 is similar to the one used in Maven 1. The main difference is the scope tag, which is explained in the following section.

 

7.5.依赖范围

In a real-world enterprise application, you may not need to include all the dependencies in the deployed application. Some JARs are needed only for unit testing, while others will be provided at runtime by the application server. Using a technique called dependency scoping, Maven 2 lets you use certain JARs only when you really need them and excludes them from the classpath when you don't.

在实际企业应用程序中,你可能无需包含所有的依赖项到你的部署应用程序中,有些JARs只有单元测试需要,而有些可能由应用服务器提供。使用称为依赖范围的技术,Maven 2让你使用那些你需要的JARs,而在不需要时则从classpath排除它们。

Maven provides four dependency scopes: Maven提供了4个依赖范围

  • compile: A compile-scope dependency is available in all phases. This is the default value. 编译时依赖在所有阶段都可获得,这是默认值。
  • provided: A provided dependency is used to compile the application, but will not be deployed. You would use this scope when you expect the JDK or application server to provide the JAR. The servlet APIs are a good example. Provided依赖范围用来编译应用程序,但无需部署。若你用到jdk或者应用服务器提供的JAR,则使用此范围,servlet APIs就属于这个依赖范围。
  • runtime: Runtime-scope dependencies are not needed for compilation, only for execution, such as JDBC (Java Database Connectivity) drivers. 运行依赖范围在编译阶段是不需要的,只有在运行时需要,比如JDBC驱动程序。
  • test: Test-scope dependencies are needed only to compile and run tests (JUnit, for example). 测试范围依赖,仅在编译和运行单元测试时需要(比如Junit)。

7.6.项目沟通

An important part of any project is internal communication. While it is not a silver bullet, a centralized technical project Website can go a long way towards improving visibility within the team. With minimal effort, you can have a professional-quality project Website up and running in very little time.

任何项目中,内部沟通都是一个重要部分,一个集中的项目技术网站可以显著提高开发组内的沟通能力。通过Maven 2,你能够轻易的创建一个专业级的项目网站。

 

This takes a whole new dimension when the Maven site generation is integrated into a build process using continuous integration or even automatic nightly builds. A typical Maven site can publish, on a daily basis:

Maven站点产生功能使用持续集成或者自动每日构建,集成到构建过程中后,将会带来一个全新的境界。典型的Maven站点能够每日发布:

  • General project information such as source repositories, defect tracking, team members, etc. 一般项目信息,比如源代码库,缺失跟踪,小组成员等等。
  • Unit test and test coverage reports 。单元测试以及测试覆盖报表。
  • Automatic code reviews and with Checkstyle and PMD 。使用CheckstylePMD自动进行代码reviews
  • Configuration and versioning information。配置和版本信息。
  • Dependencies 。依赖项
  • Javadoc
  • Source code in indexed and cross-referenced HTML format。索引的以及交叉引用的HTML格式的源代码。
  • Team member list 。小组成员列表。
  • And much more。以及其他。

Once again, any Maven-savvy developer will immediately know where to look to become familiar with a new Maven 2 project.

再重申一下,任何熟悉Maven的开发人员将能够快速的熟悉另一个新的基于Maven 2的项目。
Maven 2 介绍(1)

7.Maven 2介绍

Maven is a popular open source build tool for enterprise Java projects, designed to take much of the hard work out of the build process. Maven uses a declarative approach, where the project structure and contents are described, rather then the task-based approach used in Ant or in traditional make files, for example. This helps enforce company-wide development standards and reduces the time needed to write and maintain build scripts.

 

Maven是一个流行的构建企业级java项目的开源构建工具,它的设计目标是将一些构建过程中的繁重工作剥离出来。Maven使用宣告式的方法,项目的结构和内容是被描述的,而不是像使用ant或者传统make文件那样基于任务的方法。这样可以加强企业范围内的开发标准,并且减少编写和维护构建脚本的时间。

 

The declarative, lifecycle-based approach used by Maven 1 is, for many, a radical departure from more traditional build techniques, and Maven 2 goes even further in this regard. In this article, I go through some of the basic principals behind Maven 2 and then step through a working example. Let's start by reviewing the fundamentals of Maven 2.

 

宣告式的,基于生命周期的方法在Maven 1中就已经得到应用,在这方面,Maven 2走的更远。先来看一下Maven 2的基础。

7.1.项目对象模型

The heart of a Maven 2 project is the project object model (or POM for short). It contains a detailed description of your project, including information about versioning and configuration management, dependencies, application and testing resources, team members and structure, and much more. The POM takes the form of an XML file (pom.xml), which is placed in your project home directory. A simple pom.xml file is shown here:

Maven 2项目的核心是项目对象模型,缩写为POM。项目对象模型包括对项目的详细描述,包括项目的版本信息、配置管理、依赖、应用和测试资源、开发组成员和结构、以及更多。POM采用XML文件方式(pom.xml),放置在项目的根目录下,下面是一个pom.xml文件的示例:

<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.javaworld.hotels</groupId>

   <artifactId>HotelDatabase</artifactId>

   <packaging>war</packaging>

   <version>1.0-SNAPSHOT</version>

   <name>Maven Quick Start Archetype</name>

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

   <dependencies>

<dependency>

         <groupId>junit</groupId>

         <artifactId>junit</artifactId>

         <version>3.8.1</version>

         <scope>test</scope>

  </dependency>

</dependencies>

</project>

 

 

7.2.Maven 2项目目录结构

Much of Maven's power comes from the standard practices it encourages. A developer who has previously worked on a Maven project will immediately feel familiar with the structure and organization of a new one. Time need not be wasted reinventing directory structures, conventions, and customized Ant build scripts for each project. Although you can override any particular directory location for your own specific ends, you really should respect the standard Maven 2 directory structure as much as possible, for several reasons:

 

Maven的魔力来自于他所鼓励的标准实践。一个经历过使用Maven开发项目的开发人员会马上对新的项目结构和组织感到熟悉。这样就无需浪费对每个项目重新熟悉目录结构,约定以及定制ANT构建脚本的时间。尽管你可以用自己的方式覆盖任何特定的目录位置,你还是应该尽量遵从标准Maven 2的目录结构:

  • It makes your POM file smaller and simpler (使你的POM文件简短)
  • It makes the project easier to understand and makes life easier for the poor guy who must maintain the project when you leave (使项目易于理解)
  • It makes it easier to integrate plug-ins(易于集成插件)

The standard Maven 2 directory structure is illustrated in Figure 1. In the project home directory goes the POM (pom.xml) and two subdirectories: src for all source code and target for generated artifacts.

 

标准的Maven 2目录结构如图1所示。项目主目录下有一个POM(pom.xml)以及两个子目录:src放置所有的源代码,target目录放置构建产生的文件。

 

Figure 1. The standard Maven 2 directory layout

The src directory has a number of subdirectories, each of which has a clearly defined purpose:

 

src目录有一些子目录,每个子目录都有清晰定义的目的:

  • src/main/java: Your Java source code goes here (strangely enough!) java源代码目录
  • src/main/resources: Other resources your application needs 。应用程序所需的资源目录。
  • src/main/filters: Resource filters, in the form of properties files, which may be used to define variables only known at runtime。资源过滤文件,属性文件格式,用于定义运行时所需得变量。
  • src/main/config: Configuration files。配置文件。
  • src/main/webapp: The Web application directory for a WAR projectWAR项目的web应用程序目录
  • src/test/java: Unit tests。单元测试源代码。
  • src/test/resources: Resources to be used for unit tests, but will not be deployed 。用于单元测试的资源,不会被部署。
  • src/test/filters: Resources filters to be used for unit tests, but will not be deployed 。单元测试所用到的资源过滤,不会被部署。
  • src/site: Files used to generate the Maven project Website。用于产生maven项目web站点的目录。
MAVEN指南(8)

5.2.parent

artifactId

父项目的artifact标识符

groupId

父项目的group标识符

version

父项目的版本

relativePath

父项目的pom.xml文件的相对路径。默认值为../pom.xml。maven首先从当前构建项目开始查找父项目的pom文件,然后从本地仓库,最有从远程仓库。RelativePath允许你选择一个不同的位置。

 

5.3.prerequisites

项目的先决条件

maven

构建此项目所需的maven的最低版本

 

5.4.issueManagement

用于管理此项目的发布跟踪(bug跟踪)。

system

构建此项目所需的maven的最低版本

url

此项目使用的发布管理系统的URL。

 

5.5.ciManagement

system

持续集成系统的名称,比如,continuum。

url

此项目使用的持续集成系统的URL,如果有WEB界面的话。

notifiers

配置用户信息和通知模式,当构建成功通知开发人员/用户,

 

5.6.notifier

配置通知方法,当构建中断时通知用户/开发人员。

Type

投递通知的机制

SendOnError

是否发送错误通知

SendOnFailure

是否发送失败通知

SendOnSuccess

是否发送成功通知

SendOnWarning

是否发送警告通知

Address

Deprecated。发送通知的地址,通常为email地址

Configuration

附加配置信息

 

5.7.mailingList

此项目的邮件列表。自动产生的站点将引用此信息。

Name

邮件列表的名称

Subscribe

订阅此邮件列表的email地址或连接。如果是email地址,产生文档时会自动生成mailto:连接

Unsubscribe

退订此邮件列表的email地址或连接。如果是email地址,产生文档时会自动生成mailto:连接

Post

可以投递到此邮件列表的email地址或连接。如果是email地址,产生文档时会自动生成mailto:连接

Archive

可以浏览到邮件列表存档信息的URL。

OtherArchives

可选的替代URLs,用于浏览邮件存档信息列表

 

 

MAVEN指南(7)

5.1.project元素

<project>元素为描述符的根元素,以下列表列出它的所有子元素:

parent

父项目的位置,父项目中的值就是此项目中未指定的值的默认值。父项目的位置由group ID,artifact ID和version指定。

modelVersion

声明此POM项目描述符所遵从的版本

groupId

一个全局唯一的项目标识符。通常使用完全限定的包名来和其它项目区隔(比如,org.apache.maven)

artifactId

在给定gorup ID内唯一的产品标识符。Artifact就是由项目产生的,或者由项目所使用的东西。比如由Maven项目产生的artifacts包括:JARs,source以及二进制分发包和WARs。

packaging

此项目产生的artifact的类型,比如jar,war,ear pom。插件能够创建他们自己的packaging,所以,这里并没有列出所有可能的类型。

name

项目的名称

version

此项目产生的artifact的当前版本。

description

此项目的详细描述,被maven用来在需要的地方描述项目,比如web站点。而此元素可以使用CDATA,让description里可以包含HTML标签。

url

项目主页的URL。

prerequisites

描述此项目构建环境的先决条件

issueManagement

描述此项目的发布管理系统信息

ciManagement

此项目的持续集成信息

inceptionYear

此项目开始年份,用四位整数指定。当产生版权信息时使用

mailingLists

项目的邮件列表信息

developers

项目的开发小组

contributors

项目的贡献者,但不属于开发小组

licenses

本项目的许可。这里的许可是对本项目的许可,而不是对依赖项的许可。如果有多个许可,那么用户可以选择其中之一,而不必全部。

scm

指定此项目使用的源代码控制系统,如CVS等等。

organization

描述此项目所属的机构。

build

构建此项目所需的信息。

profiles

项目本地构建文件列表,如果使用,可改变默认构建过程。

modules

作为本项目一部分的模块(子项目)。

repositories

查找发现依赖项和扩展项的远程仓库。

pluginRepositories

查找发现构建和报表所需插件的远程仓库。

dependencies

此项目的所有依赖项列表。这些依赖项在构建过程中用于构造所需的classpath。这些依赖项在构建时会自动下载。

reports

Deprecated。Maven现在忽略此元素。

reporting

此元素包含用于在Maven产生的项目站点中产生报表的插件的规格。这些报表在执行mvn site命令时运行,所有报表出现在导航条上。

dependencyManagement

所有项目的依赖信息继承于此。这一节中的依赖项并不马上解析。事实上,当一个继承于此的POM使用groupId和artifactId定义一个依赖项时,当版本信息和其他信息没有指定时,才会使用这里的依赖项定义。

distributionManagement

此项目的分发信息。

properties

POM可以用作替代,以及用于资源过滤。

 

MAVEN指南(6)

2.16.配置报表

maven提供了多种报表,你可以把它们加入到你的web站点上,用于显示项目的当前状态。这些报表都通过插件获得。

 

为了给你的站点添加报表,必须在POM的reporting节加入插件。下面的例子显示如何配置标准的项目信息报表。

<project>

  ...

  <reporting>

    <plugins>

      <plugin>

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

        <artifactId>maven-project-info-reports-plugin</artifactId>

      </plugin>

    </plugins>

  </reporting>

  ...

 

如果在site.xml中包含有${reports},那么产生的站点中将出现这些报表。

2.17.国际化

maven中,国际化是非常简单的。

为了允许使用多个locales,按如下配置就可以了:

<project>

  ...

  <build>

    <plugins>

      <plugin>

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

        <artifactId>maven-site-plugin</artifactId>

        <configuration>

          <locales>en,fr</locales>

        </configuration>

      </plugin>

    </plugins>

  ...

这样,就会同时产生一个英语和法语的站点版本。如果en是你的当前locale,那么,它会在站点的根目录下,而法语翻译版本则在fr/子目录下。

 

为了取代默认的翻译内容,可以在站点目录下放置一个以locale的名字为名称的子目录,同时,创建该locale的站点描述符文件,比如:

+- src/

   +- site/

      +- apt/

      |  +- index.apt     (Default version)

      +- fr/

      |  +- apt/

      |  |  +- index.apt  (French version)

      +- site.xml         (Default site descriptor)

      +- site_fr.xml      (French site descriptor)

 

通过创建相关语言的站点描述符,翻译的站点就可以独立发展了。

 

2.18.如何构建其它类型的项目?

注意,生命周期将会被应用到任何项目类型。比如,退回到基目录,可以创建一个简单的web应用程序。

mvn archetype:create /

    -DgroupId=com.mycompany.app -DartifactId=my-webapp /

    -DarchetypeArtifactId=maven-archetype-webapp</source>

这个命令必须在一行内。运行此命令后,将会创建一个my-webapp的目录,同时包含如下项目描述符:

<project>

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mycompany.app</groupId>

  <artifactId>my-webapp</artifactId>

  <packaging>war</packaging>

  <version>1.0-SNAPSHOT</version>

  <dependencies>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>3.8.1</version>

      <scope>test</scope>

    </dependency>

  </dependencies>

  <build>

    <finalName>my-webapp</finalName>

  </build>

</project>

 

注意到packaging元素,告诉maven构建一个WAR,尝试以下命令:

mvn clean package

可以看到target/my-webapp.war被构建。

 

2.19.如何一次构建多个项目?

Maven2.0支持多模块的概念。这里,我们显示如何构建上述的war,同时包含以前步骤的jar文件。

首先,在两个项目的父目录里,需要创建一个新的父pom.xml文件,如下所示:

+- pom.xml

+- my-app

| +- pom.xml

+- my-webapp

| +- pom.xml

 

pom.xml文件包含如下内容:

<project>

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mycompany.app</groupId>

  <version>1.0-SNAPSHOT</version>

  <artifactId>app</artifactId>

  <packaging>pom</packaging>

  <modules>

    <module>my-app</module>

    <module>my-webapp</module>

  </modules>

</project>

Webapp项目需要添加对jar项目的依赖项:

  .

  .

  .

  <dependencies>

    <dependency>

      <groupId>com.mycompany.app</groupId>

      <artifactId>my-app</artifactId>

      <version>1.0-SNAPSHOT</version>

    </dependency>

    .

    .

    .

最后,同时在两个模块的pom.xml文件中加入以下内容:

<project>

  <parent>

    <groupId>com.mycompany.app</groupId>

    <artifactId>app</artifactId>

    <version>1.0-SNAPSHOT</version>

  </parent>

  .

  .

  .

现在,在顶层目录,运行以下命令:

mvn clean install

这时,创建了my-webapp/target/my-webapp.war,它包含了my-app-1.0-SNAPSHOT.jar

$ jar tvf my-webapp/target/my-webapp-1.0-SNAPSHOT.war

   0 Fri Jun 24 10:59:56 EST 2005 META-INF/

 222 Fri Jun 24 10:59:54 EST 2005 META-INF/MANIFEST.MF

   0 Fri Jun 24 10:59:56 EST 2005 META-INF/maven/

   0 Fri Jun 24 10:59:56 EST 2005 META-INF/maven/com.mycompany.app/

   0 Fri Jun 24 10:59:56 EST 2005 META-INF/maven/com.mycompany.app/my-webapp/

3239 Fri Jun 24 10:59:56 EST 2005 META-INF/maven/com.mycompany.app/my-webapp/pom.xml

   0 Fri Jun 24 10:59:56 EST 2005 WEB-INF/

 215 Fri Jun 24 10:59:56 EST 2005 WEB-INF/web.xml

 123 Fri Jun 24 10:59:56 EST 2005 META-INF/maven/com.mycompany.app/my-webapp/pom.properties

  52 Fri Jun 24 10:59:56 EST 2005 index.jsp

   0 Fri Jun 24 10:59:56 EST 2005 WEB-INF/lib/

2713 Fri Jun 24 10:59:56 EST 2005 WEB-INF/lib/my-app-1.0-SNAPSHOT.jar

这是怎样工作的呢?首先,创建了父POM(名为app),它包含了一组模块,这就告诉maven,对这组模块项目进行操作(可以通过使用—non-recursive命令行选项覆盖此行为)。

 

接着,我们告诉WAR项目需要my-app jar文件,这样,使此jar出现在了classpath上,war项目中的所有代码都可以使用它。Jar项目总是在war项目之前构建,并且告诉war插件在lib目录包含那个jar文件。

 

你可能注意到junit-3.8.1.jar也是一个依赖项,但却没有出现war的lib目录,因为scope元素被设为test,只有测试才需要junit-3.8.1.jar。

 

最后,就是在模块pom.xml文件中包含parent定义。这和maven1.0中的extend元素不同:确保POM总是能够定位,即使项目和其父分布在不同地点,只要可以通过库查找。

 

 

你可以在顶层目录,通过以下命令来产生IDEA的工作空间:

mvn idea:idea

 

MAVEN指南(5)

2.10.如何把jar文件部署到自己的远程库?

<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.mycompany.app</groupId>

  <artifactId>my-app</artifactId>

  <packaging>jar</packaging>

  <version>1.0-SNAPSHOT</version>

  <name>Maven Quick Start Archetype</name>

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

  <dependencies>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>3.8.1</version>

      <scope>test</scope>

    </dependency>

    <dependency>

      <groupId>org.apache.codehaus.plexus</groupId>

      <artifactId>plexus-utils</artifactId>

      <version>1.0.4</version>

    </dependency>

  </dependencies>

  <build>

    <filters>

      <filter>src/main/filters/filters.properties</filter>

    </filters>

    <resources>

      <resource>

        <directory>src/main/resources</directory>

        <filtering>true</filtering>

      </resource>

    </resources>

  </build>

  <!--

   |

   |

   |

   -->

  <distributionManagement>

    <repository>

      <id>mycompany-repository</id>

      <name>MyCompany Repository</name>

      <url>scp://repository.mycompany.com/repository/maven2</url>

    </repository>

  </distributionManagement>

</project>

 

 

<settings>

  .

  .

  <servers>

    <server>

      <id>mycompany-repository</id>

      <username>jvanzyl</username>

      <!-- Default value is ~/.ssh/id_dsa -->

      <privateKey>/path/to/identity</privateKey> (default is ~/.ssh/id_dsa)

      <passphrase>my_key_passphrase</passphrase>

    </server>

  </servers>

  .

  .

</settings>

 

 

2.11.如何创建文档?

为了快速启用maven的文档系统,在项目尚不存在的情况下,可以使用archetype机制为你产生站点。使用如下命令:

mvn archetype:create -DgroupId=com.mycompany.app

-DartifactId=my-app -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-site

 

 

如果你看一下产生的项目目录结构,可以看到如下内容:

my-app

|-- pom.xml

`-- src

    |-- main

    |   |-- filters

    |   |   `-- filters.properties

    |   |-- java

    |   |   `-- com

    |   |       `-- mycompany

    |   |           `-- app

    |   |               `-- App.java

    |   `-- resources

    |       `-- META-INF

    |           |-- application.properties

    |           `-- application.properties~

    |-- site

    |   |-- apt

    |   |   |-- format.apt

    |   |   `-- index.apt

    |   |-- fml

    |   |   `-- faq.fml

    |   |-- fr

    |   |   |-- apt

    |   |   |   |-- format.apt

    |   |   |   `-- index.apt

    |   |   |-- fml

    |   |   |   `-- faq.fml

    |   |   `-- xdoc

    |   |       `-- xdoc.xml

    |   |-- site.xml

    |   |-- site_fr.xml

    |   `-- xdoc

    |       `-- xdoc.xml

    `-- test

        |-- java

        |   `-- com

        |       `-- mycompany

        |           `-- app

        |               `-- AppTest.java

        `-- resources

            `-- test.properties

 

注意到现在多了一个${basedir}/src/site目录,里面有站点描述符和所支持的各种文档类型。

 

Xdoc格式,和maven1.0中一样,只是把navigation.xml杯替换成了站点表述符。

APT格式,基本为文本格式,类似于wiki格式,可以用来编写简单的,结构画的文档。请参考《APT格式》。

 

FML格式,FAQ格式,在maven1.0中也有。

 

还有其它格式。

 

Maven支持几种输出格式,但对于2.0,只支持XHTML格式。

 

2.12.如何创建站点?

使用如下命令:

mvn site

产生的站点位于target/site/目录

 

2.13.如何部署站点?

为了部署站点,必须首先在pom.xml文件中定义一个分发位置。

<distributionManagement>

  <site>

    <id>website</id>

    <url>scp://www.mycompany.com/www/docs/project/</url>

  </site>

</distributionManagement>

 

Id元素用于标识库,所以在settings.xml文件中,可以和其它库一样附加安全凭据。Url给出了部署位置。当前,仅支持SSH,上面的配置将把站点拷贝到www.mycompany.com站点的/www/docs/project目录。

 

部署站点使用site-deploy goal。

mvn site-deploy

 

 

 

2.14.创建站点描述符

site.xml文件用来描述站点的布局。

示例:

<?xml version="1.0" encoding="ISO-8859-1"?>

<project name="Maven">

  <bannerLeft>

    <name>Maven</name>

    <src>http://maven.apache.org/images/apache-maven-project.png</src>

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

  </bannerLeft>

  <bannerRight>

    <src>http://maven.apache.org/images/maven-small.gif</src>

  </bannerRight>

  <body>

    <links>

      <item name="Apache" href="http://www.apache.org/" />

      <item name="Maven 1.0" href="http://maven.apache.org/"/>

      <item name="Maven 2" href="http://maven.apache.org/maven2/"/>

    </links>

 

    <menu name="Maven 2.0">

      <item name="Introduction" href="index.html"/>

      <item name="Download" href="download.html"/>

      <item name="Release Notes" href="release-notes.html" />

      <item name="General Information" href="about.html"/>

      <item name="For Maven 1.0 Users" href="maven1.html"/>

      <item name="Road Map" href="roadmap.html" />

    </menu>

 

    ${reports}

 

    ...

  </body>

</project>

 

里面的${reports}将会被配置的报表替换。

 

2.15.添加额外的资源

可以在站点的资源目录包含任意的资源,比如增加CSS

+- src/

   +- site/

      +- resources/

         +- css/

         |  +- site.css

         |

         +- images/

            +- pic1.jpg

文件site.css将被用于默认的XHTML输出,可以用它来调整maven默认的样式。

 

文件pic1.jpg可以在站点中的任何页面引用。

2.8.如何过滤资源文件?

有时候,资源文件中的某些值在构建时才确定,为了在maven中做到这点,使用${<property name>}语法将属性引用放到资源文件中。这个属性的值可以在你的pom.xml文件中定义,或者在用户的settings.xml文件中,或者在外部属性文件中,或者系统属性。

为了让maven过滤资源,只要在pom.xml的resource节将filtering设为true。

<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.mycompany.app</groupId>

  <artifactId>my-app</artifactId>

  <packaging>jar</packaging>

  <version>1.0-SNAPSHOT</version>

  <name>Maven Quick Start Archetype</name>

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

  <dependencies>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>3.8.1</version>

      <scope>test</scope>

    </dependency>

  </dependencies>

  <build>

    <resources>

      <resource>

        <directory>src/main/resources</directory>

        <filtering>true</filtering>

      </resource>

    </resources>

  </build>

</project>

 

注意到我们加入了以前没有的build,resoucses,resource元素,另外,我们必须明确的指定资源的位置,这里为src/main/resources目录,所有这些以前都是作为默认值的,但因为filtering的默认值为false,我们必须把这些都加入到pom.xml中,覆盖默认设置。

 

为了引用在pom.xml中定义的属性,属性名称使用XML元素来定义值,“pom”允许作为项目根元素的别名。所以${pom.name}指向项目的名称,而${pom.version}指向项目的版本,${pom.build.finalName}指向最后创建的打包文件的名称,等等。注意,某些POM元素有默认值,所以没有必要在pom.xml文件中显式定义。同样的,在用户settings.xml文件中的值可以通过以“settings”开头的属性名字来引用(比如,${settings.localRepository}指向用户本地库的路径)。

 

为了继续我们的例子,在application.properties文件中加入一对属性

# application.properties

application.name=${pom.name}

application.version=${pom.version}

 

在这里,执行以下命令(process-resources是构建生命周期中的一个拷贝、过滤资源的阶段)

mvn process_resources

 

为了引用外部文件中定义的属性,必须在pom.xml文件中增加对此外部文件的引用。首先,创建一个外部属性文件,假设为src/main/filters/filter.properties。

# filter.properties

my.filter.value=hello!

 

 

接下来,我们在pom.xml文件中增加对此外部资源文件的引用:

 

<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.mycompany.app</groupId>

  <artifactId>my-app</artifactId>

  <packaging>jar</packaging>

  <version>1.0-SNAPSHOT</version>

  <name>Maven Quick Start Archetype</name>

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

  <dependencies>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>3.8.1</version>

      <scope>test</scope>

    </dependency>

  </dependencies>

  <build>

    <filters>

      <filter>src/main/filters/filter.properties</filter>

    </filters>

    <resources>

      <resource>

        <directory>src/main/resources</directory>

        <filtering>true</filtering>

      </resource>

    </resources>

  </build>

</project>

 

然后,如果我们在application.properties文件中加入属性引用

# application.properties

application.name=${pom.name}

application.version=${pom.version}

message=${my.filter.value}

 

再次执行mvn processs-resources命令,将会把新属性的值放入application.properties文件中。作为在外部文件中定义my.filter.value属性的替代,你可以在pom.xml中定义properties节,可以获得同样的效果。

<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.mycompany.app</groupId>

  <artifactId>my-app</artifactId>

  <packaging>jar</packaging>

  <version>1.0-SNAPSHOT</version>

  <name>Maven Quick Start Archetype</name>

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

  <dependencies>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>3.8.1</version>

      <scope>test</scope>

    </dependency>

  </dependencies>

  <build>

    <resources>

      <resource>

        <directory>src/main/resources</directory>

        <filtering>true</filtering>

      </resource>

    </resources>

  </build>

  <properties>

    <my.filter.value>hello</my.filter.value>

  </properties>

</project>

 

资源过滤同样可以从系统属性中获取值;比如java的内建属性(java.version或者user.home)或者命令行定义的属性(使用java –D参数)。

# application.properties

java.version=${java.version}

command.line.prop=${command.line.prop}

 

 

现在,当执行以下命令后,application.properties文件将包含系统属性的值:

mvn process-resources "-Dcommand.line.prop=hello again"

 

 

2.9.如何使用外部依赖项?

你可能已经注意到在示例中的POM中已经使用了dependencies元素。更全面的介绍,请参考《依赖项管理介绍》。

 

Pom.xml文件中的dependencies节列出了所有构建时需要的外部依赖项(不管是编译时、测试时、运行时)。示例中,目前只用到了Junit。

<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.mycompany.app</groupId>

  <artifactId>my-app</artifactId>

  <packaging>jar</packaging>

  <version>1.0-SNAPSHOT</version>

  <name>Maven Quick Start Archetype</name>

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

  <dependencies>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>3.8.1</version>

      <scope>test</scope>

    </dependency>

  </dependencies>

</project>

 

 

对于每个外部依赖项,必须定义4样东西:goupId,artifactId,version以及scope。GroupId,artifactId以及version元素没有什么特别,scope元素指明项目如何使用此依赖项,其值可以是compile,test以及runtime。要想获得可以指定的依赖项的更多信息,请参考《项目描述符参考》。

 

对于更多的依赖项机制,请参考《依赖项管理》。

 

通过此dependency信息,maven可以在构建项目时引用此依赖项。那么,maven从哪儿引用此依赖项呢?Maven会去本地库(默认位置:~/.m2/repository)中查找所有依赖项。在上一节,我们把我们项目的产品(my-app-1.0-SNAPSHOT.jar)安装到了本地库中,一旦安装完成,其它项目就可以把此jar文件作为一个依赖项引用:

<project>

  <groupId>com.mycompany.app</groupId>

  <artifactId>my-other-app</artifactId>

  ...

  <dependencies>

    ...

    <dependency>

      <groupId>com.mycompany.app</groupId>

      <artifactId>my-app</artifactId>

      <version>1.0-SNAPSHOT</version>

      <scope>compile</scope>

    </dependency>

  </dependencies>

</project>

 

Maven发现项目引用的依赖项在本地库中不存在时,自动把它从远程库中下载到本地库中。你可能已经注意到,当你构建第一个项目时,Maven下载了一些包(这些依赖项与构建项目时用到的插件有关)。默认情况下,Maven使用的远程库在http://www.ibiblio.org/maven2。当然,你也可以自己设置远程库(公司的中心库),作为ibiblio的替代或者补充。关于库德更多信息,请参考《库介绍》。

 

让我们增加一个依赖项到我们的项目中,假设我们需要日志功能,必须增加log4j作为依赖项。首先,我们需要知道log4j的groupId,artifactId以及version。

 

我们可以通过浏览ibiblio来查看它,或者使用Google来搜索“site:www.ibiblio.org maven2 log4j”。这个搜索会显示一个称为“/maven2/log4j/log4j”或者“/pub/packages/maven2/log4j/log4j”的目录。在那个目录中,有一个名为maven-metadata.xml的文件,这里是那个文件的内容:

<metadata>

  <groupId>log4j</groupId>

  <artifactId>log4j</artifactId>

  <version>1.1.3</version>

  <versioning>

    <versions>

      <version>1.1.3</version>

      <version>1.2.4</version>

      <version>1.2.5</version>

      <version>1.2.6</version>

      <version>1.2.7</version>

      <version>1.2.8</version>

      <version>1.2.11</version>

      <version>1.2.9</version>

      <version>1.2.12</version>

    </versions>

  </versioning>

</metadata>

 

我们从这个文件查看到log4j的groupId=log4j,artifactId=log4j。同时,我们可以看到有多个不同的版本可以选择。现在,我们仅仅使用最新版本,1.2.12(某些maven-metadata.xml文件会指定哪个版本为当前版本)。除了maven-metadata.xml文件,我们还可以看到对应版本的一组目录,里面放了相应版本的log4j包,以及pom.xml(指明log4j的依赖项),还有另一个maven-metadata.xml文件和各个文件的md5文件。

 

现在,我们知道了所需的信息,可以在我们的pom.xml增加依赖项了,如下所示:

 

<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.mycompany.app</groupId>

  <artifactId>my-app</artifactId>

  <packaging>jar</packaging>

  <version>1.0-SNAPSHOT</version>

  <name>Maven Quick Start Archetype</name>

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

  <dependencies>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>3.8.1</version>

      <scope>test</scope>

    </dependency>

    <dependency>

      <groupId>log4j</groupId>

      <artifactId>log4j</artifactId>

      <version>1.2.12</version>

      <scope>compile</scope>

    </dependency>

  </dependencies>

</project>

 

 

现在执行编译项目(mvn compile)时,可以看到maven下载log4j。

MAVEN指南(3)

2.5.如何创建JAR以及把它安装到本地库中?

执行以下命令:

mvn package

如果你看以下项目的POM,你可以注意到packaging元素被设置为jar。这让maven知道从以上命令产生一个jar文件。在${basedir}/target目录下,产生了一个jar文件。

 

现在,你想将产生的jar文件安装到你的本地库中(~/.m2/repository是默认的位置)。想了解库的更多信息,请参考《库介绍》。执行以下命令,就能安装到本地库:

mvn install

 

执行过程中会产生如下输出:

[INFO] ----------------------------------------------------------------------------

[INFO] Building Maven Quick Start Archetype

[INFO]    task-segment: [install]

[INFO] ----------------------------------------------------------------------------

[INFO] [resources:resources]

[INFO] [compiler:compile]

Compiling 1 source file to <dir>/my-app/target/classes

[INFO] [resources:testResources]

[INFO] [compiler:testCompile]

Compiling 1 source file to <dir>/my-app/target/test-classes

[INFO] [surefire:test]

[INFO] Setting reports dir: <dir>/my-app/target/surefire-reports

 

-------------------------------------------------------

 T E S T S

-------------------------------------------------------

[surefire] Running com.mycompany.app.AppTest

[surefire] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.001 sec

 

Results :

[surefire] Tests run: 1, Failures: 0, Errors: 0

 

[INFO] [jar:jar]

[INFO] Building jar: <dir>/my-app/target/my-app-1.0-SNAPSHOT.jar

[INFO] [install:install]

[INFO] Installing <dir>/my-app/target/my-app-1.0-SNAPSHOT.jar to <local-repository>/com/mycompany/app/my-app/1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.jar

[INFO] ----------------------------------------------------------------------------

[INFO] BUILD SUCCESSFUL

[INFO] ----------------------------------------------------------------------------

[INFO] Total time: 5 seconds

[INFO] Finished at: Tue Oct 04 13:20:32 GMT-05:00 2005

[INFO] Final Memory: 3M/8M

[INFO] ----------------------------------------------------------------------------

 

 

注意执行测试的surefire插件,它会按命名规则查找所有的测试文件,默认情况下,测试文件包括:

Ø       **/*Test.java

Ø       **/Test*.java

Ø       **/*TestCase.jave

默认情况下,被排除的:

Ø       **/Abstract*Test.java

Ø       **/Abstract*TestCase.jave

 

到目前为止,已经经历了安装、构建、测试、打包、安装典型的maven项目等步骤。这可能是大多数项目使用maven需要经历的步骤,而驱动的项目模型文件只有18行内容,对比典型的build文件,它的行数比pom多的多。

 

不需任何附加工作maven依靠此POM就可以为你的项目产生一个网站,你只须定制你的maven网站,如果时间紧迫,你只须执行以下命令:

mvn site

 

还有许多goals可以执行,比如

mvn clean

此命令删除target目录。

 

可能你需要为你的项目产生一个IntelliJ IDEA 描述符,执行以下命令:

mvn idea:idea

 

2.6.如何使用插件?

Maven 1.0用户注意:在Maven 1.0中,你可能必须加入一些譬如preGoal到maven.xml中,以及project.properties的一些入口中。这里有些不同,比如,我们配置java编译器,以允许编译jdk 5.0的源码,这只要简单的在你的POM中加入:

.

<build>

  <plugins>

    <plugin>

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

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

      <configuration>

        <source>1.5</source>

        <target>1.5</target>

      </configuration>

    </plugin>

  </plugins>

</build>

.

.

 

 

maven2.0中,所有的插件看起来更像依赖项,并且某些方面确实就是依赖项。这个插件(包含特定的版本,如果指定的话,否则使用最新版本)将被自动下载并使用。

 

Configuration元素用来对编译插件的每个goal提供参数。具体请参考《插件列表》、《构建生命周期介绍》。

 

2.7.如何在jar文件中加入资源?

在示例应用程序中,增加目录${basedir}/src/main/resources,我们把资源文件放入此目录。任何在${basedir}/src/main/resources目录中的文件和目录都将被打包到jar文件中。

my-app

|-- pom.xml

`-- src

    |-- main

    |   |-- java

    |   |   `-- com

    |   |       `-- mycompany

    |   |           `-- app

    |   |               `-- App.java

    |   `-- resources

    |       `-- META-INF

    |           `-- application.properties

    `-- test

        `-- java

            `-- com

                `-- mycompany

                    `-- app

                        `-- AppTest.java

 

 

在这个例子中,我们在${basedir}/src/main/resources目录下放了一个META-INF目录并在META-INF目录中放了一个application.properties文件。如果你将jar文件解压,可以看到以下内容:

|-- META-INF

|   |-- MANIFEST.MF

|   |-- application.properties

|   `-- maven

|       `-- com.mycompany.app

|           `-- my-app

|               |-- pom.properties

|               `-- pom.xml

`-- com

    `-- mycompany

        `-- app

            `-- App.class

 

 

就像你看到的那样,${basedir}/src/main/resources中的内容在jar文件的起始位置,我们的application.properties文件在META-INF目录下。同时,一些其他文件如META-INF/MANIFEST.MF,以及pom.xml和pom.properties文件也在其中,对于maven产生的jar文件来说,这是maven的标准行为。你可以自行创建自己的manifest,不过如果你不指定,maven会产生一个默认的。由于pom.xml和pom.properties文件都被打包到jar文件中,所以maven产生的产品都是自描述的,一个简单的应用就是获得应用程序版本。打开POM文件可能需要一些maven的工具,但打开属性文件可以使用标准的java API来做到,属性文件pom.properties如下所示:

#Generated by Maven

#Tue Oct 04 15:43:21 GMT-05:00 2005

version=1.0-SNAPSHOT

groupId=com.mycompany.app

artifactId=my-app

 

 

为了单元测试,可能需要把资源加入到classpath,你可以遵照同样的模式:

my-app

|-- pom.xml

`-- src

    |-- main

    |   |-- java

    |   |   `-- com

    |   |       `-- mycompany

    |   |           `-- app

    |   |               `-- App.java

    |   `-- resources

    |       `-- META-INF

    |           |-- application.properties

    `-- test

        |-- java

        |   `-- com

        |       `-- mycompany

        |           `-- app

        |               `-- AppTest.java

        `-- resources

            `-- test.properties

 

那么在测试代码中,只须使用如下代码就可访问这些资源:

...

 

// Retrieve resource

InputStream is = getClass().getResourceAsStream( "/test.properties" );

 

// Do something with the resource

 

...

 

 

 

开始使用Maven

2.1.Maven是什么?

Maven本质上是一个项目管理工具,Maven提供一下特性来提供项目管理:

Ø       构建

Ø       文档

Ø       报表

Ø       依赖(相关性)

Ø       SCMs

Ø       发布

Ø       分发

Maven不只是一个项目构建工具,并且也不只是ant的一个替代品。Maven是和ant完全不同的一个东西。Ant仅仅是一个简单的跨平台的构建工具,而Maven是一个模式应用,为了达到可视性、复用性、维护性和易于理解的一个下部构造(Maven is about the application of patterns in order to acheive an infrastructure which displays the characteristics of visibility, reusability, maintainability, and comprehensibility.)。

 

2.2.如何开始你的第一个Maven项目

这里要使用Maven的achetype机制,一个achetype被定义成一个原始的模式或者模型。在Maven里,archetype是一个项目的模板,通过这个模板,再加上一些用户的输入就产生了一个可以工作的、符合用户需要的Maven项目。

 

下面开始创建第一个项目,执行一下命令行:

Mvn archetype:create –DgroupId=com.mycompany.app –DartifactId=my-app

一旦执行了以上命令,你会发现maven为这个新项目创建了一个名为my-app的目录,同时此目录中有一个pom.xml文件,文件内容如下所示:

 

<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.mycompany.app</groupId>

  <artifactId>my-app</artifactId>

  <packaging>jar</packaging>

  <version>1.0-SNAPSHOT</version>

  <name>Maven Quick Start Archetype</name>

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

  <dependencies>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>3.8.1</version>

      <scope>test</scope>

    </dependency>

  </dependencies>

</project>

 

Pom.xml包含项目对象模型(POM)。POM是Maven的基本单元,记住,Maven是以项目为中心的,所有的东西都围绕项目展开。简而言之,POM包含项目的所有重要信息,并且本质上提供了一站式的项目信息。关于pom的详细介绍,请参考《POM介绍》。

下面是pom中的一些重要元素:

Ø       projectpom.xml文件中的顶层元素;

Ø       modelVersion:指明POM使用的对象模型的版本。这个值很少改动。

Ø       groupId:指明创建项目的组织或者小组的唯一标识。GroupId是项目的关键标识,典型的,此标识以组织的完全限定名来定义。比如,org.apache.maven.plugins是所有Maven插件项目指定的groupId。

Ø       artifactId:指明此项目产生的主要产品的基本名称。项目的主要产品通常为一个JAR文件。第二,象源代码包通常使用artifactId作为最后名称的一部分。典型的产品名称使用这个格式:<artifactId>-<version>.<extension>(比如:myapp-1.0.jar)。

Ø       version:项目产品的版本号。Maven帮助你管理版本,可以经常看到SNAPSHOT这个版本,表明项目处于开发阶段。

Ø       name:项目的显示名称,通常用于maven产生的文档中。

Ø       url:指定项目站点,通常用于maven产生的文档中。

Ø       description:描述此项目,通常用于maven产生的文档中。

 

POM中所有元素的说明,请参考《POM参考》。

 

产生了第一个项目的archetype后,你可以发现maven为你创建了如下的目录结构:

my-app

|-- pom.xml

`-- src

    |-- main

    |   `-- java

    |       `-- com

    |           `-- mycompany

    |               `-- app

    |                   `-- App.java

    `-- test

        `-- java

            `-- com

                `-- mycompany

                    `-- app

                        `-- AppTest.java

 

 

 

archetype产生的项目有一个POM,一个源代码树,以及一个测试代码树,这是maven项目的标准布局(应用程序源码位于${basedir}/src/main/java,而测试源码位于${basedir}/src/test/java,这里${basedir}代表包含pom.xml的根目录)。

 

如果你想改变这个默认布局,请参考《标准目录布局介绍》。

 

2.3.如何编译应用程序源码?

执行一下命令编译应用程序源码:

mvn compile

当执行此命令的时候,可以看到以下信息:

[INFO] ----------------------------------------------------------------------------

[INFO] Building Maven Quick Start Archetype

[INFO]    task-segment: [compile]

[INFO] ----------------------------------------------------------------------------

[INFO] artifact org.apache.maven.plugins:maven-resources-plugin: checking for updates from central

...

[INFO] artifact org.apache.maven.plugins:maven-compiler-plugin: checking for updates from central

...

[INFO] [resources:resources]

...

[INFO] [compiler:compile]

Compiling 1 source file to <dir>/my-app/target/classes

[INFO] ----------------------------------------------------------------------------

[INFO] BUILD SUCCESSFUL

[INFO] ----------------------------------------------------------------------------

[INFO] Total time: 3 minutes 54 seconds

[INFO] Finished at: Fri Sep 23 15:48:34 GMT-05:00 2005

[INFO] Final Memory: 2M/6M

[INFO] ----------------------------------------------------------------------------

 

 

第一次执行此命令(或者其他命令)时,maven需要下载所有插件以及满足此命令的依赖项。对于一个干净的maven安装版本,这需要花费一点时间。如果你再次运行此命令,maven就无需下载新的插件和依赖项了。

 

就像你从输出中看到的那样,编译过的类文件被存放到${basedir}/target/classes目录中,这个也是Maven标准的目录布局。

 

2.4.如何编译测试源码和运行单元测试?

运行一下命令:

mvn test

 

运行过程中会有以下输出:

[INFO] ----------------------------------------------------------------------------

[INFO] Building Maven Quick Start Archetype

[INFO]    task-segment: [test]

[INFO] ----------------------------------------------------------------------------

[INFO] artifact org.apache.maven.plugins:maven-surefire-plugin: checking for updates from central

...

[INFO] [resources:resources]

[INFO] [compiler:compile]

[INFO] Nothing to compile - all classes are up to date

[INFO] [resources:testResources]

[INFO] [compiler:testCompile]

Compiling 1 source file to C:/Test/Maven2/test/my-app/target/test-classes

...

[INFO] [surefire:test]

[INFO] Setting reports dir: C:/Test/Maven2/test/my-app/target/surefire-reports

 

-------------------------------------------------------

 T E S T S

-------------------------------------------------------

[surefire] Running com.mycompany.app.AppTest

[surefire] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0 sec

 

Results :

[surefire] Tests run: 1, Failures: 0, Errors: 0

 

[INFO] ----------------------------------------------------------------------------

[INFO] BUILD SUCCESSFUL

[INFO] ----------------------------------------------------------------------------

[INFO] Total time: 15 seconds

[INFO] Finished at: Thu Oct 06 08:12:17 MDT 2005

[INFO] Final Memory: 2M/8M

[INFO] ----------------------------------------------------------------------------

 

输出中,有些内容值得注意:

Ø       这个时候,maven会下载更多的依赖项。这些依赖项和插件是运行测试时所需要的。

Ø       在编译和执行测试之前,maven按需编译main代码

 

如果你只须编译测试代码(无需运行测试),执行以下代码:

mvn test-compile

 

MAVEN指南(1)

1.下载并安装

目前的版本为2.0.2,从http://maven.apache.org下载合适的分发包,

 

系统需求:

Jdk

1.4+

内存

 

磁盘空间

大概100M

操作系统

 

 

 

安装:

Windows2000/XP下:

Ø       解压maven-2.0.2-bin.zip到相应的安装目录,以下假设目录为C:/Program Files/Apache Software Foundation/maven-2.0.2。

Ø       设置环境变量M2_HOME= C:/Program Files/Apache Software Foundation/maven-2.0.2,同时,将MAVEN的bin路径加到path环境变量中,PATH=%PATH%;%M2_HOME%/bin

Ø       运行mvn --version,来验证安装。

 

Unix系列操作系统下(Linux, Solaris and Mac OS X):

Ø       将分发包解压到安装路径下,假设为/usr/local/maven-2.0.2

Ø       bin目录加入到path环境变量,比如:export PATH=/usr/local/maven-2.0.2y/bin:$PATH

Ø       运行mvn –version来验证安装。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值