Maven进阶

13 篇文章 0 订阅
12 篇文章 4 订阅

Maven进阶

一. 深入理解pom

1⃣️ 重新认识Maven
  1. Maven的完整性

    • 在入门的时候我们介绍说Maven是一款「构架管理」和「依赖管理」的工具. 但事实上这姿势Maven的一部分功能. Maven本身的产品定位是一款「项目管理工具」
  2. 项目管理功能的具体表现

    • 下面是spring-boot-starter的POM文件, 可以看到: 除了我们熟悉的坐标标签, dependenices标签, 还有description, url, organization, licenses, developers, scm, issueManagement等这些描述项目信息的标签

    • description标签: 当前jar包的介绍

      organization标签: 组织, 子标签name: 组织名, 子标签url: 官网地址

      licenses标签: 类似于授权许可相应的, 子标签name: 许可名, 子标签url: 许可地址

      developers标签: 开发对应标签, 子标签developer, 子标签name: 开发组织名, 子标签email: 开发组织邮箱, 子标签organization: ,子标签organizationUrl: 官网

      scm标签: 软件配置管理, 子标签connection: 链接一般是git:git://, 子标签developerConnection: 开发链接一般是git:ssh, 子标签url: 链接一般是git:https仓库地址

      issueManagement解决问题管理, 子标签system: 一般来说是github或者gitee这类型的种类, 子标签url: 对应的地址链接

    • code

      <?xml version="1.0" encoding="UTF-8"?>
      <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <!-- This module was also published with a richer model, Gradle metadata,  -->
        <!-- which should be used instead. Do not delete the following line which  -->
        <!-- is to indicate to Gradle or any Gradle module metadata file consumer  -->
        <!-- that they should prefer consuming it instead. -->
        <!-- do_not_remove: published-with-gradle-metadata -->
        <modelVersion>4.0.0</modelVersion>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>2.5.6</version>
        <name>spring-boot-starter</name>
        <!-- description标签: 当前jar包的介绍 -->
        <description>Core starter, including auto-configuration support, logging and YAML</description>
        <url>https://spring.io/projects/spring-boot</url>
        <organization>
          <name>Pivotal Software, Inc.</name>
          <url>https://spring.io</url>
        </organization>
        <licenses>
          <license>
            <name>Apache License, Version 2.0</name>
            <url>https://www.apache.org/licenses/LICENSE-2.0</url>
          </license>
        </licenses>
        <developers>
          <developer>
            <name>Pivotal</name>
            <email>info@pivotal.io</email>
            <organization>Pivotal Software, Inc.</organization>
            <organizationUrl>https://www.spring.io</organizationUrl>
          </developer>
        </developers>
        <scm>
          <connection>scm:git:git://github.com/spring-projects/spring-boot.git</connection>
          <developerConnection>scm:git:ssh://git@github.com/spring-projects/spring-boot.git</developerConnection>
          <url>https://github.com/spring-projects/spring-boot</url>
        </scm>
        <issueManagement>
          <system>GitHub</system>
          <url>https://github.com/spring-projects/spring-boot/issues</url>
        </issueManagement>
      
        <dependencies>
          <dependency>
            ……
          </dependency>
        </dependencies>
      </project>
      
    • 所以从「项目管理」的角度来看, Maven提供了如下这些功能:

      • 项目对象(POM): 将整个项目本体抽象, 封装为应用成虚中的一个对象, 以便于管理和操作

      • 全局性构建逻辑重用: Maven对整个构建过程进行分装之后, 程序只需要制定配置信息即可完成构建. 让构建过程从Ant的「编程式」升级到了Maven的「声明式」

      • 构件的标准集合: 在Maven提供的标准框架体系内, 所有的构件都可以按照统一的规范生成和使用

      • 构件关系定义: Maven定义了构件之间的三种基本关系, 让大型应用系统可以使用Maven来进行管理

        • 继承关系: 通过从上倒下的继承关系, 将各个子构件中的重复信息提取到父构建中统一管理
        • 聚合关系: 将多个构建聚合为一个整体, 便于统一操作
        • 依赖关系: Maven定义了依赖的范围, 依赖的传递, 依赖的排除, 版本总裁机制等一系列规范和标准, 让大型项目可以有序容纳数百甚至更过依赖
      • 插件目标系统: Maven核心程序定义抽象的生命周期, 然后将插件的目标绑定到生命周期中的特定阶段, 实现了标准和具体实现解耦合, 让Maven程序极具扩展性

      • 项目描述信息的维护: 我们不仅可以在Pom中声明项目描述信息, 更可以将整个项目相关信息收集起来生成HTMl页面组成的一个可以直接访问的站点. 这些项目描述信息包括:

        1. 公司和组织信息

        2. 项目许可

        3. 开发成员信息

        4. issue管理信息

        5. SCM信息

          …等

