Maven: remove a single transitive dependency

前端 未结 3 1807
猫巷女王i
猫巷女王i 2020-12-02 20:52

My project includes a jar file because it is listed as a transitive dependency.

However, I have verified not only that I don\'t need it but it causes problems becaus

相关标签:
3条回答
  • 2020-12-02 21:07

    You can do this by explicitly excluding the problematic artifact. Take the dependency that includes the problem and mark it to be excluded:

    From the maven website:

    <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>
    
    0 讨论(0)
  • 2020-12-02 21:09

    You can exclude a dependency in the following manner:

            <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                    <version>2.5.6</version>
                    <exclusions>
                            <exclusion>
                                    <groupId>commons-logging</groupId>
                                    <artifactId>commons-logging</artifactId>
                            </exclusion>
                    </exclusions>
            </dependency>
    
    0 讨论(0)
  • 2020-12-02 21:10

    The correct way is to use the exclusions mechanism, however sometimes you may prefer to use the following hack instead to avoid adding a large number of exclusions when lots of artifacts have the same transitive dependency which you wish to ignore. Rather than specifying an exclusion, you define an additional dependency with a scope of "provided". This tells Maven that you will manually take care of providing this artifact at runtime and so it will not be packaged. For instance:

        <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring</artifactId>
                <version>2.5.6</version>
        </dependency>
        <dependency>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                <version>1.1.1</version>
                <scope>provided</scope>
        </dependency>
    

    Side effect: you must specify a version of the artifact-to-be-ignored, and its POM will be retrieved at build-time; this is not the case with regular exclusions. This might be a problem for you if you run your private Maven repository behind a firewall.

    0 讨论(0)
提交回复
热议问题