国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Maven 超級pom、最終有效pom、pom 詳解、settings 詳解

這篇具有很好參考價值的文章主要介紹了Maven 超級pom、最終有效pom、pom 詳解、settings 詳解。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。


超級pom

在項目的 pom.xml 中不進行任何配置,仍然不影響 Maven 構(gòu)建的運行,是因為所有的 pom 文件都會繼承一個默認的配置,這個配置稱為 超級pom,在自己項目中的配置會覆蓋 超級pom 中的配置,未被覆蓋的就會繼續(xù)使用 超級pom 中的配置

超級pom 定義在 maven-model-builder.jar 中,如果想查看其定義,需要將 jar 包解壓,解壓后 超級pom 的完整路徑為: maven 安裝目錄\lib\maven-model-builder-3.6.3.jar\org\apache\maven\model\pom-4.0.0.xml,內(nèi)容如下:

<?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 -->

最終有效pom

真實的項目中,想一眼看懂當前 Maven 工程都使用了什么配置是很困難的,因為一個 Maven 工程最終的 Pom 配置會受到:父工程 pom,當前工程 pom,超級pom,默認生命周期插件、甚至 setttings.xml 文件中 profiles 的影響

最終有效pom:上述會影響 pom 的因素合并后,所形成的可以被 Maven 構(gòu)建時真正使用的最終配置

能夠影響 pom 的因素這么多,如果我們手動挨個比較來確定最終有效的pom 肯定是不可取的,太離譜,我們可以借助 Maven 插件來自動計算,在有 pom.xml 的路徑下使用 maven-help-plugin 插件的 effective-pom 目標就可以

演示

項目的 pom.xml 中,除了 groupId、artifactId、version 之外,沒有其他任何配置,運行 mvn help:effective-pom 后效果如下(從結(jié)果來分析,最終有效pom的結(jié)構(gòu)為:超級pom的配置 + 默認生命周期插件的配置,這也正可以解釋,為何超級pom 中沒有默認生命周期插件的配置,而 Maven 卻能運行構(gòu)建功能,因為在最終的有效Pom中,生命周期插件的配置已經(jīng)被添加):

Effective POMs, after inheritance, interpolation, and profiles are applied:

<?xml version="1.0" encoding="GBK"?>
<!-- ====================================================================== -->
<!--                                                                        -->
<!-- Generated by Maven Help Plugin                                         -->
<!-- See: https://maven.apache.org/plugins/maven-help-plugin/               -->
<!--                                                                        -->
<!-- ====================================================================== -->
<!-- ====================================================================== -->
<!--                                                                        -->
<!-- Effective POM for project 'com.ares5k:ares5k-pom: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>
  <groupId>com.ares5k</groupId>
  <artifactId>ares5k-pom</artifactId>
  <version>1.0-SNAPSHOT</version>
  <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>G:\ares5k-pom\src\main\java</sourceDirectory>
    <scriptSourceDirectory>G:\ares5k-pom\src\main\scripts</scriptSourceDirectory>
    <testSourceDirectory>G:\ares5k-pom\src\test\java</testSourceDirectory>
    <outputDirectory>G:\ares5k-pom\target\classes</outputDirectory>
    <testOutputDirectory>G:\ares5k-pom\target\test-classes</testOutputDirectory>
    <resources>
      <resource>
        <directory>G:\ares5k-pom\src\main\resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>G:\ares5k-pom\src\test\resources</directory>
      </testResource>
    </testResources>
    <directory>G:\ares5k-pom\target</directory>
    <finalName>ares5k-pom-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>G:\ares5k-pom\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>G:\ares5k-pom\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>G:\ares5k-pom\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>G:\ares5k-pom\target\site</outputDirectory>
  </reporting>
</project>

pom 詳解


工程坐標 gav

沒啥好說的,就是聲明當前工程的坐標,用來在 Maven 中進行唯一定位的,簡稱 gav