2⃣️ POM的四个层次
  1. 超级POM

    官方解释

    The Super POM is Maven’s default POM. All POMs extend the Super POM unless explicitly set, meaning the configuration specified in the Super POM is inherited by the POMs you created for your projects.

    译文:Super POM 是 Maven 的默认 POM。除非明确设置,否则所有 POM 都扩展 Super POM,这意味着 Super POM 中指定的配置由您为项目创建的 POM 继承。

    所以我们自己的POM即使没有明确指定一个父工程(副POM), 其实也默认继承了超级POM. 就好比一个Java类默认继承了Object类

    那么超级POM中定义如下

    <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.5.3</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-no-fork</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>
    
  2. 父POM

    • 和Java一样, POM之间其实也是单继承的. 如果我们给一个POM制定了父POM, 那么继承关系如下图

      image-20220712223518311

  3. 有效POM

    • 概念

      有效pom英文翻译为effective Pom, 他的概念是这样的–在POM继承关系中, 子POM可以覆盖父POM中的配置; 如果子POM没有覆盖, 那么父POM中的配置将会被继承. 按照这个规则, 继承管系中的所有POM叠加到一起, 就得到了一个最终生效的POM. 显然Maven世纪运行过程中, 执行构建操作就是按照这个最终生效的POM来运行的. 这个最终生效的POM就是有效POM,

    • 查看有效POM命令

      mvn help:effective-pom
      
        • 类似结果集
      /Library/Java/JavaVirtualMachines/jdk-17.0.2.jdk/Contents/Home/bin/java -Dvisualgc.id=56778845030875 -Dmaven.multiModuleProjectDirectory=/Users/zangzihe/Desktop/GitHub/test/test-maven-parent -Dmaven.home=/Users/zangzihe/Desktop/study/maven/apache-maven-3.6.3 -Dclassworlds.conf=/Users/zangzihe/Desktop/study/maven/apache-maven-3.6.3/bin/m2.conf -Dmaven.ext.class.path=/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven-event-listener.jar -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=60237:/Applications/IntelliJ IDEA.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/zangzihe/Desktop/study/maven/apache-maven-3.6.3/boot/plexus-classworlds.license:/Users/zangzihe/Desktop/study/maven/apache-maven-3.6.3/boot/plexus-classworlds-2.6.0.jar org.codehaus.classworlds.Launcher -Didea.version=2022.1.3 -s /Users/zangzihe/Desktop/study/maven/apache-maven-3.6.3/conf/settings_1.17.xml help:effective-pom
      [INFO] Scanning for projects...
      [INFO] ------------------------------------------------------------------------
      [INFO] Reactor Build Order:
      [INFO] 
      [INFO] test-maven-parent                                                  [pom]
      [INFO] test-maven-son                                                     [jar]
      Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-help-plugin/maven-metadata.xml
      Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-help-plugin/maven-metadata.xml (653 B at 609 B/s)
      Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-help-plugin/3.2.0/maven-help-plugin-3.2.0.pom
      Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-help-plugin/3.2.0/maven-help-plugin-3.2.0.pom (11 kB at 30 kB/s)
      Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-help-plugin/3.2.0/maven-help-plugin-3.2.0.jar
      Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-help-plugin/3.2.0/maven-help-plugin-3.2.0.jar (86 kB at 156 kB/s)
      [INFO] 
      [INFO] --------------------< com.azang:test-maven-parent >---------------------
      [INFO] Building test-maven-parent 1.0-SNAPSHOT                            [1/2]
      [INFO] --------------------------------[ pom ]---------------------------------
      [INFO] 
      [INFO] --- maven-help-plugin:3.2.0:effective-pom (default-cli) @ test-maven-parent ---
      Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/maven-model/3.6.1/maven-model-3.6.1.pom
      Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/maven-model/3.6.1/maven-model-3.6.1.pom (4.0 kB at 11 kB/s)
      Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/maven/3.6.1/maven-3.6.1.pom
      Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/maven/3.6.1/maven-3.6.1.pom (24 kB at 55 kB/s)
      Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/plexus/plexus-utils/3.2.0/plexus-utils-3.2.0.pom
      Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/plexus/plexus-utils/3.2.0/plexus-utils-3.2.0.pom (4.8 kB at 13 kB/s)
      Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugin-tools/maven-plugin-tools-generators/3.5.2/maven-plugin-tools-generators-3.5.2.pom
      Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugin-tools/maven-plugin-tools-generators/3.5.2/maven-plugin-tools-generators-3.5.2.pom (4.2 kB at 9.5 kB/s)
      Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugin-tools/maven-plugin-tools-api/3.5.2/maven-plugin-tools-api-3.5.2.pom
      Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugin-tools/maven-plugin-tools-api/3.5.2/maven-plugin-tools-api-3.5.2.pom (2.9 kB at 8.6 kB/s)
      Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/net/sf/jtidy/jtidy/r938/jtidy-r938.pom
      Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/net/sf/jtidy/jtidy/r938/jtidy-r938.pom (9.2 kB at 27 kB/s)
      Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-artifact-transfer/0.10.0/maven-artifact-transfer-0.10.0.pom
      Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-artifact-transfer/0.10.0/maven-artifact-transfer-0.10.0.pom (0 B at 0 B/s)
      Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/plexus/plexus-utils/1.4/plexus-utils-1.4.pom
      Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/plexus/plexus-utils/1.4/plexus-utils-1.4.pom (0 B at 0 B/s)
      Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/com/thoughtworks/xstream/xstream/1.4.11.1/xstream-1.4.11.1.pom
      Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/com/thoughtworks/xstream/xstream/1.4.11.1/xstream-1.4.11.1.pom (20 kB at 53 kB/s)
      Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/com/thoughtworks/xstream/xstream-parent/1.4.11.1/xstream-parent-1.4.11.1.pom
      Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/com/thoughtworks/xstream/xstream-parent/1.4.11.1/xstream-parent-1.4.11.1.pom (37 kB at 74 kB/s)
      Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/maven-model/3.6.1/maven-model-3.6.1.jar
      Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugin-tools/maven-plugin-tools-generators/3.5.2/maven-plugin-tools-generators-3.5.2.jar
      Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugin-tools/maven-plugin-tools-api/3.5.2/maven-plugin-tools-api-3.5.2.jar
      Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-artifact-transfer/0.10.0/maven-artifact-transfer-0.10.0.jar
      Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/net/sf/jtidy/jtidy/r938/jtidy-r938.jar
      Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-artifact-transfer/0.10.0/maven-artifact-transfer-0.10.0.jar (0 B at 0 B/s)
      Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-common-artifact-filters/3.0.1/maven-common-artifact-filters-3.0.1.jar
      Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-common-artifact-filters/3.0.1/maven-common-artifact-filters-3.0.1.jar (0 B at 0 B/s)
      Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/plexus/plexus-utils/3.2.0/plexus-utils-3.2.0.jar
      Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/maven-model/3.6.1/maven-model-3.6.1.jar (186 kB at 340 kB/s)
      Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/com/thoughtworks/xstream/xstream/1.4.11.1/xstream-1.4.11.1.jar
      Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugin-tools/maven-plugin-tools-api/3.5.2/maven-plugin-tools-api-3.5.2.jar (24 kB at 36 kB/s)
      Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/net/sf/jtidy/jtidy/r938/jtidy-r938.jar (250 kB at 320 kB/s)
      Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugin-tools/maven-plugin-tools-generators/3.5.2/maven-plugin-tools-generators-3.5.2.jar (48 kB at 60 kB/s)
      Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/plexus/plexus-utils/3.2.0/plexus-utils-3.2.0.jar (263 kB at 273 kB/s)
      Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/com/thoughtworks/xstream/xstream/1.4.11.1/xstream-1.4.11.1.jar (621 kB at 594 kB/s)
      [INFO] 
      Effective POMs, after inheritance, interpolation, and profiles are applied:
      
      <?xml version="1.0" encoding="UTF-8"?>
      <!-- ====================================================================== -->
      <!--                                                                        -->
      <!-- Generated by Maven Help Plugin on 2022-07-12T22:46:25+08:00            -->
      <!-- See: http://maven.apache.org/plugins/maven-help-plugin/                -->
      <!--                                                                        -->
      <!-- ====================================================================== -->
      <projects>
        <!-- ====================================================================== -->
        <!--                                                                        -->
        <!-- Effective POM for project                                              -->
        <!-- 'com.azang:test-maven-parent:pom:1.0-SNAPSHOT'                         -->
        <!--                                                                        -->
        <!-- ====================================================================== -->
        <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
          <groupId>com.azang</groupId>
          <artifactId>test-maven-parent</artifactId>
          <version>1.0-SNAPSHOT</version>
          <packaging>pom</packaging>
          <name>test-maven-parent</name>
          <url>http://maven.apache.org</url>
          <modules>
            <module>test-maven-son</module>
          </modules>
          <properties>
            <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
            <maven.compiler.source>1.17</maven.compiler.source>
            <maven.compiler.target>1.17</maven.compiler.target>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
          </properties>
          <dependencies>
            <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>4.11</version>
              <scope>test</scope>
            </dependency>
          </dependencies>
          <repositories>
            <repository>
              <snapshots>
                <enabled>false</enabled>
              </snapshots>
              <id>central</id>
              <name>Central Repository</name>
              <url>https://repo.maven.apache.org/maven2</url>
            </repository>
          </repositories>
          <pluginRepositories>
            <pluginRepository>
              <releases>
                <updatePolicy>never</updatePolicy>
              </releases>
              <snapshots>
                <enabled>false</enabled>
              </snapshots>
              <id>central</id>
              <name>Central Repository</name>
              <url>https://repo.maven.apache.org/maven2</url>
            </pluginRepository>
          </pluginRepositories>
          <build>
            <sourceDirectory>/Users/zangzihe/Desktop/GitHub/test/test-maven-parent/src/main/java</sourceDirectory>
            <scriptSourceDirectory>/Users/zangzihe/Desktop/GitHub/test/test-maven-parent/src/main/scripts</scriptSourceDirectory>
            <testSourceDirectory>/Users/zangzihe/Desktop/GitHub/test/test-maven-parent/src/test/java</testSourceDirectory>
            <outputDirectory>/Users/zangzihe/Desktop/GitHub/test/test-maven-parent/target/classes</outputDirectory>
            <testOutputDirectory>/Users/zangzihe/Desktop/GitHub/test/test-maven-parent/target/test-classes</testOutputDirectory>
            <resources>
              <resource>
                <directory>/Users/zangzihe/Desktop/GitHub/test/test-maven-parent/src/main/resources</directory>
              </resource>
            </resources>
            <testResources>
              <testResource>
                <directory>/Users/zangzihe/Desktop/GitHub/test/test-maven-parent/src/test/resources</directory>
              </testResource>
            </testResources>
            <directory>/Users/zangzihe/Desktop/GitHub/test/test-maven-parent/target</directory>
            <finalName>test-maven-parent-1.0-SNAPSHOT</finalName>
            <pluginManagement>
              <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.5.3</version>
                </plugin>
              </plugins>
            </pluginManagement>
            <plugins>
              <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>2.5</version>
                <executions>
                  <execution>
                    <id>default-clean</id>
                    <phase>clean</phase>
                    <goals>
                      <goal>clean</goal>
                    </goals>
                  </execution>
                </executions>
              </plugin>
              <plugin>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.4</version>
                <executions>
                  <execution>
                    <id>default-install</id>
                    <phase>install</phase>
                    <goals>
                      <goal>install</goal>
                    </goals>
                  </execution>
                </executions>
              </plugin>
              <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.7</version>
                <executions>
                  <execution>
                    <id>default-deploy</id>
                    <phase>deploy</phase>
                    <goals>
                      <goal>deploy</goal>
                    </goals>
                  </execution>
                </executions>
              </plugin>
              <plugin>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.3</version>
                <executions>
                  <execution>
                    <id>default-site</id>
                    <phase>site</phase>
                    <goals>
                      <goal>site</goal>
                    </goals>
                    <configuration>
                      <outputDirectory>/Users/zangzihe/Desktop/GitHub/test/test-maven-parent/target/site</outputDirectory>
                      <reportPlugins>
                        <reportPlugin>
                          <groupId>org.apache.maven.plugins</groupId>
                          <artifactId>maven-project-info-reports-plugin</artifactId>
                        </reportPlugin>
                      </reportPlugins>
                    </configuration>
                  </execution>
                  <execution>
                    <id>default-deploy</id>
                    <phase>site-deploy</phase>
                    <goals>
                      <goal>deploy</goal>
                    </goals>
                    <configuration>
                      <outputDirectory>/Users/zangzihe/Desktop/GitHub/test/test-maven-parent/target/site</outputDirectory>
                      <reportPlugins>
                        <reportPlugin>
                          <groupId>org.apache.maven.plugins</groupId>
                          <artifactId>maven-project-info-reports-plugin</artifactId>
                        </reportPlugin>
                      </reportPlugins>
                    </configuration>
                  </execution>
                </executions>
                <configuration>
                  <outputDirectory>/Users/zangzihe/Desktop/GitHub/test/test-maven-parent/target/site</outputDirectory>
                  <reportPlugins>
                    <reportPlugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-project-info-reports-plugin</artifactId>
                    </reportPlugin>
                  </reportPlugins>
                </configuration>
              </plugin>
            </plugins>
          </build>
          <reporting>
            <outputDirectory>/Users/zangzihe/Desktop/GitHub/test/test-maven-parent/target/site</outputDirectory>
          </reporting>
        </project>
        <!-- ====================================================================== -->
        <!--                                                                        -->
        <!-- Effective POM for project 'com.azang:test-maven-son:jar:1.0-SNAPSHOT'  -->
        <!--                                                                        -->
        <!-- ====================================================================== -->
        <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
          <parent>
            <groupId>com.azang</groupId>
            <artifactId>test-maven-parent</artifactId>
            <version>1.0-SNAPSHOT</version>
          </parent>
          <groupId>com.azang</groupId>
          <artifactId>test-maven-son</artifactId>
          <version>1.0-SNAPSHOT</version>
          <name>test-maven-son</name>
          <url>http://maven.apache.org</url>
          <properties>
            <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
            <maven.compiler.source>1.17</maven.compiler.source>
            <maven.compiler.target>1.17</maven.compiler.target>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
          </properties>
          <dependencies>
            <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>4.11</version>
              <scope>test</scope>
            </dependency>
          </dependencies>
          <repositories>
            <repository>
              <snapshots>
                <enabled>false</enabled>
              </snapshots>
              <id>central</id>
              <name>Central Repository</name>
              <url>https://repo.maven.apache.org/maven2</url>
            </repository>
          </repositories>
          <pluginRepositories>
            <pluginRepository>
              <releases>
                <updatePolicy>never</updatePolicy>
              </releases>
              <snapshots>
                <enabled>false</enabled>
              </snapshots>
              <id>central</id>
              <name>Central Repository</name>
              <url>https://repo.maven.apache.org/maven2</url>
            </pluginRepository>
          </pluginRepositories>
          <build>
            <sourceDirectory>/Users/zangzihe/Desktop/GitHub/test/test-maven-parent/test-maven-son/src/main/java</sourceDirectory>
            <scriptSourceDirectory>/Users/zangzihe/Desktop/GitHub/test/test-maven-parent/test-maven-son/src/main/scripts</scriptSourceDirectory>
            <testSourceDirectory>/Users/zangzihe/Desktop/GitHub/test/test-maven-parent/test-maven-son/src/test/java</testSourceDirectory>
            <outputDirectory>/Users/zangzihe/Desktop/GitHub/test/test-maven-parent/test-maven-son/target/classes</outputDirectory>
            <testOutputDirectory>/Users/zangzihe/Desktop/GitHub/test/test-maven-parent/test-maven-son/target/test-classes</testOutputDirectory>
            <resources>
              <resource>
                <directory>/Users/zangzihe/Desktop/GitHub/test/test-maven-parent/test-maven-son/src/main/resources</directory>
              </resource>
            </resources>
            <testResources>
              <testResource>
                <directory>/Users/zangzihe/Desktop/GitHub/test/test-maven-parent/test-maven-son/src/test/resources</directory>
              </testResource>
            </testResources>
            <directory>/Users/zangzihe/Desktop/GitHub/test/test-maven-parent/test-maven-son/target</directory>
            <finalName>test-maven-son-1.0-SNAPSHOT</finalName>
            <pluginManagement>
              <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.5.3</version>
                </plugin>
              </plugins>
            </pluginManagement>
            <plugins>
              <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>2.5</version>
                <executions>
                  <execution>
                    <id>default-clean</id>
                    <phase>clean</phase>
                    <goals>
                      <goal>clean</goal>
                    </goals>
                  </execution>
                </executions>
              </plugin>
              <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                <executions>
                  <execution>
                    <id>default-testResources</id>
                    <phase>process-test-resources</phase>
                    <goals>
                      <goal>testResources</goal>
                    </goals>
                  </execution>
                  <execution>
                    <id>default-resources</id>
                    <phase>process-resources</phase>
                    <goals>
                      <goal>resources</goal>
                    </goals>
                  </execution>
                </executions>
              </plugin>
              <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <executions>
                  <execution>
                    <id>default-jar</id>
                    <phase>package</phase>
                    <goals>
                      <goal>jar</goal>
                    </goals>
                  </execution>
                </executions>
              </plugin>
              <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <executions>
                  <execution>
                    <id>default-compile</id>
                    <phase>compile</phase>
                    <goals>
                      <goal>compile</goal>
                    </goals>
                  </execution>
                  <execution>
                    <id>default-testCompile</id>
                    <phase>test-compile</phase>
                    <goals>
                      <goal>testCompile</goal>
                    </goals>
                  </execution>
                </executions>
              </plugin>
              <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <executions>
                  <execution>
                    <id>default-test</id>
                    <phase>test</phase>
                    <goals>
                      <goal>test</goal>
                    </goals>
                  </execution>
                </executions>
              </plugin>
              <plugin>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.4</version>
                <executions>
                  <execution>
                    <id>default-install</id>
                    <phase>install</phase>
                    <goals>
                      <goal>install</goal>
                    </goals>
                  </execution>
                </executions>
              </plugin>
              <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.7</version>
                <executions>
                  <execution>
                    <id>default-deploy</id>
                    <phase>deploy</phase>
                    <goals>
                      <goal>deploy</goal>
                    </goals>
                  </execution>
                </executions>
              </plugin>
              <plugin>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.3</version>
                <executions>
                  <execution>
                    <id>default-site</id>
                    <phase>site</phase>
                    <goals>
                      <goal>site</goal>
                    </goals>
                    <configuration>
                      <outputDirectory>/Users/zangzihe/Desktop/GitHub/test/test-maven-parent/test-maven-son/target/site</outputDirectory>
                      <reportPlugins>
                        <reportPlugin>
                          <groupId>org.apache.maven.plugins</groupId>
                          <artifactId>maven-project-info-reports-plugin</artifactId>
                        </reportPlugin>
                      </reportPlugins>
                    </configuration>
                  </execution>
                  <execution>
                    <id>default-deploy</id>
                    <phase>site-deploy</phase>
                    <goals>
                      <goal>deploy</goal>
                    </goals>
                    <configuration>
                      <outputDirectory>/Users/zangzihe/Desktop/GitHub/test/test-maven-parent/test-maven-son/target/site</outputDirectory>
                      <reportPlugins>
                        <reportPlugin>
                          <groupId>org.apache.maven.plugins</groupId>
                          <artifactId>maven-project-info-reports-plugin</artifactId>
                        </reportPlugin>
                      </reportPlugins>
                    </configuration>
                  </execution>
                </executions>
                <configuration>
                  <outputDirectory>/Users/zangzihe/Desktop/GitHub/test/test-maven-parent/test-maven-son/target/site</outputDirectory>
                  <reportPlugins>
                    <reportPlugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-project-info-reports-plugin</artifactId>
                    </reportPlugin>
                  </reportPlugins>
                </configuration>
              </plugin>
            </plugins>
          </build>
          <reporting>
            <outputDirectory>/Users/zangzihe/Desktop/GitHub/test/test-maven-parent/test-maven-son/target/site</outputDirectory>
          </reporting>
        </project>
      </projects>
      
      
      [INFO] ------------------------------------------------------------------------
      [INFO] Reactor Summary for test-maven-parent 1.0-SNAPSHOT:
      [INFO] 
      [INFO] test-maven-parent .................................. SUCCESS [  5.136 s]
      [INFO] test-maven-son ..................................... SKIPPED
      [INFO] ------------------------------------------------------------------------
      [INFO] BUILD SUCCESS
      [INFO] ------------------------------------------------------------------------
      [INFO] Total time:  7.462 s
      [INFO] Finished at: 2022-07-12T22:46:26+08:00
      [INFO] ------------------------------------------------------------------------
      
      Process finished with exit code 0
      
      
  4. 小结

    • 超级POM: 所有POM默认继承的, 只有直接和间接之分
    • 父POM: 可能有多个父POM,也可能没有显性的父POM, 如果没有显性的父POM的话那么父POM就是超级POM
    • 当前pom.xml的POM: 关注修改使用最多的的POM
    • 有效POM: 隐含的一层, 但实际上是真正生效的一层POM
