我正在尝试构建一个多模块maven项目。我的项目结构是
Multi-Module-Project
pom.xml
\Module-1
src\main\java
com.xyz.module1
MyTestClass.java
pom.xml
\Module-2
src\main\java
com.xyz.module2
App.java
pom.xml
现在,当我尝试在Module-2的App.java中使用MyTestClass时,我在运行命令“mvn clean install”时收到此错误:
ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project Module-2: Compilation failure: Compilation failure:
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] Multi-Module-Project ............................... SUCCESS [ 1.361 s]
[INFO] Module-1 ........................................... SUCCESS [ 2.511 s]
[INFO] Module-2 ........................................... FAILURE [ 0.135 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.721 s
[INFO] Finished at: 2018-01-31T12:11:29+05:30
[INFO] Final Memory: 36M/395M
[INFO] ------------------------------------------------------------------------
[ERROR] /home/xyz/workspace2/Multi-Module-Project/Module-2/src/main/java/com/xyz/module2/App.java:[3,26] package com.xyz.module1 does not exist
[ERROR] /home/xyz/workspace2/Multi-Module-Project/Module-2/src/main/java/com/xyz/module2/App.java:[13,17] cannot find symbol
[ERROR] symbol: class MyTestClass
[ERROR] location: class com.xyz.module2.App
这是我的主要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.xyz</groupId>
<artifactId>Multi-Module-Project</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>
<name>Multi-Module-Project</name>
<description>Multi Module Project</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</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>
<modules>
<module>Module-1</module>
<module>Module-2</module>
</modules>
</project>`
Module-1 Pom.xml
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.xyz</groupId>
<artifactId>Multi-Module-Project</artifactId>
<version>0.0.1</version>
</parent>
<packaging>war</packaging>
<groupId>com.xyz</groupId>
<artifactId>Module-1</artifactId>
<version>0.0.1</version>
<name>Module-1</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Module-2 Pom.xml
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.xyz</groupId>
<artifactId>Multi-Module-Project</artifactId>
<version>0.0.1</version>
</parent>
<packaging>war</packaging>
<groupId>com.xyz</groupId>
<artifactId>Module-2</artifactId>
<version>0.0.1</version>
<name>Module-2</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.xyz</groupId>
<artifactId>Module-1</artifactId>
<version>0.0.1</version>
</dependency>
</dependencies>
</project>
这是模块1中的MyTestClass.java:
package com.xyz.module1;
public class MyTestClass {
private String name;
public MyTestClass() {
super();
}
public MyTestClass(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
这是我在模块2中的App.java:
package com.xyz.module2;
import com.xyz.module1.MyTestClass;
public class App {
public static void main(String[] args) {
MyTestClass mtc = new MyTestClass();
mtc.setName("Bob");
System.out.println(mtc.getName());
}
}
答案 0 :(得分:2)
对于其他可能被困在这里的人来说,这些是解决我问题的链接,
在我的问题得到解决之前,我确实必须将Module-1打包到jar。
对我有用的实际答案: https://github.com/spring-projects/spring-boot/issues/6792
链接让我得到实际答案: spring-boot-maven-plugin breaks sibling module dependency
答案 1 :(得分:1)
如果您的模块1被假设用作共享库,则它将不具有WAR包装。只需从Module 1配置中删除WAR包。
WAR工件不能使用其他WAR工件。