0

I have a project which uses the latest version of Hibernate (let's say v2.0). I'm using it all around the project. But my project also uses some dependency (let's say MySQL Connector), which uses Hibernate (let's say v1.0). So in my pom.xml I would have something like:

<dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>Hibernate</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>org.mysql</groupId>
            <artifactId>MySQLConnector</artifactId>
            <version>3.7</version>
        </dependency>
</dependencies>

In the end, when I compile my project, the version of Hibernate downloaded and used is v1.0 because MySQLConnector needs this one. Is there a way to specify some version of a dependency that will be used only by one of my dependencies and the rest of the code to use another version? So something like:

<dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>Hibernate</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>org.mysql</groupId>
            <artifactId>MySQLConnector</artifactId>
            <version>3.7</version>
            <somemagicaltag>
               <groupId>org.hibernate</groupId>
               <artifactId>Hibernate</artifactId>
               <version>1.0</version>
            </somemagicaltag>
        </dependency>
</dependencies>

Thus allowing MySQLConnector to use the older version of Hibernate if it likes it, but the rest of my code to use the newer, more updated version of Hibernate?

pinpinokio
  • 505
  • 5
  • 19

3 Answers3

1
  1. In all "normal" cases, the dependency that you declare wins against the ones that come transitively. So I would assume that in your setup, you get version 2 of hibernate (and nothing else). You can find out by calling mvn dependency:list.

  2. You cannot load the same class twice in different versions, so normally, you cannot have two versions of hibernate in the same project. There are approaches around this (using the Maven shade plugin), but this should be the exception. Try to make your own code and your dependencies work with the same version of hibernate.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
1

Is there a way to specify some version of a dependency that will be used only by one of my dependencies and the rest of the code to use another version?

No. There can be only one. So in your case either 1.0 or 2.0 (usually using newer version makes more sense). Which version is used depends on the order of dependencies in pom.xml which use such transitive dependency: Why order of Maven dependencies matter?

You can also define which version will be used by specifying such dependency (this overrides transitive dependency version) or by defining such dependency either in dependencyManagement tag: Differences between dependencyManagement and dependencies in Maven or by using BOM mechanism: Maven BOM [Bill Of Materials] Dependency

jirka.pinkas
  • 918
  • 6
  • 21
-1

You can skip downloading that default artifact which is getting downloaded by Maven.

<dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>Hibernate</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>org.mysql</groupId>
            <artifactId>MySQLConnector</artifactId>
            <version>3.7</version>
            <exclusions>
                <exclusion>  <!-- declare the exclusion here -->
                    <groupId>org.hibernate</groupId>
                    <artifactId>Hibernate</artifact>
                </exclusion>
           </exclusions> 
        </dependency>
</dependencies>
Rahul Agrawal
  • 623
  • 1
  • 5
  • 18