【Maven实战】学习之聚合与继承

来源:互联网 发布:爱知超声波流量计软件 编辑:程序博客网 时间:2024/05/22 17:32

前言

  • Maven的聚合特性能够把项目的各个模块聚合在一起构建,而Maven的继承特性则能帮助抽取各模块相同的依赖和插件等配置,在简化POM的同时,还能促进各个模块配置的一致性。

聚合

  • 父模块代码如下:
<?xml version="1.0" encoding="UTF-8"?><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">    <!--model版本-->    <modelVersion>4.0.0</modelVersion>    <!--构件ID定义-->    <groupId>com.modual</groupId>    <artifactId>father</artifactId>    <version>1.0</version>    <packaging>pom</packaging>    <!--模块定义-->    <modules>        <module>son-one</module>        <module>son-two</module>    </modules></project>
  • 子模块POM代码如下
<?xml version="1.0" encoding="UTF-8"?><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.modual</groupId>        <artifactId>father</artifactId>        <version>1.0</version>    </parent>    <modelVersion>4.0.0</modelVersion>    <artifactId>son-one</artifactId>    <packaging>war</packaging></project>
  • 注意 :子模块所在目录应该在父pom定义modual名称一致
  • 用【mvn clean install】就可以看到项目的输出结果

继承

  • 解决重复的配置
  • 子模块代码
<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/maven-v4_0_0.xsd">      <modelVersion>4.0.0</modelVersion>      <parent>          <groupId>com.modual</groupId>          <artifactId>father</artifactId>          <version>1.0.0-SNAPSHOT</version>        <!--relativePath默认是../pom.xml-->        <relativePath>../pom.xml</relativePath>      </parent>      <artifactId>son-one</artifactId>      <name>son</name>      <dependencies>          ...      </dependencies>      <build>  <plugins>  ...  </plugins>  </build>  

可继承的元素

  • 1)groupId:项目组ID,项目坐标的核心元素
  • 2)version:项目版本,项目坐标的核心元素
  • 3)description:项目的描述信息
  • 4)organization:项目的组织信息
  • 5)inceptionYear:项目的创始年份
  • 6)url:项目的URL地址
  • 7)developers:项目的开发者信息
  • 8)contributors:项目的贡献值信息
  • 9)distributionManagement:项目的部署配置
  • 10)issueManagement:项目的缺陷跟踪系统信息
  • 11)ciManagement:项目的持续集成系统信息
  • 12)scm:项目的版本控制系统信息
  • 13)mailingLists:项目的邮件列表信息
  • 14)properties:自定义的Maven属性
  • 15)dependencies:项目的依赖配置
  • 16)dependencyManagement:项目的依赖管理配置
  • 17)repositories:项目的仓库配置
  • 18)build:包括项目的源码目录配置、输出目录配置、插件配置、插件管理配置等。
  • 19)reporting:包括项目的报告输出目录配置、报告插件配置等。

依赖管理

  • Maven提供的dependencyManagement元素既能让子模块继承到父模块的依赖配置,又能保证子模块依赖使用的灵活性。在dependencyManagement元素下的依赖声明不会引入实际的依赖,不过它能够约束dependencies下的依赖使用。
  • 父Pom文件代码:
<?xml version="1.0" encoding="UTF-8"?><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">    <!--model版本-->    <modelVersion>4.0.0</modelVersion>    <!--构件ID定义-->    <groupId>com.modual</groupId>    <artifactId>father</artifactId>    <version>1.0</version>    <packaging>pom</packaging>    <!--模块定义-->    <modules>        <module>son-one</module>        <module>son-two</module>    </modules>    <!--属性定义-->    <properties>        <version>1.0.01</version>    </properties>    <!--插件管理-->    <dependencyManagement>        <dependencies>            <dependency>                <groupId>com.plugin</groupId>                <artifactId>id</artifactId>                <version>${version}</version>            </dependency>        </dependencies>    </dependencyManagement></project>
  • 子类引用如下
<?xml version="1.0" encoding="UTF-8"?><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.modual</groupId>        <artifactId>father</artifactId>        <version>1.0</version>    </parent>    <modelVersion>4.0.0</modelVersion>    <artifactId>son-one</artifactId>    <packaging>war</packaging>    <dependencies>        <dependency>            <groupId>com.modual</groupId>            <artifactId>id</artifactId>        </dependency>    </dependencies></project>- 这样子模块只需要引用自己需要的依赖,而父模块统一管理项目范围内依赖的版本。如果子模块不声明依赖的使用,即使在父模块已经声明也不会产生任何实际的效果。

插件管理

  • Maven提供的pluginManagement元素管理插件。在该元素中配置的依赖不会造成实际的插件调用行为,只有在子pom中配置了相应的plugin才会被真实调用。
  • 好处在与由父pom统一管理版本和相应的配置,避免潜在的插件不一致或者不稳定问题,也更易于维护。
  • 如下面源码:
父Pom:<!--构建/编译定义-->    <build>        <pluginManagement>            <!--插件定义-->            <plugins>                <!--编译插件-->                <plugin>                    <groupId>org.apache.maven.plugins</groupId>                    <artifactId>maven-compiler-plugin</artifactId>                    <version>2.3.2</version>                    <configuration>                        <!--采用JDK1.8-->                        <source>1.8</source>                        <target>1.8</target>                    </configuration>                </plugin>            </plugins>        </pluginManagement>    </build>
子pom:    <build>        <!--插件定义-->        <plugins>            <!--编译插件-->            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>            </plugin>        </plugins>    </build>

maven定义标准

  • maven有一个隐式的pom,所有maven项目都会隐式继承该pom,该pom定义了一系列配置标准,该文件在【$MAVEN_HOME/lib/maven-model-builder-x.x.x.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 oneor more contributor license agreements.  See the NOTICE filedistributed with this work for additional informationregarding copyright ownership.  The ASF licenses this fileto you under the Apache License, Version 2.0 (the"License"); you may not use this file except in compliancewith the License.  You may obtain a copy of the License at    http://www.apache.org/licenses/LICENSE-2.0Unless 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 ANYKIND, either express or implied.  See the License for thespecific language governing permissions and limitationsunder the License.--><!-- START SNIPPET: superpom --><project>  <modelVersion>4.0.0</modelVersion>  <repositories>    <repository>      <id>central</id>      <name>Central Repository</name>      <url>https://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>https://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>${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>      <!-- 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.8</version>        </plugin>        <plugin>          <artifactId>maven-release-plugin</artifactId>          <version>2.3.2</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 -->

反应堆

  • 在多模块中,指所有模块组成的一个构建结构【根据模块之间的继承和依赖关系,能够计算出合理的模块构建顺序】。
  • 在单模块中,指本身。
  • 模块之间的关系只能是有向非循环,否则maven就会报错

裁剪反应堆

  • 一般来说,开发者会构建整个项目,或者选择单独的模块进行构建。如果仅仅想构建反应堆的某些模块。用户就需要实时地裁剪反应堆。
  • 裁剪反应堆指令如下
-am:同时构建所列模块的依赖模块-amd:同时构建依赖于所列模块的模块-pl:构建指定模块-rf:从指定的模块返回一个反应堆对象
  • eg:裁剪A项目,但是A依赖于B项目,这时候执行命令如下
mvn clean install -pl A,B或者mvn clean install -pl A -am
  • eg:如果A,B,C三个项目,A,B依赖于C项目,想构建依赖C项目执行一下命令
mvn clean install -pl C -amd
  • eg:如果在裁剪出来的A,B,C三个项目,指定项目构建可以用-rf命令
clean install -pl C -amd -rf A这样就只会构建A项目了