<groupId>com.ares5k</groupId>
<artifactId>ares5k-pom</artifactId>
<version>1.0-SNAPSHOT</version>

打包方式 packaging

Maven 在執(zhí)行生命周期 package 階段時,會根據(jù)打包方式,在最終有效pom 里添加合適的默認插件,Maven 的打包方式有很多,列舉幾個我常用的:

  • war:項目最終需要放到外置 servlet web 容器運行的場景適合使用,Maven 會將 maven-war-plugin 插件添加到最終有效pom 中當作 package 階段的默認生命周期插件,執(zhí)行 package 階段的構(gòu)建后會生成一個 war 文件

  • jar:想通過 java -jar 直接運行項目的場景適合使用,會將 maven-jar-plugin 插件添加到最終有效pom中當作 package 階段的默認生命周期插件,執(zhí)行 package 階段的構(gòu)建后會生成一個 jar 文件

  • pom:作為父工程或聚合工程的場景適合使用,主要目的是定義工程結(jié)構(gòu),執(zhí)行后,不會生成 jar 或 war 文件

  • maven-plugin:自定義生命周期插件的場景適合使用,Maven 會將 maven-jar-pluginmaven-plugin-plugin 插件添加到最終有效pom 中當作 package 階段的默認生命周期插件,執(zhí)行 package 階段的構(gòu)建后會生成一個 jar 文件

<packaging>jar</packaging>

指定父工程 parent

子模塊中使用,不寫 <relativePath> 時會從本地或遠程倉庫中通過 GAV 來定位父工程,寫 <relativePath> 時會先從 <relativePath> 中指定的相對路徑中尋找父工程,找不到再從本地或遠程倉庫查找

  <parent>
    <groupId>com.ares5k</groupId>
    <artifactId>ares5k-pom-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../ares5k-pom-parent</relativePath>
  </parent>

聚合子模塊 modules

聚合工程中使用,將子模塊聲明在 <module> 后,在聚合工程中執(zhí)行 Maven 生命周期命令時,所有子模塊的相同生命周期命令也都會被執(zhí)行,有兩點要注意:

  1. 不要忘了在聚合工程中指定 <packaging>pom</packaging>
  2. <module> 中的值是子模塊工程的相對路徑,而不是子模塊的 <artifactId>
<modules>
  <!-- 子模塊不在聚合工程目錄中 -->
  <module>../ares5k-module-test</module>
  <!-- 子模塊在聚合工程目錄中 -->
  <module>ares5k-module-inner</module>
</modules>

定義遠程倉庫 repositories

定義下載依賴時的遠程倉庫,Maven 默認有一個中央倉庫 <id>central</id>,我們也可以自己添加新的遠程倉庫,當定義多個遠程倉庫后,下載依賴時會按照倉庫的定義順序依次去查找下載,直到找到依賴為止,如果所有自定義倉庫都未找到所需依賴,那么最后會去 Maven 默認的中央倉庫查找

下面是 Maven 默認中央倉庫的定義,我們可以仿照這個結(jié)構(gòu),添加自己的 <repository>,遠程倉庫內(nèi)部又會分為發(fā)布庫和快照庫,下面代碼中 <snapshots><releases> 就用來指定是去發(fā)布庫下載還是快照庫下載

<repositories>
  <repository>
    <snapshots>
      <enabled>false</enabled>
    </snapshots>
    <releases>
      <enabled>true</enabled>
    </releases>
    <id>central</id>
    <name>Central Repository</name>
    <url>https://repo.maven.apache.org/maven2</url>
  </repository>
</repositories>

定義遠程插件倉庫 pluginRepositories

Maven 下載插件類型依賴時的遠程倉庫,也就是在 <plugin> 中聲明的依賴。用法和前面的 <repositories> 大同小異,其默認的插件遠程倉庫結(jié)構(gòu)如下,想使用自己的插件遠程倉庫時,添加 <pluginRepository> 即可:

