如何覆盖依赖项本身内的属性?

时间:2018-05-07 22:44:24

标签: java maven

我有当前的场景,pom.xml有很多依赖项,而且假设我的依赖项 dependency.to.override.property 取决于另一个工具的版本我可能会避免。

但工件 dependency.to.override.property 使用名为 tools-to-avoid.version 的属性,我需要在插入时覆盖此属性依赖。我一直试图在属性标记处覆盖它,但它不起作用。

为了澄清这个场景,我将在这里公开一些代码:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.my.groupId</groupId>
    <artifactId>my-parent-pom</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <tools-to-avoid.version>o.p.q.RELEASE</tools-to-avoid.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.dependency.with.dependencies.list</groupId>
                <artifactId>dependency.to.override.property</artifactId>
                <version>x.y.z.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

我猜你想这样做是为了排除他们的依赖版本并使用你自己的版本吗?

在这种情况下,您应该排除他们的依赖性。如果这样做,将使用您自己定义的任何版本的排除依赖项,而不是两者。

<dependencies>
<dependency>
  <groupId>com.dependency.with.dependencies.list</groupId>
  <artifactId>dependency.to.override.property</artifactId>
  <version>x.y.z.RELEASE</version>
  <scope>compile</scope>
  <exclusions>
    <exclusion>  <!-- declare the exclusion here -->
      <groupId>tools.to.avoid</groupId>
      <artifactId>artifact.to.exclude</artifactId>
    </exclusion>
  </exclusions> 
</dependency>

查看此链接了解详情:https://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html