gpt4 book ai didi

java - 多模块 Maven 项目!无法将模块作为依赖项包含在同一项目的另一个模块中

转载 作者:行者123 更新时间:2023-12-01 17:52:26 29 4
gpt4 key购买 nike

我正在尝试构建一个多模块 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>`

模块 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>

模块 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>

这是 module-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;
}



}

这是我在 module-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());
}
}

最佳答案

对于其他可能被困在这里的人,这些是解决我的问题的链接,

在我的问题得到解决之前,我确实必须将 Module-1 打包为 jar。

对我有用的实际答案: https://github.com/spring-projects/spring-boot/issues/6792

引导我找到实际答案的链接: spring-boot-maven-plugin breaks sibling module dependency

Maven compilation error: package does not exist

关于java - 多模块 Maven 项目!无法将模块作为依赖项包含在同一项目的另一个模块中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48536373/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com