MAVEN! Spring Boot accepting wrong version of dependencies mentioned in dependency project

I have two Spring Boot projects and want to use one of them as a MAVEN dependency on the others.

  • Project scraper depends on the database design
  • The project database contains the database layer (Objects and DAOs are built and tested here)

In the Database project, I have to override the Hibernate version and as described in https://spring.io/blog/2016/04/13/overriding-dependency-versions-with-spring-boot option 2.

<properties>
...
    <hibernate.version>5.2.10.Final</hibernate.version>
</properties>

      

This works great and I can see the correct version in the MAVEN depdndencies for the Database project: /home/pm/.m2/repository/org/hibernate/hibernate-core/5.2.10.Final/ hibernate-core-5.2.10.Final. jar

Than I come to the Scraper project. It does not include Hibernate in Maven dependencies as I do not import them explicitly. Now I add the pject database dependency

    <dependency>
        <groupId>web.scraper.database</groupId>
        <artifactId>DataBase</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

      

And this also imports hibernate but with the wrong version (the version mentioned in Spring parent pom instead of my database version of Hibernate) /home/pm/.m2/repository/org/hibernate/hibernate-core/5.0.12. Final / hibernate-core-5.0.12.Final.jar

I want to get the hibernate version mentioned in the database dependecy project. Ho do it?

I would not like to override the Hibernate version in the Scraper project.

Scraper / pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>web.scraper.engine</groupId>
    <artifactId>Scraper</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Scraper</name>
    <description>Web scraper application</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>web.scraper.database</groupId>
            <artifactId>DataBase</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.6</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

      

Database / pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>web.scraper.database</groupId>
    <artifactId>DataBase</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>DataBase</name>
    <description>Data base model and DAO</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <!-- Need to set Hibernate version explicitly,
        because java 8 date and time not mapped properly
        with default version -->
        <hibernate.version>5.2.10.Final</hibernate.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <!-- 
            <scope>runtime</scope>
             -->
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- This makes H2 web console available -->
        <!-- 
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <optional>true</optional>
            </dependency>
        -->             

<!-- 
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>
 -->

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.4.1.Final</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

      

+3


source to share


2 answers


To reconcile dependencies between projects, you must enter a parent pom that both projects inherit. The parent project itself can inherit from the Spring Boot parent. All dependency version overrides via properties should occur in your new parent pump, something like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>web.scraper</groupId>
    <artifactId>Parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>Parent</name>
    <description>Parent pom to coordinate dependencies</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <!-- Need to set Hibernate version explicitly,
        because java 8 date and time not mapped properly
        with default version -->
        <hibernate.version>5.2.10.Final</hibernate.version>
    </properties>

    <!-- Optionally, you can make this parent define a multi-module project, 
         so that the artifacts can be built together, but you don't have to. -->
    <modules>
        <module>Scraper</module>
        <module>Database</module>
    </modules>
</project>

      

And then, in your scraper and database apprentices, declare the parent like this:

<parent>
    <groupId>web.scraper</groupId>
    <artifactId>Parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath/> <!-- lookup parent, or make a multi-module project -->
</parent>

      



Also, any new dependencies you need to add that are not managed with spring can be added to the <dependencyManagement>

parent pump section so that your other posts never have to declare versions.

It mvn dependency:tree | grep hibernate-core

shows the correct versions in both projects:

[INFO] |  +- org.hibernate:hibernate-core:jar:5.2.10.Final:compile

      

+1


source


You can enforce the correct version. In the scrapper project, exclude the Hibernate dependency from the database dependency tag.

<exclusions>
  <exclusion>  
    <groupId>sample.ProjectB</groupId>
    <artifactId>Project-B</artifactId>
  </exclusion>
</exclusions> 

      

Maven Optional Dependencies and Dependency Exceptions

Obviously replace sample.ProjectB file with Hibernate info. Then you just include a separate dependency for the Hibernate version you want.

I think there is an easier way to do this depending on the database in the scraper project, but I cannot find it in the ATM and I am running out of time.

Also note / help:



EDIT : Based on our comments, try to exclude Hibernate from the first Spring dependency so that Spring cannot commit it to the scraper project.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
  <exclusions>
    <exclusion>  
      <groupId>HIBERNATE-STUFF</groupId>
      <artifactId>HIBERNATE-STUFF</artifactId>
  </exclusion>
</exclusions> 
</dependency>

      

This will be the Spring dependency in the database file. Have a separate dependency for the Hibernate version you want.

As a side note, give them a spin. They can help determine what is coming from here:

mvn dependency:tree -Dverbose 
mvn dependency:tree -Dverbose | grep 'omitted for conflict'

      

+1


source







All Articles