文章目录
  1. 1. Super POM
  2. 2. minimal POM
  3. 3. 项目继承
    1. 3.1. Example 1
      1. 3.1.1. 目录结构
      2. 3.1.2. com.mycompany.app:my-module:1’s POM
    2. 3.2. Example 2
      1. 3.2.1. 同级目录
      2. 3.2.2. Solution
  4. 4. 项目集成(Aggregation)
    1. 4.1. Examle 3
      1. 4.1.1. 目录结构
    2. 4.2. Example 4
      1. 4.2.1. 目录结构
      2. 4.2.2. POM
  5. 5. Inheritance vs Aggregation
    1. 5.1. 两者集成到一起
      1. 5.1.1. 目录结构
      2. 5.1.2. com.mycompany.app:my-app:1’s POM
      3. 5.1.3. com.mycompany.app:my-module:1’s POM

POM是maven的基本组成部分,包含项目的信息和配置信息。

Super POM

Super POM是maven默认的POM,所有的POM都会继承这个Super POM除非显示设置。下面给出Maven 2.1.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>
    <!-- TODO: MNG-3731 maven-plugin-tools-api < 2.4.4 expect this to be relative... -->
    <scriptSourceDirectory>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.3</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.4</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.5</version>
         </plugin>
         <plugin>
           <artifactId>maven-plugin-plugin</artifactId>
           <version>2.4.3</version>
         </plugin>
         <plugin>
           <artifactId>maven-rar-plugin</artifactId>
           <version>2.2</version>
         </plugin>        
         <plugin>                
           <artifactId>maven-release-plugin</artifactId>
           <version>2.0-beta-8</version>
         </plugin>
         <plugin>                
           <artifactId>maven-resources-plugin</artifactId>
           <version>2.3</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-2</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>

minimal POM

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1</version>
</project>

项目继承

POM中会merge的元素有:

  1. dependencies
  2. developers and contributors
  3. plugin lists (including reports)
  4. plugin executions with matching ids
  5. plugin configuration
  6. resources

Super POM就是一个继承的实例,同时我们还可以通过<parent>定义自己的项目继承。下面给出一些例子:

Example 1

这里我们重用一下上面的minimal POM,下面的例子我们再介绍另一个artifact com.mycompany.app:my-module:1

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-module</artifactId>
  <version>1</version>
</project>    

目录结构

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

my-module/pom.xml is the POM of com.mycompany.app:my-module:1 while pom.xml is the POM of com.mycompany.app:my-app:1

com.mycompany.app:my-module:1’s POM

<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>
  <artifactId>my-module</artifactId>
  <version>1</version>
</project>

如果想要和parent中artifact版本和groupid相同,我们可以选择去掉pom中的<version>和<groupId>,maven会自动继承相应的元素。

Example 2

同级目录

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

Solution

可以添加<relativePath>

<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>

项目集成(Aggregation)

Examle 3

目录结构

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

我们将my-module集成到my-app

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1</version>
  <packaging>pom</packaging>

  <modules>
    <module>my-module</module>
  </modules>
</project>

Example 4

目录结构

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

POM

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1</version>
  <packaging>pom</packaging>    
  <modules>
    <module>../my-module</module>
  </modules>
</project>

Inheritance vs Aggregation

如果有很多Projects也有很多相同的配置,我们使用inheritance

如果有一组需要一起构建或者运行的Projects,我们可以创建一个parent project,然后将这些project当做Modules在parent中声明,这时候只需要build parent就可以build所有modules啦。

两者集成到一起

目录结构

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

com.mycompany.app:my-app:1’s POM

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1</version>
  <packaging>pom</packaging>

  <modules>
    <module>../my-module</module>
  </modules>
</project>

com.mycompany.app:my-module:1’s POM

<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>
文章目录
  1. 1. Super POM
  2. 2. minimal POM
  3. 3. 项目继承
    1. 3.1. Example 1
      1. 3.1.1. 目录结构
      2. 3.1.2. com.mycompany.app:my-module:1’s POM
    2. 3.2. Example 2
      1. 3.2.1. 同级目录
      2. 3.2.2. Solution
  4. 4. 项目集成(Aggregation)
    1. 4.1. Examle 3
      1. 4.1.1. 目录结构
    2. 4.2. Example 4
      1. 4.2.1. 目录结构
      2. 4.2.2. POM
  5. 5. Inheritance vs Aggregation
    1. 5.1. 两者集成到一起
      1. 5.1.1. 目录结构
      2. 5.1.2. com.mycompany.app:my-app:1’s POM
      3. 5.1.3. com.mycompany.app:my-module:1’s POM