Hello Maven 5 - 聚合与继承

聚合与继承
聚合——把项目分成模块,使用统一的maven命令管理

<project>

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test<groupId>

    <artifactId>aggregator</artifactId>

    <version>1.0.0-SNAPAHOT</version>

    <packaging>pom</packaging> <!-- 默认的打包方式为jar,负责聚合的module打包方式为pom -->

    <name>Aggregator</name>

    <modules>

        <module>parent</module>

        <module>module1</module> <!-- 这里填写的是被聚合的模块的pom文件所在的相对路径 -->

        <module>module2</module> <!-- 比如对于平行目录结构的聚合,这里应写成../module2 -->

    </modules>

</project>

聚合块除了pom文件以外,不需要其他东西

继承——继承是为了复用配置
声明一个父模块

<project>

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test<groupId>

    <artifactId>parent</artifactId>

    <version>1.0.0-SNAPAHOT</version>

    <packaging>pom</packaging> <!-- 默认的打包方式为jar,负责聚合的module打包方式为pom -->

    <name>Parent</name>

</project>

父模块除了pom文件以外,不需要其他东西
继承的模块声明

<project>

    <parent>

        <groupId>com.test<groupId>

        <artifactId>parent</artifactId>

        <version>1.0.0-SNAPAHOT</version>

        <relativePath>../parent/pom.xml</relativePath> <!-- 不写的话,默认从../pom.xml查找父pom -->

    </parent>

    

    <artifactId>module1</artifactId> <!-- groupId、version从父继承了 -->

    <name>Module1</name>

    

    ...

</project>


可继承的pom元素
groupId
version
description
organization
inceptionYear
url
developers
contributors
distributionManagement 部署配置
issueManagement 缺陷跟踪系统信息
ciManagement 持续集成系统信息
scm 版本控制系统信息
mailingLists 邮件列表信息
properties 自定义属性
dependencies 依赖配置
dependencyManagement 依赖管理配置
repositories 仓库配置
build 源码目录配置、输出目录配置、插件配置、插件管理配置等
reporting 报告输出目录配置、报告插件配置等

依赖管理
在parent中使用dependencyManagement包裹dependencies

<dependencyManagement>

    <dependencies>

        <groupId>org.springframework</groupId>

        <artifactId>spring-core</artifactId>

        <version>3.0.0</version>

    </dependencies>

</dependencyManagement>

在子模块中

<dependencies>

    <groupId>org.springframework</groupId>

    <artifactId>spring-core</artifactId>    <!-- 不需要指定version等其他信息,从父继承了 -->

</dependencies>


插件管理
父配置

<build>

    <pluginManagement>

        <plugins>

            <plugin>

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

                <artifactId>maven-source-plugin</artifactId>

                <version>2.1.1</version>

                <executions>

                    <execution>

                        <id>attach-sources</id>

                        <phase>verify</phase>

                        <goals>

                            <goal>jar-no-fork</goal>

                        </goals>

                    </execution>

                </executions>

            </plugin>

        </plugins>

    </pluginManagement>

</build>

子配置

<build>

    <pluginManagement>

        <plugins>

            <plugin>

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

                <artifactId>maven-source-plugin</artifactId>

            </plugin>

        </plugins>

    </pluginManagement>

</build>

子模块声明使用了插件,同事又继承了模块的插件配置

反应堆
对于单模块项目,反应堆就是该模块本身,对于多模块项目,反应堆包含了各模块之间继承与依赖的关系,从而能够自动计算出合理的模块构建顺序
剪裁反应堆
install
-am also make 同时构建所列模块的依赖模块
-amd also make dependencies 同时构建依赖于所列模块的模块
-pl projects 构建指定的模块,逗号分隔
-rf resume-from 在完整的反应堆构建顺序上,从指定的模块开始构建

你可能感兴趣的:(maven)