3⃣️. 属性的声明与引用
  1. help插件的各个命令目标

    • 官网说明地址

      目标说明
      help:active-profiles列出当前已激活的 profile
      help:all-profiles列出当前工程所有可用 profile
      help:describe描述一个插件和/或 Mojo 的属性
      help:effective-pom以 XML 格式展示有效 POM
      help:effective-settings为当前工程以 XML 格式展示计算得到的 settings 配置
      help:evaluate计算用户在交互模式下给出的 Maven 表达式
      help:system显示平台详细信息列表,如系统属性和环境变量
  2. 使用help:evaluate查看属性值

    使用命令

    [INFO] Scanning for projects...
    [INFO] ------------------------------------------------------------------------
    [INFO] Reactor Build Order:
    [INFO] 
    [INFO] test-maven-parent                                                  [pom]
    [INFO] test-maven-son                                                     [jar]
    [INFO] 
    [INFO] --------------------< com.azang:test-maven-parent >---------------------
    [INFO] Building test-maven-parent 1.0-SNAPSHOT                            [1/2]
    [INFO] --------------------------------[ pom ]---------------------------------
    [INFO] 
    [INFO] --- maven-help-plugin:3.2.0:evaluate (default-cli) @ test-maven-parent ---
    [INFO] No artifact parameter specified, using 'com.azang:test-maven-parent:pom:1.0-SNAPSHOT' as project.
    [INFO] Enter the Maven expression i.e. ${project.groupId} or 0 to exit?:
    
    # 输入要查看的key[表达式]
    ${maven.compiler.source}
    [INFO] 
    # 表达式计算结果 
    1.17
    [INFO] Enter the Maven expression i.e. ${project.groupId} or 0 to exit?:
    
    # 使用0退出
    0
    [INFO] ------------------------------------------------------------------------
    [INFO] Reactor Summary for test-maven-parent 1.0-SNAPSHOT:
    [INFO] 
    [INFO] test-maven-parent .................................. SUCCESS [03:59 min]
    [INFO] test-maven-son ..................................... SKIPPED
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  03:59 min
    [INFO] Finished at: 2022-07-15T11:52:51+08:00
    [INFO] ------------------------------------------------------------------------
    
    
  3. 通过Maven访问系统属性

    • Java系统属性一览

      • code

        public static void main(String[] args) {
          Properties properties = System.getProperties();
          Set<Object> propNameSet = properties.keySet();
          for (Object propName : propNameSet) {
            String propValue = properties.getProperty((String) propName);
            System.out.println(propName + " = " + propValue);
          }
        }
        
        // 控制台输出
        java.specification.version = 17
        sun.jnu.encoding = UTF-8
        java.class.path = /Users/zangzihe/Desktop/GitHub/test/test-maven-parent/test-maven-son/target/classes:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar
        java.vm.vendor = Oracle Corporation
        sun.arch.data.model = 64
        java.vendor.url = https://java.oracle.com/
        java.vm.specification.version = 17
        os.name = Mac OS X
        sun.java.launcher = SUN_STANDARD
        user.country = CN
        sun.boot.library.path = /Library/Java/JavaVirtualMachines/jdk-17.0.2.jdk/Contents/Home/lib
        sun.java.command = com.azang.App
        http.nonProxyHosts = 127.0.0.1|localhost|*.localhost
        jdk.debug = release
        sun.cpu.endian = little
        user.home = /Users/zangzihe
        user.language = zh
        java.specification.vendor = Oracle Corporation
        java.version.date = 2022-01-18
        java.home = /Library/Java/JavaVirtualMachines/jdk-17.0.2.jdk/Contents/Home
        file.separator = /
        java.vm.compressedOopsMode = Zero based
        line.separator = 
        
        java.vm.specification.vendor = Oracle Corporation
        java.specification.name = Java Platform API Specification
        intellij.debug.agent = true
        visualgc.id = 20740549992333
        user.script = Hans
        sun.management.compiler = HotSpot 64-Bit Tiered Compilers
        ftp.nonProxyHosts = 127.0.0.1|localhost|*.localhost
        java.runtime.version = 17.0.2+8-LTS-86
        user.name = zangzihe
        path.separator = :
        os.version = 12.4
        java.runtime.name = Java(TM) SE Runtime Environment
        file.encoding = UTF-8
        java.vm.name = Java HotSpot(TM) 64-Bit Server VM
        java.vendor.url.bug = https://bugreport.java.com/bugreport/
        java.io.tmpdir = /var/folders/wn/pfk8kk2n4dv8357p344t7rd40000gn/T/
        java.version = 17.0.2
        jboss.modules.system.pkgs = com.intellij.rt
        user.dir = /Users/zangzihe/Desktop/GitHub/test/test-maven-parent
        os.arch = aarch64
        java.vm.specification.name = Java Virtual Machine Specification
        native.encoding = UTF-8
        java.library.path = /Users/zangzihe/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
        java.vm.info = mixed mode, sharing
        java.vendor = Oracle Corporation
        java.vm.version = 17.0.2+8-LTS-86
        sun.io.unicode.encoding = UnicodeBig
        socksNonProxyHosts = 127.0.0.1|localhost|*.localhost
        java.class.version = 61.0
        Disconnected from the target VM, address: '127.0.0.1:53829', transport: 'socket'
        
        Process finished with exit code 0
        
        
  4. 访问settings全局配置

    ${settings.标签名}可以访问settings.xml中的配置的元素值

  5. 用途

    • 在当前pom.xml文件中引用属性
    • 资源过滤功能: 在非Maven配置文件中引用属性, 有Maven在处理资源时将引用过属性的表达式替换为属性值
