如何以编程方式设置 Maven 依赖版本? [英] How do I set a Maven dependency version programmatically?

查看:63
本文介绍了如何以编程方式设置 Maven 依赖版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们以这个例子为例:

<依赖><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.21</version></依赖></依赖项>

我知道想要将 设置为我选择的东西.

有点像mvn dependency:set-version :<版本号>

特别是对于 slf4j,我可能想要:mvn dependency:set-version org.slf4j:slf4j-api 1.7.24

我知道我可以使用各种自动更新方法/插件来提升"版本,但我不想要那样.我想设置一个特定的版本.该命令应该足够灵活,可以接受任何内容作为版本,或者至少它应该接受 Maven 当前无法检索的版本.

请注意,我还想避免将依赖版本提取到我通过其他方式设置的属性中.

我不是在寻找任何涉及使用父 POM 或类似问题的解决方案.

简单地说,解决方案应该是一个 mvn 特定的命令,它可以代替 sed 的调用来简单地强制依赖版本.em>

解决方案

您可以简单地使用 以下命令:

mvn 版本:use-dep-version -Dincludes=junit:junit -DdepVersion=1.0 -DforceVersion=true

In 包含是这样给出的:groupId:artifactId 但可以是这样的(引用自文档):

<块引用>

要包含的工件模式列表.遵循模式"groupId:artifactId:type:classifier:version".

与此相关的问题是,通过 forceVersion 将关闭任何检查以验证您提供给工件可用版本的版本,因此请谨慎使用.>

Let's take this example:

<dependencies>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.21</version>
    </dependency>
</dependencies>

I know want to set <version> to be something of my choosing.

Something like mvn dependency:set-version <groupId>:<artifactId> <version-number>

Specifically for slf4j, I might want: mvn dependency:set-version org.slf4j:slf4j-api 1.7.24

I know I can use various auto-update methods/plugins to "bump" the version, but I don't want that. I want to set a specific version. The command should be flexible enough to accept anything as a version, or at least it should accept versions that can't currently be retrieved by Maven.

Note that I also want to avoid extracting the dependency version into a property that I set by other means.

I'm not looking for any solutions that involve the usage of the parent POM, or anything like that.

Simply stated, the solution should be a mvn specific command that would replace, say, an invoication of sed to simply force the dependency version.

解决方案

You can simply use the following command:

mvn versions:use-dep-version -Dincludes=junit:junit -DdepVersion=1.0 -DforceVersion=true

In includes is given like this: groupId:artifactId but can be something like this (quote from the docs):

A list of artifact patterns to include. Follows the pattern "groupId:artifactId:type:classifier:version".

The issue related to that is that via forceVersion any check which will be turned off to validate the version you have given to the versions which is available for the artifact so use it with care.

这篇关于如何以编程方式设置 Maven 依赖版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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