<pluginRepositories>
  <pluginRepository>
    <releases>
      <enabled>true</enabled>
    </releases>
    <snapshots>
      <enabled>false</enabled>
    </snapshots>
    <id>central</id>
    <name>Central Repository</name>
    <url>https://repo.maven.apache.org/maven2</url>
  </pluginRepository>
</pluginRepositories>

工程中引入依賴 dependencies

定義在 <dependencies> 中的依賴會被下載到本地倉庫并被工程引用,想添加新的依賴就追加 <dependency> 節(jié)點,然后在節(jié)點中指定待引入依賴的工程坐標,既 gav

<dependencies>
  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.24</version>
  </dependency>
</dependencies>

依賴版本管理 dependencyManagement

常在父工程和子模塊搭配時使用,在父工程的 <dependencyManagement> 中定義版本,在子模塊的 <dependency> 真正引用。在同一工程中也可以同時使用 <dependencyManagement><dependency>,但是沒有意義

上面說的 <dependency> 的節(jié)點路徑是: project->dependencies->dependency,而不是 project->dependencyManagement->dependencies->dependency

<dependencyManagement> 聲明依賴后,其不會觸發(fā)下載和引用,只有在 project->dependencies->dependency 中真正引用后,才會觸發(fā)下載和引用,并且此時可以省略版本信息 <version>

<!-- 定義版本 -->
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.24</version>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <!-- 引入,不需要指定 version  -->
  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
  </dependency>
</dependencies>

指定遠程部署庫 distributionManagement

一般搭建私服時常用,執(zhí)行 Maven 生命周期 deploy 階段后,會將 package 階段打好的包,發(fā)送到私服

私服就是自己搭建的遠程倉庫,一般用 nexus 做私服的比較多,遠程倉庫中又細分發(fā)布庫和快照庫,分別與 <repository> <snapshotRepository> 對應(yīng)

<distributionManagement>
  <repository>
    <id>ares5k-release</id>
    <url>http://xxx.xxx.xxx.xxx:xxxx/repository/release/</url>
  </repository>
  <snapshotRepository>
    <id>ares5k-snapshot</id>
    <url>http://xxx.xxx.xxx.xxx:xxxx/repository/snapshot/</url>
  </snapshotRepository>
</distributionManagement>

構(gòu)建 build

<build> 節(jié)點內(nèi),主要是項目構(gòu)建相關(guān)的配置,大部分場合就是引入和配置一些生命周期插件

設(shè)置項目基礎(chǔ)構(gòu)建信息

主要是針對源代碼路徑、資源文件路徑、編譯后輸出路徑、打包后路徑、打包名稱等進行設(shè)置,這些都是 Maven 默認生命周期插件執(zhí)行時所需要的信息

節(jié)點名 描述
sourceDirectory Java 源代碼路徑
testSourceDirectory 測試用的 Java 源代碼路徑
outputDirectory Java 源代碼編譯后的二進制文件存放路徑
testOutputDirectory 測試用的 Java 源代碼編譯后的二進制文件存放路徑
resources 資源文件路徑
testResources 測試用的資源文件路徑
directory 打包后,包的存放路徑
finalName 打包的包名

示例如下,注意路徑中的正反斜杠:

<build>
  <sourceDirectory>G:/ares5k-pom/src/main/java</sourceDirectory>
  <testSourceDirectory>G:/ares5k-pom/src/test/java</testSourceDirectory>
  <outputDirectory>G:/ares5k-pom/target/classes</outputDirectory>
  <testOutputDirectory>G:/ares5k-pom/target/test-classes</testOutputDirectory>
  <resources>
    <resource>
      <directory>G:/ares5k-pom/src/main/resources</directory>
    </resource>
  </resources>
  <testResources>
    <testResource>
      <directory>G:/ares5k-pom/src/test/resources</directory>
    </testResource>
  </testResources>
  <directory>G:/ares5k-pom/target</directory>
  <finalName>ares5k-pom-3-1.0-SNAPSHOT</finalName>
</build>

引入插件 plugins