4⃣️. build标签
1) 是什么
  • 是在我们需要定制构建过程的时候才会通过配置build标签覆盖默认或补充配置.

  • 所以本质上来说: 我们配置的build标签都是对超级POM配置的叠加. 因为无法帮助我们定制构建的需求

  • 超级POM位置/apache-maven-3.6.3/lib/maven-model-builder-3.6.3.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.5.3</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-no-fork</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 -->
    
2) build标签的组成
  • 从超级pom的实例中我们可以看出, build标签的子标签大致包含三个主体部分:

    1. 定义约定的目录结

      <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>
      
      标签作用
      sourceDirectory主体源程序存放目录
      scriptSourceDirectory脚本源程序存放目录
      testSourceDirectory测试源程序存放目录
      outputDirectory主题源程序编译结果输出目录
      testOutputDirectory测试源程序编译结果输出目录
      resources主体资源文件存放目录
      testResources测试资源文件存放目录
      directory构件结果输出目录
    2. 备用插件管理

      pluginManagement 标签存放着几个极少用到的插件:

      • maven-antrun-plugin
      • maven-assembly-plugin
      • maven-dependency-plugin
      • maven-release-plugin

      通过 pluginManagement 标签管理起来的插件就像 dependencyManagement 一样,子工程使用时可以省略版本号,起到在父工程中统一管理版本的效果。情看下面例子:

      • 被 spring-boot-dependencies 管理的插件信息:
      <plugin>
      	<groupId>org.springframework.boot</groupId>
      	<artifactId>spring-boot-maven-plugin</artifactId>
      	<version>2.6.2</version>
      </plugin>
      
      • 子工程使用的插件信息:
      <build>
          <plugins>
              <plugin>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-maven-plugin</artifactId>
              </plugin>
          </plugins>
      </build>
      
    3. 生命周期插件

      plugins 标签存放的是默认生命周期中实际会用到的插件,这些插件想必大家都不陌生,所以抛开插件本身不谈,我们来看看 plugin 标签的结构:

      <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.1</version>
          <executions>
              <execution>
                  <id>default-compile</id>
                  <phase>compile</phase>
                  <goals>
                      <goal>compile</goal>
                  </goals>
              </execution>
              <execution>
                  <id>default-testCompile</id>
                  <phase>test-compile</phase>
                  <goals>
                      <goal>testCompile</goal>
                  </goals>
              </execution>
          </executions>
      </plugin>
      

      坐标部分

      作用标签
      artifactId 和 version标签定义了插件的坐标,作为 Maven 的自带插件这里省略了 groupId

      执行部分

      作用标签
      executions标签内可以配置多个 execution 标签
      execution
      execution子标签id指定唯一标识
      execution子标签phase关联的生命周期阶段
      execution子标签goals/goal关联指定生命周期的目标 goals 标签中可以配置多个 goal 标签,表示一个生命周期环节(生命周期要看phase的配置)可以对应当前插件的多个目标。
      execution子标签configuration标签内进行配置时使用的标签是插件本身定义的

      另外,插件目标的执行过程可以进行配置,例如 maven-site-plugin 插件的 site 目标:

      <execution>
      <id>default-site</id>
      <phase>site</phase>
      <goals>
      <goal>site</goal>
      </goals>
      <configuration>
      <!-- 这里就是可以配置生成站点输出的目录 -->
      <outputDirectory>${project.basedir}\target\site</outputDirectory>
      <!-- 以及发布的时候使用特定的插件 -->
      <reportPlugins>
      <reportPlugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-project-info-reports-plugin</artifactId>
      </reportPlugin>
      </reportPlugins>
      </configuration>
      </execution>
      

      configuration 标签内进行配置时使用的标签是插件本身定义的。就以 maven-site-plugin 插件为例,它的核心类是 org.apache.maven.plugins.site.render.SiteMojo,在这个类中我们看到了 outputDirectory 属性:

      image-20220715143436933

      SiteMojo 的父类是:AbstractSiteRenderingMojo,在父类中我们看到 reportPlugins 属性:

      image-20220715143514404

      结论:每个插件能够做哪些配置需要根据这些插件本身都提供了那些个配置来决定的,无法一概而论,需要去看一下对应插件的代码

