如何在maven中指定传递依赖的版本?

时间:2016-09-27 04:17:46

标签: maven

我有一个依赖于B的依赖关系A,而在A的pom文件中,可以指定B'version。所以我想知道如何在构建时指定B的版本?感谢。

3 个答案:

答案 0 :(得分:2)

如其他答案所述"瞬态依赖"涉及3件神器

  • A取决于B
  • B取决于C

所以C是A

的传递依赖

您想要的是每个正在运行的构建的依赖项的控件版本号。为此,您应该将该特定依赖项的版本号绑定到maven属性

<?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>

    <artifactId>A</artifactId>
    <groupId>com.xyz</groupId>
    <version>1.0</version>



 <properties>
    <B_project_version>1.5</B_project_version>
</properties>

<dependencies>
    <dependency>
        <groupId>com.xyz</groupId>
        <artifactId>B</artifactId>
        <version>${B_project_version}</version>
    </dependency>
</dependencies>

现在,您可以将参数传递给构建,以指定&#34; B_project_version&#34;的值。例如

man package -DB_project_version=3.8

如果没有指定任何正确的值,将使用默认值(上例中为1.5)

使用apposite配置文件的定义为最可能的场景定义配置文件非常方便

 <profiles>
    <profile>
        <id>use_version_2</id>
        <properties>
            <B_project_version>2.0</B_project_version>
        </properties>
    </profile>
    <profile>
        <id>use_version_1</id>
        <properties>
            <B_project_version>1.0</B_project_version>
        </properties>
    </profile>
</profiles>

如果你的意图是控制真正的传递依赖,你必须在你的pom中将它排除在B依赖之外,并使其成为直接依赖。上面的例子变为

<properties>
    <C_project_version>1.5</C_project_version>
</properties>

<dependencies>
    <dependency>
        <groupId>com.xyz</groupId>
        <artifactId>B</artifactId>
        <version>1.0</version>
        <exclusions>
            <exclusion>
                <groupId>com.xyz</groupId>
                <artifactId>C</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>com.xyz</groupId>
        <artifactId>C</artifactId>
        <version>${C_project_version}</version>
    </dependency>
</dependencies>


<profiles>
    <profile>
        <id>use_version_2</id>
        <properties>
            <C_project_version>2.0</C_project_version>
        </properties>
    </profile>
    <profile>
        <id>use_version_1</id>
        <properties>
            <C_project_version>1.0</C_project_version>
        </properties>
    </profile>
</profiles>

答案 1 :(得分:1)

要描述传递依赖,我们需要三个工件,因为它是依赖的依赖。

让我们看看这三个SELECT CAST(obsDate AS DATE) as obsDate, (CASE WHEN COUNT(id) > 12 THEN COUNT(id) ELSE 0 END) numOfHoursAtHigh FROM diseaseScores WHERE diseaseID=2 AND siteID=72160 AND numOfRotationYears=3 AND currentScore > 16 AND month(obsDate) IN (6) GROUP BY CAST(obsDate AS DATE) UNION SELECT CAST(obsDate AS DATE) AS obsDate, 0 AS numOfHoursAtHigh FROM diseaseScores WHERE diseaseID=2 AND siteID=72160 AND numOfRotationYears=3 AND currentScore < 17 --<-- note the change AND month(obsDate) IN (6) GROUP BY CAST(obsDate AS DATE) ORDER BY CAST(obsDate AS DATE) 文件:

pom.xml

a/pom.xml

<?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>x.y</groupId> <artifactId>a</artifactId> <version>2.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>x.y</groupId> <artifactId>b</artifactId> <version>2.0-SNAPSHOT</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>x.y</groupId> <artifactId>c</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </dependencyManagement> </project>

b/pom.xml

<?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>x.y</groupId> <artifactId>b</artifactId> <version>2.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>x.y</groupId> <artifactId>c</artifactId> <version>2.0-SNAPSHOT</version> </dependency> </dependencies> </project>

c/pom.xml

在此示例中,工件<?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>x.y</groupId> <artifactId>c</artifactId> <version>2.0-SNAPSHOT</version> </project> 取决于a,工件b取决于b

如果c中没有<dependencyManagement>阻止,则工件a/pom.xmlb将在版本c中使用,但使用此&#34; 覆盖&#34;该工件在版本2.0-SNAPSHOT中使用b,但在版本2.0-SNAPSHOT中使用传递依赖c

答案 2 :(得分:0)

Maven将使用&#34;最近的&#34;版。因此,如果您在自己的pom中定义对B及其版本的依赖关系,那将替换A中的依赖关系。

这可能会破坏A取决于A如何使用B - 因为只有一个版本会在项目中结束,如果B破坏了它的某些API,那么你注定要失败。只有尝试和错误。

有关maven如何解析依赖关系的更多详细信息,请参阅:https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html