通過追加 <plugin> 節(jié)點來新增插件,<executions> 部分是用來將插件與生命周期進行綁定的,如果是 Maven 的默認生命周期插件還好,如果是自己引入的非 Maven 默認生命周期插件,不設(shè)置 <executions> 的話,就無法將插件和生命周期綁定,也就不能通過生命周期構(gòu)建命令直接運行插件,只能通過運行插件的方式來運行插件,<phase> 用來綁定生命周期階段,<goal> 用來指定該階段執(zhí)行的插件目標

上述部分不太理解的,可以看我關(guān)于 maven 自定義插件的文章,相信會有更好的理解

<configuration> 部分是設(shè)置插件的屬性,每個插件都有自己的屬性,所以這個地方無法展開來講

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <version>2.3.7.RELEASE</version>
      <executions>
        <execution>
          <goals>
            <goal>repackage</goal>
          </goals>
          <phase>package</phase>
        </execution>
      </executions>
      <configuration>
        <mainClass>com.ares5k.App</mainClass>
      </configuration>
    </plugin>
  </plugins>
</build>

管理插件 pluginManagement

一般定義在父工程中,網(wǎng)上很多文章都說 <pluginManagement><dependencyManagement> 一樣,聲明插件后不會真正的引入,必須要子模塊的 project->plugins->plugin主動聲明時才能真正的引入,我不知道他們測沒測過,我在 maven 3.6.3 中測試過程是這樣的:

  • 在父工程的 pluginManagement 中聲明一個插件
  • 子工程的 projecet->plugins->plugin 中不主動引入
  • 在子工程 pom.xml 目錄運行構(gòu)建命令

結(jié)果:依然使用了父工程 pluginManagement 中聲明的插件,這個結(jié)果和網(wǎng)上說的完全不同,反正我自己是相信測試結(jié)果,所以 pluginManagement 在我看來并不是與 dependencyManagement 一樣用來管理版本,而是統(tǒng)一管理插件

<pluginManagement>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.1.2</version>
    </plugin>
  </plugins>
</pluginManagement>

備用配置 profiles

當項目需要滿足多環(huán)境配置時,比如開發(fā)環(huán)境、測試環(huán)境、商用環(huán)境、甚至是 Java 8 環(huán)境 和 Java 17 環(huán)境,每個環(huán)境的 pom.xml 配置肯定都有不一樣的地方,如果準備很多份 pom.xml 文件,然后以文件替換的方式來實現(xiàn)環(huán)境切換就很麻煩

上述場景就可以使用備用配置的方式,將各個環(huán)境間相同的配置像以前一樣寫在 pom.xml 中,然后將各環(huán)境間不同的地方,定義在 <profiles> 中作為備用配置,當使用 Maven 執(zhí)行構(gòu)建時,在命令中指定想應(yīng)用的備用配置,讓其加入最終有效pom中,實現(xiàn)環(huán)境切換的效果

<profile> 節(jié)點中可以包函 <build>、<dependencies><repositories> 、<pluginRepositories>、<properties><dependencyManagement>、<distributionManagement> 等信息,但是不能包函 gav、<parent><packaging> 這種工程信息

來個示例

假設(shè)項目有兩個運行環(huán)境,其要求如下:

  1. 要求 環(huán)境 A 和 環(huán)境 B 都使用 spring-boot-starter 模塊
  2. 環(huán)境 A 時,要求引入 spring-boot-maven-plugin 插件來實現(xiàn)打包
  3. 環(huán)境 B 時,要求引入 lombok 模塊

上面這種情況就是很典型的備用配置適用場景,我們可以這樣實現(xiàn)環(huán)境切換:

  1. spring-boot-starter 模塊是兩個環(huán)境都需要的,就正常在 pom.xml 中引入就可以
  2. 為 環(huán)境 A 和 環(huán)境 B 不同的地方,分別定義專屬的備用配置,既分別定義 <profile>

