Maven中相同依赖项的多个版本 [英] Multiple versions of the same dependency in Maven

查看:237
本文介绍了Maven中相同依赖项的多个版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Maven存储库中声明相同依赖项的多个版本?

Is it possible to declare multiple versions of the same dependency in a Maven repo?

我一次需要这些依赖项:

I need these dependencies all at once:

    <dependency>
        <groupId>org.bukkit</groupId>
        <artifactId>craftbukkit</artifactId>
        <version>1.7.9-R0.2</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.bukkit</groupId>
        <artifactId>craftbukkit</artifactId>
        <version>1.7.2-R0.3</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.bukkit</groupId>
        <artifactId>craftbukkit</artifactId>
        <version>1.6.4-R2.0</version>
        <scope>compile</scope>
    </dependency>

因为它们每个都包含我关心的不同软件包:

Because each of them contains a different package I care about:

org.bukkit.craftbukkit.v1_6_R3

org.bukkit.craftbukkit.v1_6_R3

org.bukkit.craftbukkit.v1_7_R1

org.bukkit.craftbukkit.v1_7_R1

org.bukkit.craftbukkit.v1_7_R3

org.bukkit.craftbukkit.v1_7_R3

如果我如第一个代码片段所示声明依赖项,则只有最后一个代码才会生效.在Maven中有什么方法可以实现这一目标?

If I declare dependencies as shown in the first snippet, only the last one will take effect. Is there any way to achieve this in Maven?

@Edit可以解决任何问题吗?

@Edit Any workaround, maybe?

推荐答案

否. Maven将仅解决模块中的一个依赖项,并将省略其他版本以避免任何冲突.即使在整个依赖关系层次结构中使用了相同依赖关系的多个版本,Maven也会使用依赖关系树中的最近关系" 策略选择一个版本.

No. Maven will only resolve one dependency in your module and will omit the other versions to avoid any conflict. Even if multiple versions of the same dependency are used in the whole dependency hierarchy, Maven will pick one version using the "nearest in the dependency tree" strategy.

可以使用不同的配置文件.对于Bukkit的每个版本,都可以定义和激活配置文件.即使您激活多个配置文件,也只会使用一个版本.

It is possible to specify different dependency versions using different profiles. For each version of Bukkit a profile can be defined and activated. Still if you activate more than one profile, only one version would be used.

<profiles>
    <profile>
        <id>Bukkit_1_7_9_R02</id>
        <activation>
            ...
        </activation>
        <dependencies>
            <dependency>
                <groupId>org.bukkit</groupId>
                <artifactId>craftbukkit</artifactId>
                <version>1.7.9-R0.2</version>
                <scope>compile</scope>
            </dependency>
        </dependencies>
    </profile>
    <profile>
        <id>Bukkit_1_7_2_R03</id>
        <activation>
            ...
        </activation>
        <dependencies>
            <dependency>
                <groupId>org.bukkit</groupId>
                <artifactId>craftbukkit</artifactId>
                <version>1.7.2-R0.3</version>
                <scope>compile</scope>
            </dependency>
        </dependencies>
    </profile>
    ...
</profiles>

这篇关于Maven中相同依赖项的多个版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