3) 典型应用: 指定JDK版本
  • 之前是在settings.xml文件中配置了JDK版本, 如下代码所示----》官网解释

    <profiles>
      <profile>
        <id>jdk-1.17</id>
        <activation>
          <activeByDefault>true</activeByDefault>
          <jdk>1.17</jdk>
        </activation>
        <properties>
          <maven.compiler.source>17</maven.compiler.source>
          <maven.compiler.target>17</maven.compiler.target>
          <maven.compiler.compilerVersion>17</maven.compiler.compilerVersion>
        </properties>
      </profile>
    </profiles>
    
  • 如果脱离了本地maven的话那么就相当脱离了本地的setting.xml文件的配置, 意味着如果两个环境的maven配置不同的情况下, 那么就会导致编译某一个可能编译产生问题,

  • 当某一环境中的maven的setting.xml文件中没有配置JDK版本的话则会出现如下报错, 当然我这里使用的是JDK17, 如果是完全没有配置的话, 然后进行了流失变成就会出现1.5 不支持lambda表达式之类的

  • 所以为了防止这种情况的出现我们可以在pom.xml文件中配置build标签来解决如下

    <build>
      <!-- plugins 标签:Maven 你给我听好了,你给我构建的时候要用到这些插件! -->
      <plugins>
        <!-- plugin 标签:这是我要指定的一个具体的插件 -->
        <plugin>
          <!-- 插件的坐标。此处引用的 maven-compiler-plugin 插件不是第三方的,是一个 Maven 自带的插件。 -->
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.1</version>
    
          <!-- configuration 标签:配置 maven-compiler-plugin 插件 -->
          <configuration>
            <!-- 具体配置信息会因为插件不同、需求不同而有所差异 -->
            <source>17</source>
            <target>17</target>
            <encoding>UTF-8</encoding>
          </configuration>
        </plugin>
      </plugins>
    </build>
    
  • 补充说明:

    Java编译器中 的-source参数. 和target参数可以看下方javac -help给出的提示, 同是在maven最新的文档中是建议

    Note: Merely setting the target option does not guarantee that your code actually runs on a JRE with the specified version. The pitfall is unintended usage of APIs that only exist in later JREs which would make your code fail at runtime with a linkage error. To avoid this issue, you can either configure the compiler’s boot classpath to match the target JRE, or use the Animal Sniffer Maven Plugin to verify your code doesn’t use unintended APIs, or better yet use the release option supported since JDK 9. In the same way, setting the source option does not guarantee that your code actually compiles on a JDK with the specified version. To compile your code with a specific JDK version, different than the one used to launch Maven, refer to the Compile Using A Different JDK example.

    翻译:

    注意: 仅设置目标选项并不能保证您的代码实际上在具有指定版本的JRE上运行。缺陷是仅在以后的JREs中存在的api的意外使用,这将使您的代码在运行时失败并出现链接错误。为避免此问题,您可以配置编译器的引导类路径以匹配目标JRE,或者使用Animal Sniffer Maven Plugin来验证您的代码没有使用非预期的 API ,或者更好地使用自JDK 9以来支持的发布选项。同样,设置source选项不能保证您的代码实际上是在具有指定版本的JDK上编译的。要使用与用于启动Maven的版本不同的特定JDK版本编译代码,请参阅使用其他JDK示例进行编译。-------------》进入官网在进入下一城看的话就可以看到release

    ➜  ~ javac -help
    用法: javac <options> <source files>
    其中, 可能的选项包括:
      @<filename>                  从文件读取选项和文件名
      -Akey[=value]                传递给注释处理程序的选项
      --add-modules <模块>(,<模块>)*
            除了初始模块之外要解析的根模块; 如果 <module>
                    为 ALL-MODULE-PATH, 则为模块路径中的所有模块。
      --boot-class-path <path>, -bootclasspath <path>
            覆盖引导类文件的位置
      --class-path <path>, -classpath <path>, -cp <path>
            指定查找用户类文件和注释处理程序的位置
      -d <directory>               指定放置生成的类文件的位置
      -deprecation                 输出使用已过时的 API 的源位置
      --enable-preview             启用预览语言功能。要与 -source 或 --release 一起使用。
      -encoding <encoding>         指定源文件使用的字符编码
      -endorseddirs <dirs>         覆盖签名的标准路径的位置
      -extdirs <dirs>              覆盖所安装扩展的位置
      -g                           生成所有调试信息
      -g:{lines,vars,source}       只生成某些调试信息
      -g:none                      不生成任何调试信息
      -h <directory>               指定放置生成的本机标头文件的位置
      --help, -help, -?            输出此帮助消息
      --help-extra, -X             输出额外选项的帮助
      -implicit:{none,class}       指定是否为隐式引用文件生成类文件
      -J<flag>                     直接将 <标记> 传递给运行时系统
      --limit-modules <模块>(,<模块>)*
            限制可观察模块的领域
      --module <模块>(,<模块>)*, -m <模块>(,<模块>)*
            只编译指定的模块,请检查时间戳
      --module-path <path>, -p <path>
            指定查找应用程序模块的位置
      --module-source-path <module-source-path>
            指定查找多个模块的输入源文件的位置
      --module-version <版本>        指定正在编译的模块版本
      -nowarn                      不生成任何警告
      -parameters                  生成元数据以用于方法参数的反射
      -proc:{none,only}            控制是否执行注释处理和/或编译。
      -processor <class1>[,<class2>,<class3>...]
            要运行的注释处理程序的名称; 绕过默认的搜索进程
      --processor-module-path <path>
            指定查找注释处理程序的模块路径
      --processor-path <path>, -processorpath <path>
            指定查找注释处理程序的位置
      -profile <profile>           请确保使用的 API 在指定的配置文件中可用
      --release <release>
            为指定的 Java SE 发行版编译。支持的发行版:7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17
      -s <directory>               指定放置生成的源文件的位置
      --source <release>, -source <release>
            提供与指定的 Java SE 发行版的源兼容性。支持的发行版:7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17
      --source-path <path>, -sourcepath <path>
            指定查找输入源文件的位置
      --system <jdk>|none          覆盖系统模块位置
      --target <release>, -target <release>
            生成适合指定的 Java SE 发行版的类文件。支持的发行版:7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17
      --upgrade-module-path <path>
            覆盖可升级模块位置
      -verbose                     输出有关编译器正在执行的操作的消息
      --version, -version          版本信息
      -Werror                      出现警告时终止编译
    
  • 或者我们通过在pom中的properties标签中配置

    <properties>
      <maven.compiler.source>${Java.version}</maven.compiler.source>
      <maven.compiler.target>${Java.version}</maven.compiler.target>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    