配置如下,每個 <profile> 都必須要有自己的唯一 <id>

<!-- 環(huán)境A 和 環(huán)境B 都要求使用 spring-boot -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>2.4.1</version>
    </dependency>
</dependencies>

<profiles>
    <!-- 環(huán)境A的配置 -->
    <profile>
        <id>ares5k-A</id>
        <build>
            <plugins>
                <!-- 引入 spring-boot-maven-plugin -->
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>2.3.7.RELEASE</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                            <phase>package</phase>
                        </execution>
                    </executions>
                    <configuration>
                        <mainClass>com.ares5k.App</mainClass>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

    <!-- 環(huán)境B的配置 -->
    <profile>
        <id>ares5k-B</id>
        <dependencies>
            <!-- 引入 lombok -->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.24</version>
            </dependency>
        </dependencies>
    </profile>
</profiles>

現(xiàn)在 pom.xml 已經(jīng)配置完成,之后構(gòu)建時通過語法 mvn 生命周期階段 -P<profile Id> 就可以實現(xiàn)環(huán)境切換了

以上面配置為例,用環(huán)境A的備用配置打包,命令為 mvn package -Pares5k-A

假設(shè)經(jīng)常使用環(huán)境A的配置進行打包,那么每次輸入命令都需要指定 <profile Id> 就顯得很麻煩,我們也可以在 <profile> 節(jié)點下添加激活條件:

<profile>
  <id>ares5k-A</id>
  <!-- 激活條件為默認激活 -->
  <activation>
    <activeByDefault>true</activeByDefault>
  </activation>
  <build>
    <plugins>
    .......
</profile>

添加了上面的配置后,環(huán)境A 的備用配置就變成了默認激活,以后再想用 環(huán)境A 的配置構(gòu)建項目時,直接輸入mvn package 就可以,不需要指定 <profile Id> 了。<activation> 下的激活方式有很多,<activeByDefault> 則是默認激活的意思,其他的激活方式有:

<!-- 是否默認激活 -->
<activeByDefault>false</activeByDefault>

<!-- 當 Java 版本是 1.8 時,自動激活 -->
<jdk>1.8</jdk>

<!-- 當操作系統(tǒng)是 WinXp 32位時,自動激活 -->
<os>
  <name>Windows XP</name>
  <family>Windows</family>
  <arch>x86</arch>
</os>

<!-- 當構(gòu)建時,傳入的屬性 ares5k-prop=6666 時,自動激活 -->
<!-- 例如:mvn package -Dares5k-prop=6666 -->
<property>
  <name>ares5k-prop</name>
  <value>6666</value>
</property>

<!-- 當文件存在時自動激活 -->
<file>
  <exists>env.properties</exists>  
</file>

<!-- 當文件不存在時自動激活 -->
<file>
  <missing>env.properties</missing>
</file>

當指定了多個激活條件時,Maven 3.2.2 之前只要滿足一個條件,備用配置就會被激活,Maven 3.2.2 開始,必須所有條件都滿足,備用配置才會被激活

settings 詳解

Maven 核心配置文件位置:Maven 安裝目錄/conf/settings.xml,這是全局級別的配置文件,如果將其復(fù)制到用戶目錄內(nèi)(以 Windows 為例, C:\Users\{用戶文件夾}\.m2) 中,那么它的級別會變成用戶級,當兩個文件都存在,用戶目錄內(nèi)的優(yōu)先級更高

這個文件沒啥太多說的,因為節(jié)點很少,而且原始的文件中,對每一個節(jié)點都有詳細的注釋說明,我這里就對幾個常用的聊一聊

設(shè)置本地倉庫路徑 localRepository

Maven 從遠程倉庫下載依賴后,會將依賴放到本地倉庫中,等未來在需要時就直接用本地倉庫中的依賴,默認的本地倉庫路徑在用戶目錄內(nèi)(以 Windows 為例 C:\Users\{用戶文件夾}\.m2\repository), 可以通過 <localRepository> 修改本地倉庫路徑,修改后再次下載的依賴會存放在新的路徑內(nèi),查找本地倉庫時也會在新的路徑內(nèi)查找

