반응형

Maven - Manage Dependencies


(원문 위치 : http://www.tutorialspoint.com/maven/maven_manage_dependencies.htm )

Maven의 core특징 중 하나는 의존관계 관리이다. 의존관계들을 관리하는 것은 다중 모듈 프로젝트(수백개의 모듈/서브프로젝트로 구성된)를 다루어야 하면 어려운 작업이 된다.

Transitive Dependencies Discovery

라이브러가 A가 B를 이야기하는 다른 라이브러리 B에 의존하는 경우는 꽤 자주 있다. 다른 프로젝트 C가 A를 사용하길 바라는 경우에서는 그 프로젝트는 역시 라이브러리 B 사용을 요청한다.

Maven은 필요한 모든 라이브러리를 찾기위한 요청을 피하도록 한다. Maven은 의존관계들의 프로젝트 파일(pom.xml을 읽는 것으로 그렇게 한다. 의존관계들을 알고 등등등...

개발자는 단지 각 프로젝트 pom에 직접적인 의존과계를 정의하면 된다. Mavendㅡㄴ 자동적으로 나머지를 관리한다.

추의적?(transitive) 의존관계로 포함되는 라이브러리의 그래프는 빠르게 확장될 수 있다. 중복된 라이브러리가 있을 경우가 발생할 수 있다. Maven은 추의적 의존관계의 확장을 제어하는 몇가지 특징을 제공한다.
FeatureDescription

Dependency mediation
(의존관계 중재)

Determines what version of a dependency is to be used when multiple versions of an artifact are encountered. If two dependency versions are at the same depth in the dependency tree, the first declared dependency will be used.
(생성물의 여러 버전이 존재할 때 의존관계의 어떤 버전이 사용될지 결정한다. 만약 두 의존관계 버전이 의존관계 트리에서 같은 깊이에 있다면, 첫번째로 선언되어진 의존관계를 사용할 것이다.)

Dependency management
(의존관계 관리)

Directly specify the versions of artifacts to be used when they are encountered in transitive dependencies. For an example project C can include B as a dependency in its dependencyManagement section and directly control which version of B is to be used when it is ever referenced.
(추의적 의존관계에서 충돌될때 사용될 생성물의 버전을 직접적으로 명시한다. 예를 들어, 프로젝트 C는 의존관계 관리에서 의존관계로써 B를 포함할 수 있고 이것이 참조된적이 있을 때 B의 버전이 사용되는 것을 직접적으로 제어한다.)

Dependency scope
(의존관계 범위)
Includes dependencies as per the current stage of the build
(현재 빌드 단계에 따라 의존관계들을 포함한다.)

Excluded dependencies
(포함되지 않는 의존관계들)

Any transitive dependency can be excluede using "exclusion" element. As example, A depends upon B and B depends upon C then A can mark C as excluded.
(어떠한 추의적 의존관계는 'exclusion'요소를 사용하여 제외되어질 수 있다. A가 B를 의존하고 B가 C를 의존하면 A는 제외되는 것으로 C를 표시할 수 있다.)

Optional dependencies
(선택적인 의존관계들)

Any transitive dependency can be marked as optional using "optional" element. As example, A depends upon B and B depends upon C. Now B marked C as optional. Then A will not use C.
(어떠한 추의적 의존관계는 'optional'요소를 사용하여 선택적으로 표시되어질 수 있다. 예를 들면, A는 B를 의존하고 B는 C를 의존한다. 이제 B는 선택적으로 C를 표시한다. 그러면 A는 C를 사용하지 않을 것이다.)

Dependency Scope

추의적 의존관계 발견은  아래 언급된 것과같이 여러 의존관계 범위를 사용하여 제한되어질 수 있다.
ScopeDescription
compile

This scope indicates that dependency is available in classpath of project. It is default scope.
(이 범위는 의존관계가 프로젝트의 classpath에서 가능함을 나타낸다. 이는 기본 범위이다.)

provided

This scope indicates that dependency is to be provided by JDK or web-Server/Container at runtime
(이 범위는 의존관계가 JDK 또는 런타임에 web-Server/Container에 의해 제공되어지는 것을 나타낸다.)

runtime

This scope indicates that dependency is not required for compilation, but is required during execution.
(이 범위는 의존관계가 컴파일을 위해 요구되지 않음을 나타내지만, 실행동안에는 필요함을 나타낸다.)

testThis scope indicates that the dependency is only available for the test compilation and execution phases.
(이 범위는 의존관계가 단지 테스트 컴파일과 실행 단계에서 가능함을 나타낸다.)
system

This scope indicates that you have to provide the system path.
(이 범위는 system path를 제공해야함을 나타낸다.)

import

This scope is only used when dependency is of type pom. This scopes indicates that the specified POM should be replaced with the dependencies in that POM's <dependencyManagement> section.
(이 범위는 단지 의존관계가 pom형태일때 사용된다. 이 범위는 명시된 POM이 POM의 <dependencyManagement> 섹션의 의존관계와 대체되는 것을 나타낸다.)

Dependency Management

보통 공통 프로젝트 아래 프로젝트를 설정한다. 이런 경우 모든 공통 의존관계를 갖는 공통 pom을 만들 수 있고 이 pom의 하위 프로젝트 pom의 부모로 만든다. 아래 예는 이러한 개념을 이해하는데 도움을 줄 것이다.
dependency graph

아래는 위 의존관계 도표의 상세이다.

- App-UI_WAR는  APP-Core-lib와 App-Data-lib를 의존한다.

- Root는 App-Core-Lib와 App-Data-lib의 부모이다.

- Root는 의존관계 섹션에 의존관계로써 Lib1, lib2, Lib3 fmf wjddmlgksek.


App-UI-WAR

<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
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.companyname.groupname</groupId>
      <artifactId>App-UI-WAR</artifactId>
      <version>1.0</version>
      <packaging>war</packaging>
      <dependencies>
         <dependency>
            <groupId>com.companyname.groupname</groupId>
            <artifactId>App-Core-lib</artifactId>
            <version>1.0</version>
         </dependency>
      </dependencies>  
      <dependencies>
         <dependency>
            <groupId>com.companyname.groupname</groupId>
            <artifactId>App-Data-lib</artifactId>
            <version>1.0</version>
         </dependency>
      </dependencies>  
</project>

App-Core-lib

<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
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <parent>
         <artifactId>Root</artifactId>
         <groupId>com.companyname.groupname</groupId>
         <version>1.0</version>
      </parent>
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.companyname.groupname</groupId>
      <artifactId>App-Core-lib</artifactId>
      <version>1.0</version> 
      <packaging>jar</packaging>
</project>

App-Data-lib

<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
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <parent>
         <artifactId>Root</artifactId>
         <groupId>com.companyname.groupname</groupId>
         <version>1.0</version>
      </parent>
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.companyname.groupname</groupId>
      <artifactId>App-Data-lib</artifactId>
      <version>1.0</version>   
      <packaging>jar</packaging>
</project>

Root

<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
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.companyname.groupname</groupId>
      <artifactId>Root</artifactId>
      <version>1.0</version>
	  <packaging>pom</packaging>
      <dependencies>
         <dependency>
            <groupId>com.companyname.groupname1</groupId>
            <artifactId>Lib1</artifactId>
            <version>1.0</version>
         </dependency>
      </dependencies>  
      <dependencies>
         <dependency>
            <groupId>com.companyname.groupname2</groupId>
            <artifactId>Lib2</artifactId>
            <version>2.1</version>
         </dependency>
      </dependencies>  
      <dependencies>
         <dependency>
            <groupId>com.companyname.groupname3</groupId>
            <artifactId>Lib3</artifactId>
            <version>1.1</version>
         </dependency>
      </dependencies>  
</project>

이제 App-UI-WAR 프로젝트를 빌드할 때 Maven은 의존관계 그래프를 가로지는 것으로 모든 의존관계를 발견할 것이고 프로그램을 빌드할 것이다.


위의 예로부터, 몇가지 중요 개념을 배울 수 있다.

- 곤통 의존관계는 부모 pom의 개념을 사용하여 단일 공공에 위치시킬 수 있다. App-Data-lib와 App-Core-lib 프로젝트의 의존관계들은 Root 프로젝트에서 list되어 진다.(Root의 패키징 타입을 보자. 이것은 POM 이다.)

- App-UI-WAR에서 의존관계로써 Lib1, lib2, Lib3를 녕시할 필요가 없다. Maven은 그같은 상세를 관리하기 위해 추의적 의존관계 매커니즘을 사용한다.

반응형

+ Recent posts