Maven

(IX)Maven聚合和继承

2018-06-19  本文已影响0人  SonyaBaby

概述:当前软件设计人员会对软件划分模块,以得到更清晰的设计及更高的重用性。简单来说,Maven的聚合特性就是将项目的各个模块聚合在一起进行构建,Maven的继承特性可以帮助抽取各模块间相同的依赖和插件等配置

聚合

示例聚合模块pom:

<project>
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.play.myMaven</groupId>
  <artifactId>hello-maven-aggregator</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>POM</packaging>
  <name>Hello-Maven Aggregator</name>
  <modules>
    <module>hello-maven-persist</module>
    <module>hello-maven-server</module>
    <module>hello-maven-web</module>
  </modules>
</project>

其余模块示例:

<project>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>hello-maven-persist</artifactId>
</project>

执行一次mvn clean install

C:\Subversion\MavenPrj\helloMaven>mvn clean install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] Hello-Maven Aggregator
[INFO] hello-maven-persist
[INFO] hello-maven-server
[INFO] hello-maven-web
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Hello-Maven Aggregator 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
...
[INFO] Installing C:\Subversion\MavenPrj\helloMaven\pom.xml to C:\Repository\m2repo\com\play\myMaven\hello-maven-aggregator\1.0-SNAPSHOT\hello-maven-aggregator-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] Building hello-maven-persist 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
...
[INFO] Installing   C:\Subversion\MavenPrj\helloMaven\hello-maven-persist\target\hello-maven-persist-1.0-SNAPSHOT.jar to C:\Repository\m2repo\com\play\myMaven\hello-maven-persist\1.0-SNAPSHOT\hello-maven-persist-1.0-SNAPSHOT.jar
[INFO] Installing C:\Subversion\MavenPrj\helloMaven\hello-maven-persist\pom.xml to C:\Repository\m2repo\com\play\myMaven\hello-maven-persist\1.0-SNAPSHOT\hello-maven-persist-1.0-SNAPSHOT.pom
...
[INFO] ------------------------------------------------------------------------
[INFO] Building hello-maven-server 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
...
[INFO] ------------------------------------------------------------------------
[INFO] Building hello-maven-web 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] Hello-Maven Aggregator ............................. SUCCESS [  0.631 s]
[INFO] hello-maven-persist ................................ SUCCESS [  0.807 s]
[INFO] hello-maven-server ................................. SUCCESS [  0.087 s]
[INFO] hello-maven-web .................................... SUCCESS [  0.085 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

继承

多个POM中有着很多相同的配置,例如相同的groupId和version,有相同的spring-core、spring-beans、spring-context、spring-junit依赖等,还有相同的maven-compiler-plugin与maven-resources-plugin的插件配置等。为了避免重复,我们可以使用POM的继承特性。即“一处声明多处可用”

示例parent模块POM:

<project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.play.myMaven</groupId>
    <artifactId>hello-maven-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>parent</name>

    <properties>
        ...
    </properties>

    <dependencies>
        ...
    </dependencies>

    <build><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
        <plugins>
            ...
        </plugins>
    </build>
</project>

其余模块需要继承parent模块,示例:

<project xmlns="http://maven.apache.org/POM/4.0.0">
    <parent>
        <artifactId>hello-maven-parent</artifactId>
        <groupId>com.play.myMaven</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../hello-maven-parent</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>hello-maven-persist</artifactId>
</project>

将parent模块加入到聚合模块:

<modules>
  <module>hello-maven-persist</module>
  <module>hello-maven-server</module>
  <module>hello-maven-web</module>
  <module>hello-maven-parent</module>
</modules>

✿✿✿✿✿✿✿✿✿✿✿✿✿ 华丽丽的分界线✿✿ ✿✿✿✿✿✿✿✿✿✿✿


可继承的POM元素


聚合与继承的关系

聚合和继承是两个完全不同的概念。

聚合是为了快速构建项目,继承是为了消除重复配置。

共同点:
聚合POM与继承的父POM的packaging都必须是pom,另外,他们都没有实际的内容。当然我们也可以将聚合POM和父POM融合使用。
相关链接:
依赖管理
插件管理
约定优于配置
反应堆

上一篇 下一篇

猜你喜欢

热点阅读