<!-- localRepository
 | The path to the local repository maven will use to store artifacts.
 |
 | Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>E:\maven\repository</localRepository>

查找自定義插件 pluginGroups

想在項目中使用自定義 Maven 插件時使用,<pluginGroup> 中指定自定義插件的 <groupId> 即可

<!-- pluginGroups
 | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
 | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
 | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
 |-->
<pluginGroups>
  <!-- pluginGroup
   | Specifies a further group identifier to use for plugin lookup.
  <pluginGroup>com.your.plugins</pluginGroup>
  -->
  <pluginGroup>com.ares5k</pluginGroup>
</pluginGroups>

設(shè)置倉庫鏡像 mirrors

通過 <mirrorOf> 指定要攔截的遠程倉庫ID,當 Maven 對遠程倉庫發(fā)起請求時,會將目標地址替換成 <url>

<!-- mirrors
 | This is a list of mirrors to be used in downloading artifacts from remote repositories.
 |
 | It works like this: a POM may declare a repository to use in resolving certain artifacts.
 | However, this repository may have problems with heavy traffic at times, so people have mirrored
 | it to several places.
 |
 | That repository definition will have a unique id, so we can create a mirror reference for that
 | repository, to be used as an alternate download site. The mirror site will be the preferred
 | server for that repository.
 |-->
<mirrors>
  <!-- mirror
   | Specifies a repository mirror site to use instead of a given repository. The repository that
   | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
   | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
   |
  <mirror>
    <id>mirrorId</id>
    <mirrorOf>repositoryId</mirrorOf>
    <name>Human Readable Name for this Mirror.</name>
    <url>http://my.repository.com/repo/path</url>
  </mirror>
  -->
  <mirror>		
    <id>aliyun</id>
    <name>aliyun</name>
    <mirrorOf>central</mirrorOf>
    <url>http://maven.aliyun.com/nexus/content/groups/public</url>		
  </mirror>
</mirrors>

設(shè)置倉庫訪問密碼

當倉庫需要授權(quán)訪問時,我們必須通過 <servers> 設(shè)置訪問時的賬號密碼,否則無法下載或上傳模塊,server->id 的值不能亂寫,必須在下面幾個中選擇:

  • 與部署庫發(fā)布庫ID相同:節(jié)點路徑,distributionManagement -> repository -> id
  • 與部署庫快照庫ID相同:節(jié)點路徑,distributionManagement -> snapshotRepository-> id
  • 與插件遠程倉庫ID相同:節(jié)點路徑,pluginRepositories -> pluginRepository-> id
  • 與遠程倉庫ID相同:節(jié)點路徑,repositories -> repository-> id
  • 與鏡像倉庫ID相同:節(jié)點路徑,mirrors -> mirror -> id
<servers>
  <!-- server
   | Specifies the authentication information to use when connecting to a particular server, identified by
   | a unique name within the system (referred to by the 'id' attribute below).
   |
   | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
   |       used together.
   |
  <server>
    <id>deploymentRepo</id>
    <username>repouser</username>
    <password>repopwd</password>
  </server>
  -->

  <!-- Another sample, using keys to authenticate.
  <server>
    <id>siteServer</id>
    <privateKey>/path/to/private/key</privateKey>
    <passphrase>optional; leave empty if not used.</passphrase>
  </server>
  -->
  <!-- 為 id=ares5k-nexus 的倉庫設(shè)置訪問密碼 -->
  <server>
    <id>ares5k-nexus</id>
    <username>ares5k</username>	
    <password>123456</password>
  </server>
</servers>

<mirrors>
  <!-- 鏡像倉庫需要授權(quán)訪問 -->
  <mirror>		
    <id>ares5k-nexus</id>
    <name>aliyun</name>
    <mirrorOf>central</mirrorOf>
    <url>http://xxx.xxx.x.xx:xxxx/ares5k/public</url>		
  </mirror>
