Christian Neverdal
Christian Neverdal

Reputation: 5375

How do I set a Maven dependency version programmatically?

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.

Upvotes: 10

Views: 10057

Answers (1)

khmarbaise
khmarbaise

Reputation: 97409

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.

Upvotes: 20

Related Questions