`

Maven 聚合与继承

 
阅读更多

一、聚合

1.新建account项目,POM文件为

<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.iteye.xujava</groupId>
	<artifactId>account</artifactId>
	<version>1.0.0</version>
	<packaging>pom</packaging>
	<name>account</name>

	<modules>
		<module>account-email</module>
		<module>account-persist</module>
	</modules>
</project>

 

2.在Eclipse中删除account-email,account-persist,将两个项目文件夹放在account文件夹下,在Eclipse重新引入这两个项目,修改account-persist中的service.properties内容为:persist.file=E:/mavenspace/account/account-persist/target/test-classes/persist-data.xml

 

3.运行mvn clean test

4.运行mvn clean install

 

二、继承

修改三个工和的POM文件

account的POM文件

<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.iteye.xujava</groupId>
	<artifactId>account</artifactId>
	<version>1.0.0</version>
	<packaging>pom</packaging>
	<name>Account</name>

	<modules>
		<module>account-email</module>
		<module>account-persist</module>
	</modules>

	<properties>
		<springframework.version>2.5.6</springframework.version>
		<junit.version>4.9</junit.version>
	</properties>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-core</artifactId>
				<version>${springframework.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-beans</artifactId>
				<version>${springframework.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context</artifactId>
				<version>${springframework.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context-support</artifactId>
				<version>${springframework.version}</version>
			</dependency>
			<dependency>
				<groupId>junit</groupId>
				<artifactId>junit</artifactId>
				<version>${junit.version}</version>
				<scope>test</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	
	<build>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-resources-plugin</artifactId>
					<configuration>
						<encoding>UTF-8</encoding>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
</project>

 

account-email的POM文件

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

	<parent>
		<groupId>com.iteye.xujava</groupId>
		<artifactId>account</artifactId>
		<version>1.0.0</version>
                <!-- 当父项目的POM在上层时,其实可以不写relativePath -->
		<relativePath>../pom.xml</relativePath>
	</parent>

	<artifactId>account-email</artifactId>
	<name>Account Email</name>

	<properties>
		<javax.mail.version>1.4.1</javax.mail.version>
		<greenmail.version>1.3.1b</greenmail.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
		</dependency>
		<dependency>
			<groupId>javax.mail</groupId>
			<artifactId>mail</artifactId>
			<version>${javax.mail.version}</version>
		</dependency>
		<dependency>
			<groupId>com.icegreen</groupId>
			<artifactId>greenmail</artifactId>
			<version>${greenmail.version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
		</dependency>
	</dependencies>
</project>

 

account persist中的POM文件

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

	<parent>
		<groupId>com.iteye.xujava</groupId>
		<artifactId>account</artifactId>
		<version>1.0.0</version>
		<relativePath>../pom.xml</relativePath>
	</parent>

	<artifactId>account-persist</artifactId>
	<name>Account Persist</name>

	<properties>
		<dom4j.version>1.6.1</dom4j.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>dom4j</groupId>
			<artifactId>dom4j</artifactId>
			<version>${dom4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
		</dependency>
	</dependencies>

	<build>
		<testResources>
			<testResource>
				<directory>src/test/resources</directory>
				<filtering>true</filtering>
			</testResource>
		</testResources>
	</build>
</project>

 

运行mvn clean test

运行mvn clean install

 

三、超级POM

    超级POM位置在%M2_HOME%/lib/maven-model-builder-3.0.5.jar\org\apache\maven\model中,为pom-4.0.0.xml。

内容如下:

<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!-- START SNIPPET: superpom -->
<project>
  <modelVersion>4.0.0</modelVersion>

  <!-- 仓库 -->
  <repositories>
    <repository>
      <id>central</id>
      <name>Central Repository</name>
      <url>http://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>

  <!-- 插件仓库 -->
  <pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <name>Central Repository</name>
      <url>http://repo.maven.apache.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>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>
      <!-- NOTE: These plugins will be removed from future versions of the super POM -->
      <!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
	  <!-- 设定核心插件版本 -->
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <reporting>
    <outputDirectory>${project.build.directory}/site</outputDirectory>
  </reporting>

  <profiles>
    <!-- NOTE: The release profile will be removed from future versions of the super POM -->
    <profile>
      <id>release-profile</id>

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

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

</project>
<!-- END SNIPPET: superpom -->

 

四、反应堆

    1.在一个多模块的Maven项目中,反应堆指所有模块组成的一个构建结构。
    2.裁剪反应堆
      -am:--also-make同时构建所列模块的依赖模块
      -amd:-also-make-dependents 同时构建依赖于所列模块的模块
      -pl:--projects<arg> 构建指定的模块,模块间用逗号分隔
      -rf:-resume-from<arg>从指定模块回复反应堆

    3.例子

      mvn clean install(默认)

 

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] Account ........................................... SUCCESS [1.341s]
[INFO] Account Email ..................................... SUCCESS [3.637s]
[INFO] Account Persist ................................... SUCCESS [2.348s]
[INFO] Account Captcha ................................... SUCCESS [3.149s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

 

mvn clean install -pl account-email,account-persist(指定构建的模块)
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] Account Email ..................................... SUCCESS [4.316s]
[INFO] Account Persist ................................... SUCCESS [2.152s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
 
    mvn clean install -pl account-email -am(同时构建所指定模块的依赖模块)
 
[INFO] Reactor Summary:
[INFO]
[INFO] Account ........................................... SUCCESS [0.739s]
[INFO] Account Email ..................................... SUCCESS [3.627s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
 

    mvn clean install -pl account-parent -amd(同时构建依赖于所列模块的模块,假设有account-parent模块,其他模块都依赖于它) 

 

 

    mvn clean install -rf account-persist(在完整反应堆构建顺序的基础上指定从哪个模块开始构建,原来大排在它之前的将不执行,原来Account和Account Email排在Account Persist之前,参考默认执行)

 

[INFO] Reactor Summary:
[INFO]
[INFO] Account Persist ................................... SUCCESS [4.326s]
[INFO] Account Captcha ................................... SUCCESS [2.864s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

 

 mvn clean install -pl account-parent -amd -rf account-captcha(首先-pl account-parent构建出Email、Persist、Captcha三个模块,然后 -rf account-captcha从Captcha开始构建,Capthca在完整的反应堆中又是最后一个构建,则最终只构建Captcha。假设有account-parent模块,其他模块都依赖于它) 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics