我有两个Spring Boot项目,并希望在其他项目中使用其中一个作为MAVEN依赖项。
在数据库项目中,我必须覆盖Hibernate版本,并且像https://spring.io/blog/2016/04/13/overriding-dependency-versions-with-spring-boot选项2中描述的那样。
<properties>
...
<hibernate.version>5.2.10.Final</hibernate.version>
</properties>
这很好用,我在数据库项目的MAVEN depdndencies中看到了正确的版本: /home/pm/.m2/repository/org/hibernate/hibernate-core/5.2.10.Final/hibernate-core-5.2.10.Final.jar
我来到Scraper项目。它不包含Maven依赖项中的任何Hibernate,因为我没有显式地导入它们。现在我添加了数据库项目依赖项
<dependency>
<groupId>web.scraper.database</groupId>
<artifactId>DataBase</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
这也导入了hibernate,但版本错误(在Spring父pom中提到的版本,而不是我的数据库项目Hibernate版本) /home/pm/.m2/repository/org/hibernate/hibernate-core/5.0.12.Final/hibernate-core-5.0.12.Final.jar
我想获得数据库项目依赖中提到的hibernate版本。何你这样做?
我不想在Scraper项目中覆盖Hibernate版本。
刮刀/ 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>
数据库/ 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>
答案 0 :(得分:1)
您可以强制使用正确的版本。在您的scrapper项目中,从数据库依赖项标记中,排除Hibernate依赖项。
<exclusions>
<exclusion>
<groupId>sample.ProjectB</groupId>
<artifactId>Project-B</artifactId>
</exclusion>
</exclusions>
Maven Optional Dependencies and Dependency Exclusions
显然,用Hibernate信息替换sample.ProjectB。 然后,您只需要为您想要的Hibernate版本添加一个单独的依赖项。
我认为在剪贴板项目中有一种更简单的方法可以在数据库依赖项中完成所有操作,但是我无法找到ATM,而且我没有太多时间。
同样值得注意/帮助:
编辑:根据我们的评论,尝试从第一个Spring依赖项中排除Hibernate,以便Spring无法将其传递给scrapper项目。
<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>
这将是数据库文件中的Spring依赖项。对你想要的Hibernate版本有一个单独的依赖。
作为旁注,请给它们一个旋转。他们可以帮助发现来自哪里:
mvn dependency:tree -Dverbose
mvn dependency:tree -Dverbose | grep 'omitted for conflict'
答案 1 :(得分:1)
要协调项目之间的依赖关系,您应该引入两个项目都继承自的父pom。父项目本身可以从Spring Boot父pom继承。所有依赖版本覆盖通过属性都应该出现在新的父pom中,如下所示:
<?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>
然后,在你的Scraper和数据库poms中,声明父母是这样的:
<parent>
<groupId>web.scraper</groupId>
<artifactId>Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent, or make a multi-module project -->
</parent>
此外,您需要添加的任何不受spring管理的新依赖项都可以添加到父pom的<dependencyManagement>
部分,这样您的其他poms就不必声明版本。
有了这个,mvn dependency:tree | grep hibernate-core
在两个项目中都显示了正确的版本:
[INFO] | +- org.hibernate:hibernate-core:jar:5.2.10.Final:compile