[英]How to move all the common dependencies from maven module into parent module in spring boot
I have two simple Spring Boot
applications, one is a eureka-server
and another is an eureka-client
. 我有两个简单的Spring Boot
应用程序,一个是eureka-server
,另一个是eureka-client
。 Both of them reside in a maven aggregator module eureka-sample
. 它们都驻留在Maven聚合器模块eureka-sample
。
I have some common dependencies in both the eureka-server
and eureka-client
applications that I would like to move up to the parent pom so that I do not have to include them in each of the child modules every now and then. 我希望在eureka-server
和eureka-client
应用程序中都有一些共同的依赖关系,因此我希望移至父pom,这样就不必时不时地将它们包含在每个子模块中。
I tried moving the dependency spring-boot-starter-test
from the child modules into the parent pom under <dependencyManagement>
section but then the tests in the child module were not able to compile as those classes were not able to find the jar
. 我尝试将依赖项spring-boot-starter-test
从子模块移到<dependencyManagement>
部分下的父pom中,但是由于这些类无法找到jar
,因此子模块中的测试无法编译。 May be I misconfigured or placed it under wrong section. 可能是我配置错误或放置在错误的部分。
How can I achieve this? 我该如何实现?
eureka-sample (parent pom.xml) 尤里卡样本(父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>com.study.spring</groupId>
<artifactId>eureka-sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>eureka-sample</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR4</spring-cloud.version>
</properties>
<modules>
<module>eureka-server</module>
<module>eureka-client</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath />
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
eureka-server/pom.xml eureka-server / 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>
<artifactId>eureka-server</artifactId>
<packaging>jar</packaging>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.study.spring</groupId>
<artifactId>eureka-sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
**<!-- ALL THE FOLLOWING 3 DEPENDENCIES SHOULD GO TO PARENT POM SO THAT I DO NOT HAVE TO PUT THEM IN EVERY CHILD MODULE -->**
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
eureka-client/pom.xml eureka-client / 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>
<artifactId>eureka-client</artifactId>
<packaging>jar</packaging>
<name>eureka-client</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.study.spring</groupId>
<artifactId>eureka-sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
**<!-- ALL THE FOLLOWING 3 DEPENDENCIES SHOULD GO TO PARENT POM SO THAT I DO NOT HAVE TO PUT THEM IN EVERY CHILD MODULE -->**
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Try putting the common dependencies inside the <dependencies>
section of the parent .pom instead of the <dependencyManagement>
section. 尝试将公共依赖项放在父.pom的<dependencies>
部分中,而不是在<dependencyManagement>
部分中。
With <dependencyManagement>
you only prepare and configure the dependencies for all children modules, but in order to actually use them, you'll still need to reference them from within the child module. 使用<dependencyManagement>
您只需要准备和配置所有子模块的依赖关系,但是为了实际使用它们,您仍然需要从子模块中引用它们。
The difference is subtle, but important - see https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html for details. 差异是细微的,但很重要-有关详细信息,请参见https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.