4) 典型应用:SpringBoot 定制化打包
  • 需求

    很显然spring-boot-maven-plugin并不是Maven自带的插件, 而是SpringBoot提供的, 用来改变M啊ven默认的构建行为. 具体来说是改变打包的行为. 默认情况下Maven调用maven-jar-plugin插件的jar目标, 生成普通的jar包

    普通jar包是无法使用java - jar XXX. jar这样的命令来启动运行的, 但是SpringBoot的设计理念就是每一个[微服务]到处一个jar包, 这个jar包可以使用java -jar xxx.jar这样的米哦您光临直接启动

    所以springboot提供了spring-boot-maven-pulgin来定制这一行为

    <build>
    	<plugins>
    		<plugin>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-maven-plugin</artifactId>
    			<version>2.5.5</version>
    		</plugin>
    	</plugins>
    </build>
    
  • 插件的七个目标

    image-20220717230112315

    目标名称作用
    spring-boot:build-imagePackage an application into a OCI image using a buildpack.
    spring-boot:build-infoGenerate a build-info.properties file based on the content of the current MavenProject.
    spring-boot:helpDisplay help information on spring-boot-maven-plugin. Call mvn spring-boot:help -Ddetail=true -Dgoal= to display parameter details.
    spring-boot:repackageRepackage existing JAR and WAR archives so that they can be executed from the command line using java -jar. With layout=NONE can also be used simply to package a JAR with nested dependencies (and no main class, so not executable).
    spring-boot:runRun an application in place.
    spring-boot:startStart a spring application. Contrary to the run goal, this does not block and allows other goals to operate on the application. This goal is typically used in integration test scenario where the application is started before a test suite and stopped after.
    spring-boot:stopStop an application that has been started by the ‘start’ goal. Typically invoked once a test suite has completed.
5⃣️. 依赖配置补充

官网文档

1) 依赖范围
  • import —> scope标签

    管理依赖追击本的方法就是继承父工程, 但是和Java类一样, Maven也是单继承的, 如果不同体系的依赖信息封装在不同POM中了, 没有办法继承多个父工程怎么办? 这是就可以使用import依赖范围.

    典型案例就是在项目中引入SpringBoot、SpringCloud 依赖:

    <dependencyManagement>
      <dependencies>
    
        <!-- SpringCloud 依赖导入 -->
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-dependencies</artifactId>
          <version>Hoxton.SR9</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
    
        <!-- SpringCloud Alibaba 依赖导入 -->
        <dependency>
          <groupId>com.alibaba.cloud</groupId>
          <artifactId>spring-cloud-alibaba-dependencies</artifactId>
          <version>2.2.6.RELEASE</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
    
        <!-- SpringBoot 依赖导入 -->
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-dependencies</artifactId>
          <version>2.3.6.RELEASE</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
      </dependencies>
    </dependencyManagement>
    

    ⚠️import 依赖范围使用要求:

    • 打包类型必须是 pom —》 type标签中
    • 必须放在 dependencyManagement 中

    官网说明如下:

    scope is only supported on a dependency of type pom in the <dependencyManagement> section. It indicates the dependency is to be replaced with the effective list of dependencies in the specified POM’s <dependencyManagement> section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

    翻译:

    作用域仅支持“”部分中类型为“ pom ”的依赖项。它表示依赖项将被指定 POM 的部分中的有效依赖项列表所替代。由于它们被替换,具有“ import ”范围的依赖项实际上并不参与限制依赖项的传递性。

  • system -----> scope标签 需要配置systemPath

    以 mac 系统环境下开发为例,假设现在 \Users\405\test-maven-1.0-SNAPSHOT.jar想要引入到我们的项目中,此时我们就可以将依赖配置为 system 范围:

    <dependency>
      <groupId>com.azang.maven</groupId>
      <artifactId>test-maven</artifactId>
      <version>1.0-SNAPSHOT</version>
      <systemPath>\Users\405\test-maven-1.0-SNAPSHOT.jar</systemPath>
      <scope>system</scope>
    </dependency>
    

    但是很明显:这样引入依赖完全不具有可移植性,所以尽量不要使用如果需要引入体系外 jar 包我们后面会讲专门的办法。

  • runtime -----> scope标签

    专门用于编译时不需要, 但是运行时需要的jar包. 比如: 编译时我们根据接口调用方法, 但是实际运行时需要的是接口的实现类. 如热部署或者JDBC

    <!--热部署 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <scope>runtime</scope>
      <optional>true</optional>
    </dependency>
    
2) 可选依赖 -----》 optional标签标识可选
  • 配置举例

    <!--热部署 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    
  • 本质含义官网介绍

    可选就是可有可无, 根据具体情况进行分析

    官网解释:

    Introduction

    This section discusses optional dependencies and dependency exclusions. This will help users to understand what they are and when and how to use them. It also explains why exclusions are made on a per dependency basis instead of at the POM level.

    Optional Dependencies

    Optional dependencies are used when it’s not possible (for whatever reason) to split a project into sub-modules. The idea is that some of the dependencies are only used for certain features in the project and will not be needed if that feature isn’t used. Ideally, such a feature would be split into a sub-module that depends on the core functionality project. This new subproject would have only non-optional dependencies, since you’d need them all if you decided to use the subproject’s functionality.

    However, since the project cannot be split up (again, for whatever reason), these dependencies are declared optional. If a user wants to use functionality related to an optional dependency, they have to redeclare that optional dependency in their own project. This is not the clearest way to handle this situation, but both optional dependencies and dependency exclusions are stop-gap solutions.

    Why use optional dependencies?

    Optional dependencies save space and memory. They prevent problematic jars that violate a license agreement or cause classpath issues from being bundled into a WAR, EAR, fat jar, or the like.


    翻译:

    介绍

    本节讨论可选依赖项和依赖项排除。这将帮助用户了解它们是什么以及何时和如何使用它们。它还解释了为什么排除是基于每个依赖而不是在 POM 级别。

    可选依赖项

    当不可能将项目拆分为子模块时(不管出于什么原因),可以使用可选的依赖项。其想法是,一些依赖项只用于项目中的某些特性,如果不使用该特性,则不需要这些特性。理想情况下,这样的特性将被分割成一个依赖于核心功能项目的子模块。这个新的子项目将只有非可选依赖项,因为如果你决定使用子项目的功能,你将需要所有的依赖项。
    但是,由于项目不能分割(同样,不管出于什么原因),这些依赖项被声明为可选的。如果用户希望使用与可选依赖项相关的功能,他们必须在自己的项目中重新声明该可选依赖。这不是处理这种情况的最清晰的方法,但可选依赖项和依赖项排除都是权宜之计。

    为什么要使用可选的依赖关系?

    可选依赖项可以节省空间和内存。它们可以防止违反许可协议或导致类路径问题的有问题的 jar 被捆绑到 WAR 、 EAR 、 fat jar 等中。

  • 举例说明

    当Project X 依赖 Project A,A 中一部分代码是Project X 在运行中(runtime)用不到的代码,然后这部分代码依赖了Project B,那么对 Project X 来说 Project B 就是『可有可无』的。

