[英]Multi module Maven Spring boot Project with sibling project dependencies
I'm trying to create a multi-module maven Spring Boot project. 我正在尝试创建一个多模块Maven Spring Boot项目。 My project hierarchy is as follows:
我的项目层次结构如下:
Parent-Project: (packaging type: pom) | |==> Commons-Project (packaging type: jar) |==> Child-Project (packaging type: jar | Has Commons-Project dependency)
When I try to compile the parent project, it successfully compiles the Parent-Project and Commons-Project. 当我尝试编译父项目时,它会成功编译Parent-Project和Commons-Project。 However, the build for Child-Project fails!
但是,Child-Project的构建失败!
Following is the code: 以下是代码:
Parent 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.1.0</version>
<name>parent</name>
<packaging>pom</packaging>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<commons.version>1.0</commons.version>
</properties>
<dependencies>
<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>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>commons</artifactId>
<version>${commons.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<modules>
<module>commons</module>
<module>child</module>
</modules>
</project>
Commons pom:
公用pom:
<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>
<parent>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.1.0</version>
</parent>
<artifactId>commons</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
</project>
Commons Utility class:
Commons Utility类:
package commons.utils;
public class CommonUtility {
//Common Utility methods go here
}
Child pom:
儿童pom:
<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>
<parent>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.1.0</version>
</parent>
<artifactId>child</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>commons</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
Child Main class:
儿童主班:
package child;
import javax.annotation.PostConstruct;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import commons.utils.CommonUtility;
@SpringBootApplication
public class ChildApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(ChildApplication.class, args);
}
@PostConstruct
public void init() {
// For test
System.out.println(CommonUtility.class.getName());
}
}
When I run the Child's Main class in STS, everything works fine. 当我在STS中运行Child's Main类时,一切正常。 But when I try to run
mvn clean package
or mvn clean install
, the build fails with the message: 但是,当我尝试运行
mvn clean package
或mvn clean install
,构建失败并显示以下消息:
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project child: Compilation failure: Compilation failure:
...
package commons.utils does not exist
I'm not sure, what wrong am I doing? 我不确定,我在做什么错?
I have uploaded this project on GitHub , just in case someone wants to have a look at the code. 我已将此项目上传到GitHub上 ,以防万一有人想看一下代码。 If anyone is able to find a solution.
如果有人能够找到解决方案。 Please let me know.
请告诉我。
Please try with the following changes. 请尝试以下更改。
Add the below in Commons pom. 在Commons pom中添加以下内容。
<groupId>commons</groupId>
Finally the common pom.xml looks like the below. 最后,通用的pom.xml如下所示。
<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>
<parent>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>commons</groupId>
<artifactId>commons</artifactId>
</project>
Now add the below in child pom.xml. 现在,将以下内容添加到子pom.xml中。
<groupId>commons</groupId>
Finally child pom.xml looks like this. 最后,子pom.xml看起来像这样。
<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>
<parent>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>child</artifactId>
<dependencies>
<dependency>
<groupId>commons</groupId>
<artifactId>commons</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
Try updating the poms as below. 尝试如下更新pom。 Complete example is uploaded on GitHub
完整示例已上传到GitHub
parent pom: 父pom:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.1.0</version>
<name>parent</name>
<packaging>pom</packaging>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<commons.version>1.0</commons.version>
</properties>
<dependencyManagement>
<dependencies>
<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>
</dependencyManagement>
<modules>
<module>commons</module>
<module>child</module>
</modules>
commons pom: 公地pom:
<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.example</groupId>
<artifactId>commons</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.1.0</version>
</parent>
child pom: 儿童pom:
<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.example</groupId>
<artifactId>child</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.1.0</version>
</parent>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.