Dmitry
Dmitry

Reputation: 185

Maven: dynamic specification of dependencies

I have started to learn Maven and have the following question:

I would like to dynamically specify a dependency for building maven project instead of using the dependency specified in POMs - is there a way to do that?

So although I have the following dependencies specified in POM

...
<dependencies>
<dependency>
<groupId>group</groupId>
<artifactId>ProjectComponent</artifactId>
<version>1.0</version>
 </dependency>
...

I would like to specify in the build command that I want to use a different version. Is there a way to specify this?

The idea is that I want to have an integration build made in Jenkins with a dependency on the latest available snapshot of the system for a particular branch. That snapshot is not released to the maven repository yet, so I would like to fetch it in Jenkins and specify a dependency for mvn build.

Thanks!

POSSIBLE SOLUTION: What I ended up with is to use ${my.lib.version} construction and specify it with -Dmy.lib.version=1.0-SNAPSHOT" when calling to mvn. Thus I can use it for Jenkins integration builds by fetching arbitrary snapshot versions of dependencies from svn and feeding their snapshot versions to the integration build pom.

Upvotes: 2

Views: 9425

Answers (4)

Anton Kokarski
Anton Kokarski

Reputation: 140

Just came across this as I was looking for something similar. In my case same application code is being reused on different stacks which means using different "drivers" for accessing data. Although drivers implement same interface they do come from different artifacts.

Upvotes: 1

Dmitry
Dmitry

Reputation: 185

What I ended up with is to use ${my.lib.version} construction and specify it with -Dmy.lib.version=1.0-SNAPSHOT" when calling to mvn. Thus I can use it for Jenkins integration builds by fetching arbitrary snapshot versions of dependencies from svn and feeding their snapshot versions to the integration build pom.

Upvotes: 1

sten_aksel
sten_aksel

Reputation: 41

Maven may use "dynamically" specified property (ex: group.ProjectComponent.version) with the help of profiles.

<dependencies>
    <dependency>
    <groupId>group</groupId>
    <artifactId>ProjectComponent</artifactId>
    <version>${group.ProjectComponent.version}</version>
</dependency>

So if you create some profiles you may switch between them (see maven references)

Example:

<profile>
    <id>stable-builds</id>
    <properties>
        <group.ProjectComponent.version>1.0</group.ProjectComponent.version>
    </properties>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
</profile>

<profile>
    <id>beta-builds</id>
    <properties>
        <group.ProjectComponent.version>2.0.Beta1</group.ProjectComponent.version>
    </properties>
    <activation>
        <activeByDefault>false</activeByDefault>
    </activation>
</profile>

Upvotes: 4

khmarbaise
khmarbaise

Reputation: 97348

No you can't change the dependencies dynamically. Furthermore it does not make sense, cause you would like to have a reproducible build.

Upvotes: -2

Related Questions