我正在尝试创建一个多模块 Maven Spring Boot 项目。我的项目层次结构如下:
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. However, the build for Child-Project fails!
Following is the code:
Parent 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:
<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 实用程序类:
package commons.utils;
public class CommonUtility {
//Common Utility methods go here
}
子 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>
子主类:
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());
}
}
当我在 STS 中运行 Child 的主类时,一切正常。但是,当我尝试运行 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
我不确定,我做错了什么?
我已将此项目上传到 GitHub ,以防万一有人想看一下代码。如果有人能够找到解决方案。请告诉我。
请您参考如下方法:
请尝试进行以下更改。
在 Commons pom 中添加以下内容。
<groupId>commons</groupId>
最后,常见的 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>
现在在子 pom.xml 中添加以下内容。
<groupId>commons</groupId>
最后子 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>