Как заблокировать версию Dependency / Plugin с архетипом Maven - PullRequest
0 голосов
/ 04 июня 2019

В настоящее время я работаю над тем, как исправить версию зависимостей и плагинов с помощью архетипа 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 Ответ

1 голос
/ 05 июня 2019

от 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>

Тогда два дочерних помпона станут намного проще:

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

Вы можете сделать это также для плагинов с Что такое pluginManagement в pom.xml Maven?

...