【Maven實戰】學習之聚合與繼承

前言

  • 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 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>
    <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項目了
發佈了42 篇原創文章 · 獲贊 6 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章