</mirrors>

備份配置 profiles

和前面 pom.xml 中的 <profiles> 一樣,只不過 settings.xml 中的是全局級別,pom.xml 中的是項目級別

激活備份配置 activeProfiles

在前面講的 pom.xml 中定義 <profile> 的知識點中,介紹了兩種啟用備份配置的方式:

  1. 運行構(gòu)建命令時,通過 -P<profile id> 指定激活的備份配置
  2. <profile> 節(jié)點下通過添加 <activation> 節(jié)點來激活備份配置

settings.xml 中多了一種方式來激活備份配置,就是利用 <activeProfiles> 節(jié)點,在 <activeProfiles> 的子節(jié)點<activeProfile> 中指定 <profile.id> 就可以激活對應(yīng)的備份配置

<!-- 定義備份配置 -->
<profiles>
  <profile>   
    <id>ares5k</id>   
    <properties>   
      <maven.compiler.source>1.8</maven.compiler.source>   
      <maven.compiler.target>1.8</maven.compiler.target>    
    </properties>   
  </profile>
<profiles>

<!-- activeProfiles
 | List of profiles that are active for all builds.
 |
<activeProfiles>
  <activeProfile>alwaysActiveProfile</activeProfile>
  <activeProfile>anotherAlwaysActiveProfile</activeProfile>
</activeProfiles>
-->
<!-- 啟用備份配置 -->
<activeProfiles>
  <activeProfile>ares5k</activeProfile>
</activeProfiles>

網(wǎng)絡(luò)代理 proxies

當網(wǎng)絡(luò)訪問不順暢或無法訪問外部網(wǎng)絡(luò)時,可以設(shè)置訪問代理服務(wù)器,由代理服務(wù)器真正發(fā)起網(wǎng)絡(luò)請求

說實話我沒用過這個功能,因為我一直用的國內(nèi)鏡像倉庫, 網(wǎng)絡(luò)訪問一直順暢,還沒有機會使用這個功能,我把原始配置文件中這部分的注釋粘過來了,感覺沒啥東西,實際使用時對照注釋中的例子簡單改改就行文章來源地址http://www.zghlxwxcb.cn/news/detail-489495.html

<proxies>
  <!-- proxy
   | Specification for one proxy, to be used in connecting to the network.
   |
  <proxy>
    <id>optional</id>
    <active>true</active>
    <protocol>http</protocol>
    <username>proxyuser</username>
    <password>proxypass</password>
    <host>proxy.host.net</host>
    <port>80</port>
    <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
  </proxy>
  -->
</proxies>

