如何使用 Maven 原型锁定版本 Dependency/Plugin

How to lock version Dependency/Plugin with Maven archetype

我目前正在研究如何使用 maven 原型修复依赖项和插件的版本。这是我的 archetype-resources/pom.xml 的样子。

    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>${spring-version}</version>

archetype-metadata.xml 看起来像这样:

<requiredProperties>
 <requiredProperty key="spring-version">
   <defaultValue>2.1.5.RELEASE</defaultValue>
 </requiredProperty>
</requiredProperties>

然后我将 属性 添加到 archetype.properties 文件

spring-version=2.1.5.RELEASE

当我从这个原型创建项目时,它会正确显示 2.1.5.RELEASE 版本。 然而,当你有更多的依赖关系时,这种方法似乎不是最好的,或者它不是如何锁定版本的正确方式?

来自 https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

依赖管理

依赖管理部分是一种集中依赖信息的机制。当您有一组继承公共父项的项目时,可以将所有有关依赖项的信息放在公共 POM 中,并在子 POM 中对工件进行更简单的引用。通过一些示例可以最好地说明该机制。给定这两个扩展相同父级的 POM:

项目 A:

<project>
  ...
  <dependencies>
    <dependency>
      <groupId>group-a</groupId>
      <artifactId>artifact-a</artifactId>
      <version>1.0</version>
      <exclusions>
        <exclusion>
          <groupId>group-c</groupId>
          <artifactId>excluded-artifact</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>group-a</groupId>
      <artifactId>artifact-b</artifactId>
      <version>1.0</version>
      <type>bar</type>
      <scope>runtime</scope>
    </dependency>
  </dependencies>
</project>

项目 B:

<project>
  ...
  <dependencies>
    <dependency>
      <groupId>group-c</groupId>
      <artifactId>artifact-b</artifactId>
      <version>1.0</version>
      <type>war</type>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>group-a</groupId>
      <artifactId>artifact-b</artifactId>
      <version>1.0</version>
      <type>bar</type>
      <scope>runtime</scope>
    </dependency>
  </dependencies>
</project>

这两个示例 POM 共享一个共同的依赖关系,每个都有一个 non-trivial 依赖关系。这些信息可以像这样放在父 POM 中:

<project>
  ...
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>group-a</groupId>
        <artifactId>artifact-a</artifactId>
        <version>1.0</version>

        <exclusions>
          <exclusion>
            <groupId>group-c</groupId>
            <artifactId>excluded-artifact</artifactId>
          </exclusion>
        </exclusions>

      </dependency>

      <dependency>
        <groupId>group-c</groupId>
        <artifactId>artifact-b</artifactId>
        <version>1.0</version>
        <type>war</type>
        <scope>runtime</scope>
      </dependency>

      <dependency>
        <groupId>group-a</groupId>
        <artifactId>artifact-b</artifactId>
        <version>1.0</version>
        <type>bar</type>
        <scope>runtime</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

那么两个子pom就简单多了:

<project>
  ...
  <dependencies>
    <dependency>
      <groupId>group-a</groupId>
      <artifactId>artifact-a</artifactId>
    </dependency>

    <dependency>
      <groupId>group-a</groupId>
      <artifactId>artifact-b</artifactId>
      <!-- This is not a jar dependency, so we must specify type. -->
      <type>bar</type>
    </dependency>
  </dependencies>
</project>
<project>
  ...
  <dependencies>
    <dependency>
      <groupId>group-c</groupId>
      <artifactId>artifact-b</artifactId>
      <!-- This is not a jar dependency, so we must specify type. -->
      <type>war</type>
    </dependency>

    <dependency>
      <groupId>group-a</groupId>
      <artifactId>artifact-b</artifactId>
      <!-- This is not a jar dependency, so we must specify type. -->
      <type>bar</type>
    </dependency>
  </dependencies>
</project>

你也可以为插件做这个 What is pluginManagement in Maven's pom.xml?