在这里插入图片描述

3) 版本仲裁(最终使用的依赖可以使用maven dependency:list命令之后进行查看)
  • 最短路径有限

    下图例子中, 对模块pro25-module-a来说, maven会使用log4j1.2.12的版本

    image-20220804225329376

  • 相同路径时先声明者优先

    下图例子中, Maven采纳的版本取决于pro-moudle-x中, 对pr30-moudle-y和pr31-moudle-z两个模块中的对应依赖哪一个先被声明(这里声明的意思就是说在dependencies标签中哪一个denpendency标签排在前面)

    image-20220804230222496

6⃣️. 如何自定义Maven插件

一般来说很难讲什么时候回去写一个maven插件

这里的maven插件千万不要理解成maven依赖

这里的插件指的是maven的插件(plugins)

image-20220804233035123

1) 概念

插件顾名思义就是插件, 只不过在正常的实际开发场景中我们很少用到Maven插件, 所以这里以后用的的话可以来复习, 用不到的话就理解插件目标和生命周期阶段之间的关系

2) 插件开发
  • 首先修改打包方式

    <packaging>maven-plugin</packaging>
    
  • 引入依赖(下面两种方式进行二选一)

    1. 将来在文档注释中使用注解

      <dependency>
          <groupId>org.apache.maven</groupId>
          <artifactId>maven-plugin-api</artifactId>
          <version>3.5.2</version>
      </dependency>
      
    2. 将来直接使用注解

      <dependency>
          <groupId>org.apache.maven.plugin-tools</groupId>
          <artifactId>maven-plugin-annotations</artifactId>
          <version>3.5.2</version>
      </dependency>
      
  • 创建Mojo类

    Mojo 类是一个 Maven 插件的核心类。

    Mojo 这个单词的意思是:Maven Old Java Object,其实 mojo 这个单词本身包含魔力;符咒(袋);护身符;(人的)魅力的含义,Maven 用 Mojo 是因为它是对 POJO 开的一个小玩笑

    • 每一个Mojo都需要实现org.apache.maven.plugin.Mojo接口

      image-20220811231051671

    • AbstractMojo抽象类

      我们实现Mojo接口比较困难, 幸好可以继承AbstractMojo, 此时我们只要实现execute()这个方法即可

      package com.azang;
      
      import org.apache.maven.plugin.AbstractMojo;
      import org.apache.maven.plugin.MojoExecutionException;
      import org.apache.maven.plugin.MojoFailureException;
      
      /**
       * Hello world!
       */
      public class MyPlugin extends AbstractMojo {
      
          @Override
          public void execute() throws MojoExecutionException, MojoFailureException {
              System.out.println("hello-plugin");
          }
      }
      
3) 配置插件
  • Mojo类中的配置(需要注意的是这里要根据依赖的不同来做对应的选择)

    1. 文档注释中用注解

      /**
       * @goal sayHello
       * 对应的maven依赖<artifactId>maven-plugin-api</artifactId>
       */
      public class MyPlugin extends AbstractMojo {
          @Override
          public void execute() throws MojoExecutionException, MojoFailureException {
              System.out.println("hello-plugin");
          }
      }
      
    2. 直接在类上标记注解

      package com.azang;
      
      import org.apache.maven.plugin.AbstractMojo;
      import org.apache.maven.plugin.MojoExecutionException;
      import org.apache.maven.plugin.MojoFailureException;
      import org.apache.maven.plugins.annotations.Mojo;
      
      /**
       * 需要使用的maven依赖是<artifactId>maven-plugin-annotations</artifactId>
       */
      @Mojo(name = "sayHello")
      public class MyPlugin extends AbstractMojo {
          @Override
          public void execute() throws MojoExecutionException, MojoFailureException {
              System.out.println("hello-plugin");
          }
      
      }
      
  • 安装插件(执行mvn install命令)

  • 注册插件

    这里需要将插件moudel对应pom坐标中的 groupId 部分注册到 settings.xml

    <pluginGroups>
    	<!-- pluginGroup
    	 | Specifies a further group identifier to use for plugin lookup.
    	<pluginGroup>com.your.plugins</pluginGroup>
    	-->
    	<pluginGroup>com.azang</pluginGroup>
    </pluginGroups>
    
  • 使用插件

    1. 识别插件前缀(这里对应的artfactId不是乱起的名字, 是你创建插件的moudel对应pom中坐标artfactId是要一一对应的)

      • 前置匹配

        • 匹配规则:${prefix}-maven-plugin
        • artifactId:hello-maven-plugin
        • 前缀:hello
      • 中间匹配

        • 匹配规则:maven-${prefix}-plugin
        • artifactId:maven-good-plugin
        • 前缀:good
    2. 在命令行直接使用

      这里需要在要使用这个插件的moudel的pom中的build标签中配置自定义使用需要引入

      <build>
        <!-- plugins 标签:Maven 你给我听好了,你给我构建的时候要用到这些插件! -->
        <plugins>
          <!-- plugin 标签:这是我要指定的一个具体的插件 -->
          <plugin>
            <!-- 插件的坐标。此处引用的 maven-compiler-plugin 插件不是第三方的,是一个 Maven 自带的插件。 -->
            <groupId>com.azang</groupId>
            <artifactId>maven-hello-plugin</artifactId>
            <version>1.0</version>
            <executions>
              <execution>
                <id>hello</id>
                <!-- 指定和目标关联的生命周期阶段 -->
                <phase>clean</phase>
                <goals>
                  <!-- 这里如果是文章注释方式的则对应的是@goal后面跟着的东西,如果是注解方式的话则对应的是@Mojo注解内name属性对应的 -->
                  <goal>sayHello</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
      

      接下来就可以使用自定义maven插件的命令

      mvn sayHello:sayHello
      

      执行后可在控制台查看具体结果

二. profile详解

1⃣️. profile概述

侧写

  • 项目的不同运行环境

    image-20220813000007213

    通常情况下, 我们至少有三种运行环境:

    • 开发环境: 供不同开发工程师开发的各个模块之间互相调用, 访问; 内部使用
    • 测试环境: 供测试工程师对项目的各个模块进行功能测试; 内部使用
    • 生产环境: 正式运行环境, 对外提供服务

    可是代码只有一套, 所以需要在Maven中, 使用profile机制管理不同环境下的配置信息. 但是解决同类问题的类似机制在其他框架中也有, 而且冲模块划分的角度来说, 持久层的信息放在构建工具(Maven -> setting.xml文件)中也违反了[高内聚, 低耦合]的原则, 所以Maven的profile我们了解一下即可, 不必深究

  • 默认profile

    其实即使我们在pom.xml中没有配置profile标签, 但是也已经用到了profile了, 因为根标签project下厨了modelVersion和坐标标签之外的其他标签其实都可以配置到profile中

2⃣️. profile 配置
  • 外部视角, profile可以在下面两种配置文件中配置:

    • settings.xml:全局生效。其中我们最熟悉的就是配置 JDK 1.8。(个人其实建议这个也最好配置到pom中去,)

      <profiles>
        <profile>
          <!-- id 标签: 唯一标识 -->
          <id>jdk-1.17</id>
          <!-- activation 标签: 激活方式 -->
          <activation>
            <!-- activeByDefault 标签: 是否默认激活 -->
            <activeByDefault>true</activeByDefault>
            <!-- jdk 标签: 标识当前profile可以根据JDK版本来激活 -->
            <jdk>17</jdk>
          </activation>
          <!-- properties 以及内部的其他标签: 当前profile被激活后要采纳的配置-->
          <properties>
            <maven.compiler.source>17</maven.compiler.source>
            <maven.compiler.target>17</maven.compiler.target>
            <maven.compiler.compilerVersion>17</maven.compiler.compilerVersion>
          </properties>
        </profile>
      </profiles>
      
    • pom.xml:当前 POM 生效

  • 内部实现: 具体标签

    • profiles/profile 标签

      • 由于 profile 天然代表众多可选配置中的一个所以由复数形式的 profiles 标签统一管理。
      • 由于 profile 标签覆盖了 pom.xml 中的默认配置,所以 profiles 标签通常是 pom.xml 中的最后一个标签。
    • id 标签

      每个 profile 都必须有一个 id 标签,指定该 profile 的唯一标识。这个 id 标签的值会在命令行调用 profile 时被用到。这个Maven(mvn)命令格式是:-D<profile id>

    • 其他允许出现的标签

      一个 profile 可以覆盖项目的最终名称、项目依赖、插件配置等各个方面以影响构建行为。

      • build
        • defaultGoal
        • finalName
        • resources
        • testResources
        • plugins
      • reporting
      • modules
      • dependencies
      • dependencyManagement
      • repositories
      • pluginRepositories
      • properties
