Maven: how to override the dependency added by a library

Here's my generic problem:

My project P depends on A which depends on B which depends on C which depends on version 1.0.1 of D.

There's a problem with version 1.0.1 of D and I want to force the use of another module. I don't know how to declare this in my project's POMs since I haven't added a dependency on D directly. It's C which declared the dependency on D.

Important: In this case, not only the version is changed, but the group & artifact as well. So it's not just a matter of overriding the version of the dependency, but rather, of excluding a module and including another one.

In the concrete case, D is StAX whose 1.0.1 has a bug. According to the notes in the bug, "the problems were solved by replacing the stax-api-1.0.1 (maven GroupId = stax) by stax-api-1.0-2 (maven GroupId = javax.xml.stream)" so I'm trying just that.

Thus, D = stax:stax-api:jar:1.0.1 and C = org.apache.xmlbeans:xmlbeans:jar:2.3.0

I'm using maven 2.0.9 in case it matters.

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

In my project's POM I have the following dependency on "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>

Thanks in advance.


Simply specify the version in your current pom. The version specified here will override other.

Forcing a version
A version will always be honoured if it is declared in the current POM with a particular version - however, it should be noted that this will also affect other poms downstream if it is itself depended on using transitive dependencies.


Resources :

  • Dependency Mediation and Conflict Resolution
  • Introduction to the Dependency Mechanism

  • Alternatively, you can just exclude the dependency that you don't want. STAX is included in JDK 1.6, so if you're using 1.6 you can just exclude it entirely.

    My example below is slightly wrong for you - you only need one of the two exclusions but I'm not quite sure which one. There are other versions of Stax floating about, in my example below I was importing A which imported B which imported C & D which each (through yet more transitive dependencies) imported different versions of Stax. So in my dependency on 'A', I excluded both versions of 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>
    

    I also had trouble overruling a dependency in a third party library. I used scot's approach with the exclusion but I also added the dependency with the newer version in the pom. (I used Maven 3.3.3)

    So for the stAX example it would look like this:

    <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>
    
    链接地址: http://www.djcxy.com/p/29702.html

    上一篇: Maven POM依赖管理与子模块

    下一篇: Maven:如何覆盖由库添加的依赖项