Victor Ronin
Victor Ronin

Reputation: 23298

Maven and dependencies on two different versions of a library

I am using maven 3.1.1 and I have following problem

I have a main project (pom.xml) which will be compiled to WAR file. And it has two modules.

<module>module1</module>
<module>module2</module>

...

<dependency>
   <groupId>com.foo</groupId>
   <artifactId>module1</artifactId>
   <version><1.0/version>
</dependency>
<dependency>
   <groupId>com.foo</groupId>
   <artifactId>module2</artifactId>
   <version>1.0</version>
</dependency>

Module 1 has following (please notice version of artifact)

<dependency>
   <groupId>com.bar</groupId>
   <artifactId>library</artifactId>
   <version>1</version>
</dependency>

Module 2 has following (please notice version of artifact)

<dependency>
   <groupId>com.bar</groupId>
   <artifactId>library</artifactId>
   <version>2</version>
</dependency>

So, as a result my project depends on two versions of the same library. And the problem is that the library version 2 isn't backward compatible with version 1. So, I can't just include version 2.

I was considering to modify the module 1 to use library version 2, but it make take substantial amount of time. So, I am looking for a way to include both versions of the library.

How can I make sure that:

Update 1 (thoughts on optional and exclusion)

I read about optional and exclusion of dependencies here. And I am not sure that it will work.

Based on the documentation

Exclusions are set on a specific dependency in your POM, and are targeted at a specific groupId and artifactId. When you build your project, that artifact will not be added to your project's classpath by way of the dependency in which the exclusion was declared.

I believe if I will try to exclude library v2 in my main project, it just won't be included. Main project will build the module2. However, when it will be packaging everything (including a module2) to a WAR file, it will just exclude library V2.

Based on the documentation If a user wants to use functionality related to an optional dependency, they will have to redeclare that optional dependency in their own project.

So, if I will declare in my module2 dependency on library v2 optional then if I won't redeclare it in the main project, it won't be included to final WAR file.

Upvotes: 0

Views: 4613

Answers (1)

user1717259
user1717259

Reputation: 2883

Your Module should declare a dependency on yourJar2, as normal.

Where you specify that your Project has a dependency on your Module, use the

<exclusions>

tags to exclude jar version 2 as being 'exported' as a transitive dependency.

        <dependency>
            <groupId>yourGroup</groupId>
            <artifactId>yourJar</artifactId>
            <version>1</version>
        </dependency>
        So your project has a dependency on Jar1
         <dependency>
            <groupId>yourGroup</groupId>
            <artifactId>yourModule</artifactId>
            <version>1</version>
            <optional>true</optional>
            <exclusions>
                <exclusion>
                    <groupId>yourGroup</groupId>
                    <artifactId>yourJar</artifactId>
                    <version>2</version>
                </exclusion>
            </exclusions>
        </dependency>
        And now your project has a dependency on yourModule, which will not give you a transitive dependency on yourJar2

Upvotes: 0

Related Questions