【问题标题】:How a maven dependecy can be defined without version tag?如何在没有版本标签的情况下定义 Maven 依赖项?
【发布时间】:2015-04-02 15:50:54
【问题描述】:

我有一个 pom 可以在 pom 中不定义依赖版本的情况下正常工作,而另一个没有依赖版本的 pom 则无法正常工作。

有效的:

<project>
    <parent>
        <artifactId>artifact-parent</artifactId>
        <groupId>group-parent</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>group-a</groupId>
            <artifactId>artifact-a</artifactId>
        </dependency>
        <dependency>
            <groupId>group-a</groupId>
            <artifactId>artifact-b</artifactId>
        </dependency>
    </dependencies>
</project>

这个不起作用:

<project>
    <dependencies>
        <dependency>
            <groupId>group-a</groupId>
            <artifactId>artifact-a</artifactId>
        </dependency>
        <dependency>
            <groupId>group-a</groupId>
            <artifactId>artifact-b</artifactId>
        </dependency>
    </dependencies>
</project>

这两个似乎唯一不同的是:

<parent>
    <artifactId>artifact-parent</artifactId>
    <groupId>group-parent</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>

第二个不起作用它对我来说似乎很好,但我的问题是为什么第一个起作用?

引用自maven pom reference

这个三位一体代表了具体项目在时间上的坐标, 将其划分为该项目的依赖项。

所以我的问题是第一个是如何工作的?

【问题讨论】:

    标签: maven dependencies pom.xml


    【解决方案1】:

    这里要注意的主要是:

    <parent>
        <artifactId>artifact-parent</artifactId>
        <groupId>group-parent</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    

    依赖的版本看起来像是在父 pom.xml 中定义的。这可能是这样的:

    <project>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>group-a</groupId>
                    <artifactId>artifact-a</artifactId>
                    <version>1.0</version>
                </dependency>
                <dependency>
                    <groupId>group-a</groupId>
                    <artifactId>artifact-b</artifactId>
                    <version>1.0</version>
                </dependency>
            </dependencies>
        </dependencyManagement>
    </project>
    

    再次引用doc

    This is because the minimal set of information for matching a dependency reference against a dependencyManagement section is actually {groupId, artifactId, type, classifier}.

    这里我们不需要定义{type, classifier},因为它与默认值相同,如下所示:

    <type>jar</type>
    <classifier><!-- no value --></classifier>
    

    如果此值与默认值不同,则需要在父 pom 和子 pom 中明确定义。

    【讨论】:

    • 嗯...是还是不是?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-10
    • 1970-01-01
    • 2020-03-30
    • 1970-01-01
    • 2015-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多