Maven2介绍

 1. Maven2如何安装

下载软件包, 解压缩到目录xx,然后设置环境变量
M2_HOME 为xx,最后设置一下 PATH 路径为 $M2_HOME/bin , 即可

2. Maven2的配置文件

直接改
$M2_HOME/conf/settings.xml 或者改 $USER_HOME/.m2/settings.xml (推 荐)

3.Maven2的阶段

Maven Phases

Although hardly a comprehensive list, these are the most common default lifecycle phases executed.

validate : validate the project is correct and all necessary information is available
compile : compile the source code of the project
test : test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
package : take the compiled code and package it in its distributable format, such as a JAR.
integration-test : process and deploy the package if necessary into an environment where integration tests can be run
verify : run any checks to verify the package is valid and meets quality criteria
install : install the package into the local repository, for use as a dependency in other projects locally
deploy : done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

There are two other Maven lifecycles of note beyond the default list above. They are

clean : cleans up artifacts created by prior builds
site : generates site documentation for this project

4.将工程部署到远程仓库

先在
pom.xml 中配置仓库的URL地址
<distributionManagement>
    <repository>
      <id>mycompany-repository</id>
      <name>MyCompany Repository</name>
      <url>scp://repository.mycompany.com/repository/maven2</url>
    </repository>
</distributionManagement>

然后再去
settings.xml 中配置验证信息
<settings 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/xsd/settings-1.0.0.xsd">
...
<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>


5.复合工程

父工程中设置如下
<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/xsd/maven-4.0.0.xsd">
<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>


子工程中设置如下

<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/xsd/maven-4.0.0.xsd">
<parent>
    <groupId>com.mycompany.app</groupId>
    <artifactId>app</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

...

6.LifeCycle,Phases和Goals的关系

内建有三个LifeCycle,分别是
default , clean site

LifeCycle由Phases组成 , 如default生命周期由以下phases组成:

validate, compile, test, package, integration-test, verify, install, deploy

而,
Phase则由Goals组成 , Goal绑定到零个或者多个Phase, 如果绑定了,则在此Phase执行的时候,自动执行,否则需要单独调用。


7.为工程设置生命周期


1 . 利用<packaging>标签设置基本的生命周期, 值可以是jar(默认), war, ear和pom.


如,jar的phases和goals对应如下

process-resources resources:resources
compile compiler:compile
process-test-resources resources:testResources
test-compile compiler:testCompile
test surefire:test
package jar:jar
install install:install
deploy deploy:deploy


2 .如何将goals添加到phases?

通过在pom.xml中的build标签下的plugins标签的子标签plugin中配置,进行绑定。


<plugin>

   <groupId>com.mycompany.example</groupId>
   <artifactId>maven-touch-plugin</artifactId>
   <version>1.0</version>
   <executions>
   
<execution>
      
<phase>process-test-resources</phase> //如果不指定,则进行默认的绑定
       <goals>
         <goal>timestamp</goal>
       </goals>

    
</execution>
   </executions>
</plugin>

1.什么是POM

POM,即Project Object Model, 通过pom.xml文件配置Maven2,然后Maven2根据此配置执行。


2.Super POM


Super POM是Maven2的默认pom.xml文件, 无需显示声明, 所有的pom.xml文件都默认继承此文件。

2.0.x的默认Super POM文件如下:

<project>
<modelVersion>4.0.0</modelVersion>
<name>Maven Default Project</name>

<repositories>
    <repository>
      <id>central</id>
      <name>Maven Repository Switchboard</name>
      <layout>default</layout>
      <url>http://repo1.maven.org/maven2</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <name>Maven Plugin Repository</name>
      <url>http://repo1.maven.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
    </pluginRepository>
</pluginRepositories>

