【问题标题】:Removing maven dependancies of child pom from parent pom从父 pom 中删除子 pom 的 maven 依赖项
【发布时间】:2014-04-21 22:16:37
【问题描述】:

我有一个父 pom A 和子 pom B、C 和 D。在 B、C 和 D 中,我有相同类型的依赖项。

即 父绒球

    <artifactID>blah</artifactID>
    <modules>
        <module>B</module>
        <module>C</module>
        <module>D</module>
    </modules>

子 Pom

    <dependencies>
          <dependency>
              .
              .
              <scope>test</scope>
              <type>test-jar</type>
          </dependency>
    </dependencies>

到目前为止,我已尝试编辑 plugin-surefire,但在尝试排除子 pom 的测试范围时不起作用。

我的问题是,是否有办法将配置文件添加到父 pom 以排除所有子 pom 的类型为 test-jar 的所有依赖项。 谢谢

【问题讨论】:

  • 您是否正在寻找一种方法来做到这一点而不指定每一个?
  • 是否可以选择将配置文件放入使用测试罐的子 pom 中,而父 pom 不使用此配置文件?

标签: maven dependencies pom.xml


【解决方案1】:

由于您尝试更新test 范围内的路径,您可以挂钩maven-surfire-plugin,它实际上负责构建测试类路径并排除不需要的依赖项或添加新的依赖项。因此,如果您希望每个模块都应用此限制,您可以将以下条目添加到子 pom,例如B

<project>
  ...
  <artifactId>B</artifactId>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.17</version>
        <configuration>
          <classpathDependencyExcludes>
            <classpathDependencyExclude>${undesired-dependency-groupId}:${undesired-dependency-artifactId}</classpathDependencyExclude>
          </classpathDependencyExcludes>
        </configuration>
      </plugin>
    </plugins>
  </build>
  ...
</project>

或者,如果您希望此限制被所有子模块继承,请在父 pom 中定义为基于配置文件的插件:

<project>
  ...
  <artifactId>A</artifactId>
  <module>B</module>
  <module>C</module>
  <module>D</module>
  ...
  <profiles>
    <profile>
      <id>custom-test-goal</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.17</version>
            <configuration>
              <classpathDependencyExcludes>
                <classpathDependencyExclude>${undesired-dependency-groupId}:${undesired-dependency-artifactId}</classpathDependencyExclude>
              </classpathDependencyExcludes>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
  ...
</project>

【讨论】:

  • 嗨所以我必须为每个模块做这个?有没有办法仅在父级中执行此操作或将其封装在配置文件中?
  • 您可以将此声明移动到父 pom 中,然后将 &lt;plugin&gt; 声明包装在 &lt;pluginManagement&gt; 标记中。然后简单地在子 poms 中调用 &lt;plugin&gt;: org.apache.maven.pluginsmaven-surefire-plugin 或者使用基于配置文件的激活插件.我会更新答案。
  • 好的,所以我已经尝试过了,但 maven 仍在尝试下载依赖项,这导致了错误。我相信我的问题与我最初假设的不同。谢谢
  • 如果它有助于解决一半的问题,至少你可以投票赞成 :)
猜你喜欢
  • 2017-12-31
  • 2018-04-12
  • 1970-01-01
  • 2022-01-27
  • 1970-01-01
  • 2022-12-06
  • 2016-12-17
  • 2013-05-29
  • 2020-07-12
相关资源
最近更新 更多