Daniel
Daniel

Reputation: 6039

Maven: Add older version of the same artifact as dependency

Is it possible to add older version of the same artifact as dependency? Here is an example:

<groupId>edu.illinois.cs.cogcomp</groupId>
<artifactId>illinois-pos</artifactId>
<version>2.0.3</version>

<dependencies>
    <dependency>
        <groupId>edu.illinois.cs.cogcomp</groupId>
        <artifactId>illinois-pos</artifactId>
        <version>2.0.2</version>
        <classifier>model</classifier>
    </dependency>
</dependencies>

If not, is there any way to make it work?

Upvotes: 0

Views: 1252

Answers (2)

A_Di-Matteo
A_Di-Matteo

Reputation: 27862

Based on your comments (and further clarification and needs), I would suggest to have a Maven multi-module project structured as following:

  • A module providing the code using the latest dependency, which would not provide any test
  • A module providing only test cases and using the old dependency and as such running the tests on the concerned code by using another dependency. To make it work, the test module must have a dependency on the code module and explicitly re-declare the dependency version you effectively want for the tests.

As such, you would have a an aggregator pom (empty project, no sources, no tests) having packaging pom and defining two modules (i.e., code-module, test-module). You would keep on working on the code-module and then provide your test cases on the test-module, where you can also override and re-define any required dependency.

As such, as part of the same build (on the aggregator project) you could build the two modules as you need.

Alternatively, keeping a single layer module, you could define a profile which would re-define the required dependency (with a different version) and run your test cases. That would also mean that:

  • As part of the default build, you will need to skip tests, configuring the Maven Surefire Plugin to skip tests via a property (i.e. <skip>${skip.tests}</skip>) which would have as default value true
  • You would then need to configure your profile to change the value of the skip.tests property to false and re-define the dependency

With the second approach, you would then need to execute your build twice: first build would run normally but skipping tests (mvn clean install), second build you would activate the profile and starting from the test phase (mvn test -Pprofile-name).

The first approach (multi-module project) is definitely more recommended.

Upvotes: 1

GreyBeardedGeek
GreyBeardedGeek

Reputation: 30088

No, it's not, because maven will just select the later version. That's because at runtime, java can't use both versions - if you had both in the classpath, the first one loaded would "win" and the other wouldn't be used.

Upvotes: 0

Related Questions