到了這里,關(guān)于Maven 超級pom、最終有效pom、pom 詳解、settings 詳解的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • maven中JDK版本的修改,setting.xml修改以及pom.xml的文件修改

    maven中JDK版本的修改,setting.xml修改以及pom.xml的文件修改

    修改已經(jīng)建好的maven的JDK的版本,在父模塊中修改JDK的信息 在這里先看一下本地的配置,我這里先設(shè)置的17版本 出現(xiàn)這種情況的解決辦法 先修改根目錄的setting.xml文件,將里邊的JDK版本全部換為1.8,然后保存即可。將17全部換為1.8 setting.xml的路徑為:D:mavenconf(這是我安裝的

    2024年02月16日
    瀏覽(20)
  • IDEA創(chuàng)建spring boot項目無法加載出maven里的pom.xml,而是settings.gradle

    IDEA創(chuàng)建spring boot項目無法加載出maven里的pom.xml,而是settings.gradle

    在創(chuàng)建的maven項目里找不到pom.xml文件而是settings.gradle,如下圖所示: 原因和解決方法: 是因為在創(chuàng)建Springboot項目的時候type里選的是Gradle,gradle和maven又不一樣,換成maven就行了,具體操作如下圖所示: 在用idea創(chuàng)建maven項目時,在Project Metadata界面Type選項下唯有Maven和Maven PO

    2024年02月09日
    瀏覽(85)
  • 【親測有效】解決idea中maven項目的pom文件不會自動下載jar包問題 + 更新不完整依賴命令

    【親測有效】解決idea中maven項目的pom文件不會自動下載jar包問題 + 更新不完整依賴命令

    一杯茶,一袋煙,一個bug改一天,唉~ 一天下來整個人都麻了; 廢話不多說了,作者創(chuàng)作本文旨在使用言簡意賅的語言為同樣遇到此問題的伙伴們盡快解決困難,少走彎路。 看問題:idea中maven項目的pom文件不會自動下載jar包 setting —— maven —— 去掉work offline 的勾 Settings -

    2024年02月06日
    瀏覽(31)
  • Maven--pom.xml文件詳解

    1.pom簡介 pom指的是project object model,又叫項目對象模型。Maven的pom文件是一個XML文件,用于描述項目的各種屬性、依賴和構(gòu)建信息,包括項目的名稱、版本、許可證、作者、描述、依賴關(guān)系、構(gòu)建過程、插件等??偟膩碚f,POM文件是Maven工程的基本工作單元,它包含了項目的所

    2024年01月19日
    瀏覽(24)
  • maven的pom.xml文件詳解

    2024年02月12日
    瀏覽(29)
  • maven之pom.xml配置文件詳解

    maven之pom.xml配置文件詳解

    pom代表項目對象模型,它是Maven中工作的基本組成單位。它是一個XML文件,在項目的根目錄中。pom包含的對象是使用maven來構(gòu)建的,pom.xml文件包含了項目的各種配置信息。 創(chuàng)建一個POM之前,應(yīng)該要先決定項目組(groupId),項目名(artifactId)和版本(version),因為這些屬性在項目倉

    2023年04月09日
    瀏覽(21)
  • Maven聚合項目(微服務(wù)項目)創(chuàng)建流程,以及pom詳解

    Maven聚合項目(微服務(wù)項目)創(chuàng)建流程,以及pom詳解

    1、首先創(chuàng)建springboot項目作為父項目 只留下pom.xml 文件,刪除src目錄及其他無用文件 ?2、創(chuàng)建子項目 子項目可以是maven項目,也可以是springboot項目 3、父子項目關(guān)聯(lián) ?4、父項目中依賴管理 ? depedencyManagement標簽介紹 在maven的聚合工程中,父模塊的pom文件中,使用dependencyManag

    2024年02月11日
    瀏覽(34)
  • Maven--settings.xml配置詳解

    一、settings.xml配置文件簡介 Maven的settings.xml文件是一個全局配置文件,用于定義Maven的參數(shù)和配置。這個文件包含了Maven的一些高級配置,比如本地倉庫的位置、代理設(shè)置、認證信息等。settings.xml文件位于Maven的安裝目錄下的conf文件夾中,在使用Maven的時候如果未指定settings.

    2024年01月19日
    瀏覽(24)
  • 【Maven】Maven 中 pom.xml 文件

    Maven 是一個項目管理工具,可以對 Java 項目進行構(gòu)建和管理依賴。 本文,我們認識下 pom.xml 文件。POM(Project Object Model, 項目對象模型) 是 Maven 工程的基本工作單位,也是 Maven 的核心。其包含項目的基本信息,用于描述項目如何構(gòu)建、聲明項目依賴等。 POM 是 Project Object Mod

    2024年02月15日
    瀏覽(23)
  • maven的pom文件

    maven的pom文件

    maven項目中會有pom文件, 當新建項目時候, 需要添加我們需要的依賴包。所以整理了一份比較常用的依賴包的pom,方便以后新建項目或者添加依賴包時copy且快捷。不需要的依賴可以刪掉,避免首次遠程拉取失敗和縮小項目打包大小。 pom結(jié)構(gòu)說明解說如下 properties 定義全局變

    2024年02月08日
    瀏覽(29)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包