<build>
    <directory>${project.basedir}/target</directory>
    <outputDirectory>${project.build.directory}/classes</outputDirectory>
    <finalName>${project.artifactId}-${project.version}</finalName>
    <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
    <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
    <scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
    <resources>
      <resource>
        <directory>${project.basedir}/src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>${project.basedir}/src/test/resources</directory>
      </testResource>
    </testResources>
    <pluginManagement>
       <plugins>
         <plugin>
           <artifactId>maven-antrun-plugin</artifactId>
           <version>1.1</version>
         </plugin>      
         <plugin>
           <artifactId>maven-assembly-plugin</artifactId>
           <version>2.2-beta-2</version>
         </plugin>
         <plugin>
           <artifactId>maven-clean-plugin</artifactId>
           <version>2.2</version>
         </plugin>
         <plugin>
           <artifactId>maven-compiler-plugin</artifactId>
           <version>2.0.2</version>
         </plugin>
         <plugin>
           <artifactId>maven-dependency-plugin</artifactId>
           <version>2.0</version>
         </plugin>
         <plugin>
           <artifactId>maven-deploy-plugin</artifactId>
           <version>2.3</version>
         </plugin>
         <plugin>
           <artifactId>maven-ear-plugin</artifactId>
           <version>2.3.1</version>
         </plugin>
         <plugin>
           <artifactId>maven-ejb-plugin</artifactId>
           <version>2.1</version>
         </plugin>
         <plugin>
           <artifactId>maven-install-plugin</artifactId>
           <version>2.2</version>
         </plugin>
         <plugin>
           <artifactId>maven-jar-plugin</artifactId>
           <version>2.2</version>
         </plugin>
         <plugin>
           <artifactId>maven-javadoc-plugin</artifactId>
           <version>2.4</version>
         </plugin>
         <plugin>
           <artifactId>maven-plugin-plugin</artifactId>
           <version>2.4.2</version>
         </plugin>
         <plugin>
           <artifactId>maven-rar-plugin</artifactId>
           <version>2.2</version>
         </plugin>
         <plugin>               
           <artifactId>maven-release-plugin</artifactId>
           <version>2.0-beta-7</version>
         </plugin>
         <plugin>               
           <artifactId>maven-resources-plugin</artifactId>
           <version>2.2</version>
         </plugin>
         <plugin>
           <artifactId>maven-site-plugin</artifactId>
           <version>2.0-beta-7</version>
         </plugin>
         <plugin>
           <artifactId>maven-source-plugin</artifactId>
           <version>2.0.4</version>
         </plugin>        
         <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.4.3</version>
         </plugin>
         <plugin>
           <artifactId>maven-war-plugin</artifactId>
           <version>2.1-alpha-1</version>
         </plugin>
       </plugins>
     </pluginManagement>
</build>

<reporting>
    <outputDirectory>${project.build.directory}/site</outputDirectory>
</reporting>
<profiles>
    <profile>
      <id>release-profile</id>

      <activation>
        <property>
          <name>performRelease</name>
          <value>true</value>
        </property>
      </activation>

      <build>
        <plugins>
          <plugin>
            <inherited>true</inherited>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-sources</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-javadocs</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
            <configuration>
              <updateReleaseInfo>true</updateReleaseInfo>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
</profiles>

</project>

3.Minimal POM

最小的POM文件应该具有以下几个元素

  • project root
  • modelVersion - should be set to 4.0.0
  • groupId - the id of the project's group.
  • artifactId - the id of the artifact (project)
  • version - the version of the artifact under the specified group

4.Project Inheritance ( 注意,是继承而不是组合 )

POM中的以下元素会被合并:

    * dependencies
    * developers and contributors
    * plugin lists (including reports)
    * plugin executions with matching ids
    * plugin configuration
    * resources

示例:

pom.xml 文件为
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</project>


1.标准目录结构下的继承

.
|-- my-module
| `-- pom.xml
`-- pom.xml

my-module下的pom.xml文件为

<project>
<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId> //可以省略,省略则继承parent pom相同的groupId
<artifactId>my-module</artifactId>
<version>1</version>
</project>

