programing

Maven : 라이브러리에서 추가 한 종속성을 재정의하는 방법

nasanasas 2020. 8. 18. 07:46
반응형

Maven : 라이브러리에서 추가 한 종속성을 재정의하는 방법


내 일반적인 문제는 다음과 같습니다.

내 프로젝트 P는 D의 버전 1.0.1에 의존하는 C에 의존하는 B에 의존하는 A에 의존합니다.

D 버전 1.0.1에 문제가 있으며 다른 모듈을 강제로 사용하고 싶습니다. D에 대한 종속성을 직접 추가하지 않았기 때문에 프로젝트의 POM에서 이것을 선언하는 방법을 모르겠습니다. D에 대한 종속성을 선언 한 것은 C입니다.

중요 :이 경우 버전뿐만 아니라 그룹 및 아티팩트도 변경됩니다. 따라서 종속성 버전을 재정의하는 문제가 아니라 모듈을 제외하고 다른 모듈을 포함하는 것입니다.

구체적인 경우 D는 1.0.1에 버그 가있는 StAX입니다 . 버그의 메모에 따르면 "stax-api-1.0.1 (maven GroupId = stax)을 stax-api-1.0-2 (maven GroupId = javax.xml.stream)로 대체하여 문제가 해결되었습니다." 그냥 시도하고 있어요.

따라서 D = stax : stax-api : jar : 1.0.1 및 C = org.apache.xmlbeans : xmlbeans : jar : 2.3.0

중요한 경우를 대비하여 maven 2.0.9를 사용하고 있습니다.

mvn dependency : tree "의 출력

mvn dependency:tree
[..snip..]
[INFO] +- org.apache.poi:poi-ooxml:jar:3.6:compile
[INFO] |  +- org.apache.poi:poi-ooxml-schemas:jar:3.6:compile
[INFO] |  |  +- org.apache.xmlbeans:xmlbeans:jar:2.3.0:compile
[INFO] |  |  |  \- stax:stax-api:jar:1.0.1:compile

내 프로젝트의 POM에서 "A"에 대해 다음과 같은 종속성이 있습니다.

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.6</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.6</version>
</dependency>

미리 감사드립니다.


현재 pom에 버전을 지정하기 만하면됩니다. 여기에 지정된 버전이 다른 버전보다 우선합니다.

버전 강제 적용
A 버전은 특정 버전으로 현재 POM에서 선언 된 경우 항상 적용되지만, 전이 종속성 사용에 의존하는 경우 다른 poms 다운 스트림에도 영향을 미친다는 점에 유의해야합니다.


자원 :


또는 원하지 않는 종속성을 제외 할 수도 있습니다. STAX는 JDK 1.6에 포함되어 있으므로 1.6을 사용하는 경우 완전히 제외 할 수 있습니다.

아래의 예는 약간 잘못되었습니다. 두 가지 제외 중 하나만 필요하지만 어느 것이 있는지 잘 모르겠습니다. 다른 버전의 Stax가 떠 다니는 경우가 있습니다. 아래의 예에서는 C와 D를 가져온 B를 가져 왔으며 각각 (아직 더 전이적인 종속성을 통해) Stax의 다른 버전을 가져 왔습니다. 그래서 'A'에 대한 의존성에서 두 버전의 Stax를 모두 제외했습니다.

<dependency>
  <groupId>a.group</groupId>
  <artifactId>a.artifact</artifactId>
  <version>a.version</version>
  <exclusions>
    <!--  STAX comes with Java 1.6 -->
    <exclusion>
      <artifactId>stax-api</artifactId>
      <groupId>javax.xml.stream</groupId>
    </exclusion>
    <exclusion>
      <artifactId>stax-api</artifactId>
      <groupId>stax</groupId>
    </exclusion>
  </exclusions>
<dependency>

또한 타사 라이브러리의 종속성을 무시하는 데 어려움이있었습니다. 나는 제외와 함께 scot의 접근 방식을 사용했지만 pom의 최신 버전과의 종속성도 추가했습니다. (Maven 3.3.3을 사용했습니다)

따라서 stAX 예제의 경우 다음과 같습니다.

<dependency>
  <groupId>a.group</groupId>
  <artifactId>a.artifact</artifactId>
  <version>a.version</version>
  <exclusions>
    <!--  STAX comes with Java 1.6 -->
    <exclusion>
      <artifactId>stax-api</artifactId>
      <groupId>javax.xml.stream</groupId>
    </exclusion>
    <exclusion>
      <artifactId>stax-api</artifactId>
      <groupId>stax</groupId>
    </exclusion>
  </exclusions>
<dependency>

<dependency>
    <groupId>javax.xml.stream</groupId>
    <artifactId>stax-api</artifactId>
    <version>1.0-2</version>
</dependency>

</dependencies>루트 pom 태그 안에 넣은 것은 루트 pom의 모든 자식 모듈에 포함됩니다. 모든 모듈이 해당 종속성을 사용하는 경우 이것이 갈 길입니다.

However, if only 3 out of 10 of your child modules use some dependency, you do not want this dependency to be included in all your child modules. In that case, you can just put the dependency inside the </dependencyManagement>. This will make sure that any child module that needs the dependency must declare it in their own pom file, but they will use the same version of that dependency as specified in your </dependencyManagement> tag.

You can also use the </dependencyManagement> to modify the version used in transitive dependencies, because the version declared in the upper most pom file is the one that will be used. This can be useful if your project A includes an external project B v1.0 that includes another external project C v1.0. Sometimes it happens that a security breach is found in project C v1.0 which is corrected in v1.1, but the developers of B are slow to update their project to use v1.1 of C. In that case, you can simply declare a dependency on C v1.1 in your project's root pom inside `, and everything will be good (assuming that B v1.0 will still be able to compile with C v1.1).

참고URL : https://stackoverflow.com/questions/3937195/maven-how-to-override-the-dependency-added-by-a-library

반응형