본문 바로가기
Dependency Manager/Maven

[Tag] dependencyManagement

by BAYABA 2021. 9. 16.
  1. 개인 공부 목적으로 작성한 글입니다.
  2. 아래 출처를 참고하여 작성하였습니다.

1. Dependency Management 태그

  1. Dependency Management 태그는 종속성 버전 정보를 한데 모으는데 사용하는 태그입니다. 예를 들어 서로 다른 두 모듈이 공통으로 사용하는 라이브러리가 있다면, 공통된 종속성에 대한 정보를 Dependency Management에 넣으면 하위 모듈 pom.xml에서는 버전 정보를 기입하지 않아도 됩니다.

2. 사용 예시

  1. 각 각의 모듈 Project A와 Project B가 있다고 가정하겠습니다.
<!-- before Project 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>

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

  1. 이 때 두 모듈이 의존하는 라이브러리에 대한 버전 정보를 dependencyManagement 태그를 사용해서 상위 pom.xml에 한 번에 기입할 수 있습니다.
<!-- parent pom.xml -->

<project>
  ...
  <dependencyManagement>
    <dependencies>
      <!-- for Project A -->
      <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>

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

      <!-- for Project A, B -->
      <dependency>
        <groupId>group-a</groupId>
        <artifactId>artifact-b</artifactId>
        <version>1.0</version>
        <type>bar</type>
        <scope>runtime</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

  1. 그러면 각 각의 A, B 모듈에서는 참조하는 라이브러리에 대한 버전을 기입하지 않아도 됩니다.
<!-- after Project A -->

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

<!-- after Project B -->
<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>

출처

  1. Introduction to the Dependency Mechanism

'Dependency Manager > Maven' 카테고리의 다른 글

maven-surefire-plugin:2.18.1:test failed  (0) 2021.10.11
spring-boot-maven-plugin  (0) 2021.09.28
Maven 멀티 모듈 구성방법  (0) 2021.09.27
Maven Wrapper 사용법  (0) 2021.09.26