2.非标准目录结构下的继承
.

 |-- my-module
| `-- pom.xml
`-- parent
`-- pom.xml

<project>
<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<relativePath>../parent/pom.xml </relativePath> //注意这里的区别
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>my-module</artifactId>
</project>


5. Project Aggresgation

改用组合,需要做以下工作
  • Change the parent POMs packaging to the value "pom" .
  • Specify in the parent POM the directories of its modules (children POMs)
即,通过在parent pom文件中添加以下元素,即可
<modules>
<module>relativePath/To/Module</module> //如果是标准目录结构,则直接指定子模块文件夹名
...
</modules>


6.Aggregation和Inheritance的区别


当构建一个工程时,需要执行几个子工程,那么就可以使用aggregation

当需要抽取多个工程中的相同配置的话,那么可以使用inheritance

实际使用中,往往两者相结合使用,方法如下:
  • Specify in every child POM who their parent POM is.
  • Change the parent POMs packaging to the value "pom" .
  • Specify in the parent POM the directories of its modules (children POMs)
profiles的目的是将配置文件中不能移植的部分,分割出来,根 据环境进行应用,从而达到可移植性

1.Maven2中的几类profile


Per Project
- 定义在pom.xml中
Per User
- 定义在Maven-settings中 (%USER_HOME%/.m2/settings.xml).
Global
- 定义在global maven-settings中 (%M2_HOME%/conf/settings.xml).
Profile descriptor
- 直接放到工程basedir下的profiles.xml中

2.如何激活这些配置


A profile can be triggered/activated in several ways:

  • Explicitly
    通过命令行参数
    -P 激活,后跟用逗号分割的profile
  • Through Maven settings
    通过在settings.xml中的
    <activeProfiles>
    <activeProfile>profile-1</activeProfile>
    </activeProfiles>

    激活
  • Based on environment variables
    根据环境变量/操作系统/JDK等激活,设置方式如下
    <profiles>
    <profile>
        <activation>
          <property>
            <name>debug</name>
          </property>

        </activation>
        ...
    </profile>
    </profiles>
  • OS settings
    <profiles>
    <profile>
        <activation>
          <os>
            <name>Windows XP</name>
          <family>Windows</family>
          <arch>x86</arch>
        <version>5.1.2600</version>
          </os>

        </activation>
        ...
    </profile>
    </profiles>
  • Present or missing files
    例:
    <profiles>
    <profile>
        <activation>
          <file>
            <missing>target/generated-sources/axistools/wsdl2java/org/apache/maven</missing>
          </file>

        </activation>
        ...
    </profile>
    </profiles>
默认激活 ,则是
<profiles>
<profile>
    <id>profile-1</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>

    ...
</profile>
</profiles>

不激活 ,则是
mvn -P !profile-id1, !profile-id2 ...

3.POM中的哪些部分可以通过profiles定义

Profiles in external files

从严格意义上来说,放在外部文件中的profiles是无法移植的,所以,只允许在外部文件中,如 settings.xml,profile.xmls指 定<repositories>,<pluginRepositories>和<properties>的区块

Profiles in POM

Profiles specified in the POM can modify the following POM elements:

  • <repositories>
  • <pluginRepositories>
  • <dependencies>
  • <plugins>
  • <properties> (not actually available in the main POM, but used behind the scenes)
  • <modules>
  • <reporting>
  • <dependencyManagement>
  • <distributionManagement>
  • a subset of the <build> element, which consists of:
    • <defaultGoal>
    • <resources>
    • <testResources>
    • <finalName>
POM elements outside <profiles>

不允许修改某些pom-profiles外的元素,因为运行时的修改,并不会发布到仓库之中


4.如何查看激活的Profiles

mvn help:active-profiles
谢原作者:

http://hi.baidu.com/dvdface/blog/item/26c453fb787216106c22eb5d.html

你可能感兴趣的:(Maven2介绍)