9

我的 pom 中有两个依赖项,称为 A 和 B。A 和 B 都对工件 C(cassandra-all)具有传递依赖项。A 和 B 使用 C 的不同版本。依赖项 A 是工件astyanax

我想保留B 附带的 C版本。我通过在 A (Astyanax) 中为 C 添加排除来完成。

不幸的是,我希望 B 的范围是“测试”。这意味着在 A 中排除,C 将不会包含在测试范围之外。

我该如何解决这个问题?排除项只能针对特定范围吗?或者,我可以指定用于传递依赖的版本吗?


示例:
这是我的 pom 的样子:

工件 A (astyanax) 排除了对工件 C 的依赖(称为 cassandra-all)

    <dependency>
        <groupId>com.netflix.astyanax</groupId>
        <artifactId>astyanax</artifactId>
        <version>1.0.4</version>
        <exclusions>
            <exclusion>
                <groupId>org.apache.cassandra</groupId>
                <artifactId>cassandra-all</artifactId>
            </exclusion>
        </exclusions>  
    </dependency>
    <dependency>
        <groupId>org.cassandraunit</groupId>
        <artifactId>cassandra-unit</artifactId>
        <version>1.1.1.1</version>
        <scope>test</scope>
    </dependency>

所以具体来说:当我在测试范围之外运行代码并且仍然只保留 cassandraunit 测试的范围时,如何包含 cassandra-all?

4

3 回答 3

6

如果我的问题不够清楚,我深表歉意。我解决这个问题的方法一点也不难:

  • 我在我的 pom 中为 C 添加了一个单独的依赖项
  • 我在 A 中保留了 C 的排除

具体来说,我刚刚补充道:

    <dependency>
        <groupId>org.apache.cassandra</groupId>
        <artifactId>cassandra-all</artifactId>
        <version>1.1.5</version>
    </dependency>

以及在运行时缺少的以下依赖项。

    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>
于 2013-01-29T22:11:18.007 回答
1

我不确定我是否理解所有内容,但无论如何,您应该能够通过配置文件来实现这一点。

在您的 pom 中,创建一个配置文件 A,在其中添加您的依赖项 A 并排除 B 和一个配置文件 B,您将在其中添加一个排除 A 的依赖项。

在运行时,根据您选择的配置文件,您将包含一个或另一个。

HIH

于 2012-11-27T09:29:48.227 回答
1

所以具体来说:当我在测试范围之外运行代码并且仍然只保留 cassandraunit 测试的范围时,如何包含 cassandra-all?

使用 Maven POM 配置 surefire-maven-plugin更改您的 classpath

如果你想要的只是cassandra-all在运行测试时从类路径中删除依赖项,那么下面的 POM 片段会变得很棘手:

<build>
  <!-- ... -->

  <plugins>
    <!-- ... -->

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <classpathDependencyExcludes>
          <classpathDependencyExcludes>
            org.apache.cassandra:cassandra-all
          </classpathDependencyExcludes>
        </classpathDependencyExcludes>
      </configuration>
    </plugin>
  </plugins>
</build>
于 2015-12-07T18:59:02.990 回答