如何使用Maven原型锁定版本依赖关系/插件

时间:2019-06-04 11:34:12

标签: maven 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版本。 但是,当您有更多依赖项时,这种方法似乎不是最好的方法,或者这不是锁定版本的正确方法?

1 个答案:

答案 0 :(得分:1)

来自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共享一个公共依存关系,每个都有一个不平凡的依存关系。此信息可以这样放置在父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?