欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

maven-聚合与继承

程序员文章站 2022-06-10 13:28:11
1、聚合-方便快速构建项目 多个maven模块要构建需要分别执行一次maven构建命令,怎样只执行一次构建命令就构建多个maven模块呢?maven提供了聚合模块可以满足一次运行,构建多模块的要求 2、继承-消除重复配置,统一管理 多个maven模块中的pxm.xml有很多相同的配置,如果简化配置? ......

 1、聚合-方便快速构建项目

  多个maven模块要构建需要分别执行一次maven构建命令,怎样只执行一次构建命令就构建多个maven模块呢?maven提供了聚合模块可以满足一次运行,构建多模块的要求

2、继承-消除重复配置,统一管理

  多个maven模块中的pxm.xml有很多相同的配置,如果简化配置?maven提供了继承机制,抽出重复的配置,统一依赖管理和插件管理

maven工程隐式的继承超级pom,超级pom如下:

<?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>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>  --在子maven工程中设定相同的标签元素的值,则覆盖父pom的标签元素的值
    <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 -->

以下元素可从超级pom获者其它的pom文件继承:

 maven-聚合与继承

maven-聚合与继承

3、聚合和继承的关系

maven-聚合与继承

对于聚合模块,它知道有哪些被聚合的模块,但那些被聚合的模块不知道聚合模块的存在。

对于继承关系的父pom,它不知道有哪些子模块继承它,但那些子模块知道自己继承的父pom

相同点:聚合pom和继承关系中的父pom的packaging必须是pom,

    聚合模块和继承关系中到的父模块除了pom之外都没有实际内容

 4、maven聚合工程(+父工程)

在实际项目中,通常聚合模块和继承关系中到的父模块是同一个,以下合并使用。两种常用的多模块的maven工程目录结构:

maven-聚合与继承          maven-聚合与继承

其中,parent既是聚合工程,也是父工程。

  • 新建聚合工程:略
  • parent模块的pom.xml文件
<?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">
  <modelversion>4.0.0</modelversion>

  <groupid>com.cn</groupid>
  <artifactid>parent</artifactid>
  <packaging>pom</packaging>
  <version>1.0-snapshot</version>
  <modules>
    <module>../common</module>   --被聚合的common模块
    <module>../mananger</module>
  </modules>

  <name>parent</name>
  <!-- fixme change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>  --定义属性的值,后续可以被引用
    <project.build.sourceencoding>utf-8</project.build.sourceencoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <spring-version>3.2.2.release</spring-version>
  </properties>

  <dependencies>  --需要引入的依赖
    <dependency>
      <groupid>junit</groupid>
      <artifactid>junit</artifactid>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <dependencymanagement>  --申明依赖,但实际不会被引入
    <dependencies>
      <dependency>
        <groupid>org.springframework</groupid>
        <artifactid>spring-core</artifactid>
        <version>${spring-version}</version>
      </dependency>
      <dependency>
        <groupid>org.springframework</groupid>
        <artifactid>spring-context</artifactid>
        <version>${spring-version}</version>
      </dependency>
    </dependencies>
  </dependencymanagement>

  <build>
    <pluginmanagement><!-- lock down plugins versions to avoid using maven defaults (may be moved to parent pom) -->  --申明插件,但实际不会被引入
      <plugins>
        <plugin>
          <artifactid>maven-clean-plugin</artifactid>
          <version>3.0.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactid>maven-resources-plugin</artifactid>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactid>maven-compiler-plugin</artifactid>
          <version>3.7.0</version>
        </plugin>
        <plugin>
          <artifactid>maven-surefire-plugin</artifactid>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactid>maven-jar-plugin</artifactid>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactid>maven-install-plugin</artifactid>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactid>maven-deploy-plugin</artifactid>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginmanagement>
  </build>
</project>
  • 子模块manager的pom.xml
<?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>    --配置继承父工程
        <artifactid>parent</artifactid>
        <groupid>com.cn</groupid>
        <version>1.0-snapshot</version>
        <relativepath>../parent/pom.xml</relativepath>   --父工程pom.xml的相对位置,未配置则默认为上一层目录下
    </parent>
    <modelversion>4.0.0</modelversion>

    <artifactid>mananger</artifactid> --省略了版本和组id,因为从父pom中继承了
    <packaging>war</packaging>

    <name>mananger maven webapp</name>
    <!-- fixme change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceencoding>utf-8</project.build.sourceencoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>  --实际引入了父pom中申明的jar包,只需指定id
            <groupid>org.springframework</groupid>
            <artifactid>spring-core</artifactid>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-context</artifactid>
        </dependency>
    </dependencies>

    <build>
       <plugins>
           <plugin>  --实际引入父pom中申明的插件
               <artifactid>maven-compiler-plugin</artifactid>
               <version>3.7.0</version>
               <configuration>
                   <source>1.7</source>
                   <target>1.7</target>
               </configuration>
           </plugin>
       </plugins>
    </build>
</project>
  • 执行构建命令,多模块的maven工程构建过程

  maven按序读取pom,如果该pom没有继承模块,就构建该模块,否则就先构建其继承的模块,如果该继承的模块还继承其它模块,则进一步先构建继承的模块。在此过程中,构建过的模块不再构建。(反应堆:所有模块组成的构建结构,包含了模块间的继承关系,能够计算出模块的构建顺序)

假设a项目聚合了b、c项目,b、c项目继承d项目,d继承e,则构建顺序为:a、e、d、b、c