3⃣️. 激活 profile
  • 默认配置默认呗激活

    前面提到了, POM中没有在profile标签里的就是默认的profile,默认是被激活的

  • 基于环境信息激活

    环境信息包括: JDK 版本、操作系统参数、文件、属性等各个方面。一个 profile 一旦被激活,那么它定义的所有配置都会覆盖原来 POM 中对应层次的元素。大家可以参考下面的标签结构

    <profile>
      <id>dev</id>
      <activation>
        <!-- 配置是否默认激活 -->
        <activeByDefault>false</activeByDefault>
        <jdk>1.5</jdk>
        <os>
          <name>Windows XP</name>
          <family>Windows</family>
          <arch>x86</arch>
          <version>5.1.2600</version> 
        </os>
        <property>
          <name>mavenVersion</name>
          <value>2.0.5</value>
        </property>
        <file>
          <exists>file2.properties</exists>
          <missing>file1.properties</missing>
        </file>
      </activation>
    </profile>
    

    这里有个问题是:多个激活条件之间是什么关系呢?

    • Maven 3.2.2 之前:遇到第一个满足的条件即可激活——的关系。
    • Maven 3.2.2 开始:各条件均需满足——的关系。

    下面我们来看一个具体例子。假设有如下 profile 配置,在 JDK 版本为 1.6 时被激活:

    <profiles>
      <profile>
        <id>JDK1.6</id>
        <activation>
          <!-- 指定激活条件为:JDK 1.6 -->
          <jdk>1.6</jdk>
        </activation>
        ……
      </profile>
    </profiles>
    

    这里需要指出的是:Maven 会自动检测当前环境安装的 JDK 版本,只要 JDK 版本是以 1.6 开头都算符合条件。下面几个例子都符合:

    • 1.6.0_03
    • 1.6.0_02
    • ……
  • 命令行列出激活的profile

    # 列出所有激活的 profile,以及它们在哪里定义
    mvn help:active-profiles
    

    日志如下这里配置的激活条件是使用JDK17

    Active Profiles for Project 'com.azang:test-maven-son:jar:1.0-SNAPSHOT':
    
    The following profiles are active:
    
     - JDK1.17 (source: com.azang:test-maven-parent:1.0-SNAPSHOT)
    
    
    
    [INFO] ------------------------------------------------------------------------
    [INFO] Reactor Summary for test-maven-parent 1.0-SNAPSHOT:
    [INFO] 
    [INFO] test-maven-parent .................................. SUCCESS [  0.198 s]
    [INFO] test-maven-son ..................................... SKIPPED
    
  • 指定某个具体 profile
    mvn compile -P <profile id>
    
    • 具体实现首先我们编写一下使用了Jdk8的代码如下

      package com.azang;
      
      import java.util.Optional;
      
      /**
       * Hello world!
       */
      public class App {
          public static void main(String[] args) {
              Optional<Long> longOptional = Optional.of(1L);
          }
      }
      
    • 然后在pom中添加----我们可以从下方的profile配置中不难发现没有配置标签也就意味着我们没有配置对应的激活条件

      <profiles>
        <profile>
          <id>myJDKProfile</id>
          <!-- build 标签:意思是告诉 Maven,你的构建行为,我要开始定制了! -->
          <build>
            <!-- plugins 标签:Maven 你给我听好了,你给我构建的时候要用到这些插件! -->
            <plugins>
              <!-- plugin 标签:这是我要指定的一个具体的插件 -->
              <plugin>
                <!-- 插件的坐标。此处引用的 maven-compiler-plugin 插件不是第三方的,是一个 Maven 自带的插件。 -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
      
                <!-- configuration 标签:配置 maven-compiler-plugin 插件 -->
                <configuration>
                  <!-- 具体配置信息会因为插件不同、需求不同而有所差异 -->
                  <source>1.6</source>
                  <target>1.6</target>
                  <encoding>UTF-8</encoding>
                </configuration>
              </plugin>
            </plugins>
          </build>
        </profile>
      </profiles>
      
    • 使用mvn编译mvn compile -P myJDKProfile日志如下

      [ERROR] COMPILATION ERROR : 
      [INFO] -------------------------------------------------------------
      [ERROR] 不再支持源选项 6。请使用 7 或更高版本。
      [ERROR] 不再支持目标选项 6。请使用 7 或更高版本。
      [INFO] 2 errors 
      [INFO] -------------------------------------------------------------
      [INFO] ------------------------------------------------------------------------
      [INFO] Reactor Summary for test-maven-parent 1.0-SNAPSHOT:
      [INFO] 
      [INFO] test-maven-parent .................................. SUCCESS [  0.000 s]
      [INFO] test-maven-son ..................................... FAILURE [  0.381 s]
      [INFO] ------------------------------------------------------------------------
      [INFO] BUILD FAILURE
      [INFO] ------------------------------------------------------------------------
      [INFO] Total time:  0.416 s
      
    • 这里是因为我们通过命令制定了JDK的版本, 而Optional是出现在1.8中的1.6是没有的所以编译失败

5⃣️. 资源属性过滤(不建议在maven中settiing.xml文件中配置, 仅仅作为了解就好, 一般来说是要修改pom)
  • 简介

    Maven 为了能够通过 profile 实现各不同运行环境切换,提供了一种『资源属性过滤』的机制。通过属性替换实现不同环境使用不同的参数。

  • 操作步骤

    • 配置profile

      <profiles>
        <profile>
          <id>devJDBCProfile</id>
          <properties>
            <dev.jdbc.user>root</dev.jdbc.user>
            <dev.jdbc.password>atguigu</dev.jdbc.password>
            <dev.jdbc.url>http://localhost:3306/db_good</dev.jdbc.url>
            <dev.jdbc.driver>com.mysql.jdbc.Driver</dev.jdbc.driver>
          </properties>
          <build>
            <resources>
              <resource>
                <!-- 表示为这里指定的目录开启资源过滤功能 -->
                <directory>src/main/resources</directory>
      
                <!-- 将资源过滤功能打开 -->
                <filtering>true</filtering>
              </resource>
            </resources>
          </build>
        </profile>
      </profiles>
      
    • 创建待处理的资源文件(.properties)在resources文件夹中内容如下

      dev.user=${dev.jdbc.user}
      dev.password=${dev.jdbc.password}
      dev.url=${dev.jdbc.url}
      dev.driver=${dev.jdbc.driver}
      
    • 执行处理资源命令

      mvn compile -P devJDBCProfile
      
    • 查看编译之后target中的properties文件

      image-20220824214151304

  • 拓展

    • 我们是不是会在resource标签下看到includes和excludes标签. 他们的作用是:

      • includes:指定执行 resource 阶段时要包含到目标位置的资源
      • excludes:指定执行 resource 阶段时要排除的资源
    • 例子我们在pom中添加

      <build>
        <resources>
          <resource>
            <!-- 表示为这里指定的目录开启资源过滤功能 -->
            <directory>src/main/resources</directory>
      
            <!-- 将资源过滤功能打开 -->
            <filtering>true</filtering>
      
            <includes>
              <include>*.properties</include>
            </includes>
      
            <excludes>
              <exclude>jdbc.properties</exclude>
            </excludes>
          </resource>
        </resources>
      </build>
      
    • 执行命令mvn compile -P devJDBCProfile结果如下

      image-20220824223105142

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

社畜阿藏405

挣点钱不丢人吧?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值