当许多项目需要一个模块时,如何正确构建 multi-module maven 项目?
How to properly structure a multi-module maven project when a module is required by many projects?
所以我一直在努力弄清楚如何制作一个 multi-module maven 项目,该项目允许多个项目依赖于一个核心模块,该模块也将打包到一个 JAR 中。正确的结构是什么?
郑重声明,我最初没有聚合器 pom,它 builds/debugs 很好,但我无法构建 jar。在玩了这个之后,我什至不能将同一个模块导入到 2 个单独的项目中,甚至不能 debug/run.
我认为我的问题源于我在 intellij 中创建错误的依赖关系,但这只是一个猜测。我一直在进行模块设置 -> 依赖项并导入一个 maven 项目,以便我的 poms 识别其他模块。 Intellij 似乎不喜欢这样,因为它不断从其他项目中删除我的模块。
因此应满足以下条件:
- 一个中央核心模块
- 每个 parent
一个项目特定模块
- 多个项目取决于核心模块
- 所有 Maven 项目
- 必须打包成jar
- core + secondary 模块应该在每个 intellij 实例中都是可编辑的
所以我尝试了很多结构,但我最后的尝试如下:
Parent POM/project 指向 2 个现有模块。其中一个模块将用于另一个类似结构的项目。
我已经尝试在不同的配置中设置所有这些 poms 的 Maven 依赖关系,但是在构建时我仍然没有得到清单或 noclassdefound and/or 根本无法构建或获得循环依赖关系。
这里是 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TEST</groupId>
<artifactId>CLIENT-PARENT</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>../TEST-SHARED-CORE</module>
<module>../TEST-CLIENT-CLI</module>
</modules>
</project>
这是child一个(核心)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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TEST</groupId>
<artifactId>TEST-SHARED-CORE</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
...
</dependencies>
</project>
和第二个 child 模块 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 http://maven.apache.org/xsd/maven- 4.0.0.xsd">
<modelVersion>4.0.0
</modelVersion>
<groupId>TEST</groupId>
<artifactId>TEST-CLIENT-CLI</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>TEST</groupId>
<artifactId>CLIENT-PARENT</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../TEST-CLIENT-PARENT</relativePath>
</parent>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>TEST</groupId>
<artifactId>TEST-SHARED-CORE</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
我的印象是我的结构非常不正确,那么正确的结构是什么?
编辑:
当这样做时 package/build 这是当 JAR 为 运行 时发生的情况,它没有在 shared-core 中找到 类。
Exception in thread "main" java.lang.NoClassDefFoundError: com/shared/networking/contexts/NetworkContext
at com.Main.main(Main.java:23)
Caused by: java.lang.ClassNotFoundException
你的基本结构看起来没问题。我会将包装元素添加到 CORE 和 CLI 模块
<packaging>jar</packaging>
所以 maven 知道要构建一个 jar 文件。
编辑 2
我相信你目前的结构是
parent ( a pom with modules )
- core ( a jar )
- cli ( a jar )
我认为这个相对路径不正确
<relativePath>../TEST-CLIENT-PARENT</relativePath>
因为这表明
root ( a pom with only modules )
- parent ( a pom with common jar dependencies)
- core ( a jar )
- cli ( a jar )
虽然您希望将所有内容构建为一个多模块项目,但我认为在第一次构建时您将单独构建 parent/root 子模块。
的根 pom
所以我一直在努力弄清楚如何制作一个 multi-module maven 项目,该项目允许多个项目依赖于一个核心模块,该模块也将打包到一个 JAR 中。正确的结构是什么?
郑重声明,我最初没有聚合器 pom,它 builds/debugs 很好,但我无法构建 jar。在玩了这个之后,我什至不能将同一个模块导入到 2 个单独的项目中,甚至不能 debug/run.
我认为我的问题源于我在 intellij 中创建错误的依赖关系,但这只是一个猜测。我一直在进行模块设置 -> 依赖项并导入一个 maven 项目,以便我的 poms 识别其他模块。 Intellij 似乎不喜欢这样,因为它不断从其他项目中删除我的模块。
因此应满足以下条件:
- 一个中央核心模块
- 每个 parent 一个项目特定模块
- 多个项目取决于核心模块
- 所有 Maven 项目
- 必须打包成jar
- core + secondary 模块应该在每个 intellij 实例中都是可编辑的
所以我尝试了很多结构,但我最后的尝试如下:
Parent POM/project 指向 2 个现有模块。其中一个模块将用于另一个类似结构的项目。
我已经尝试在不同的配置中设置所有这些 poms 的 Maven 依赖关系,但是在构建时我仍然没有得到清单或 noclassdefound and/or 根本无法构建或获得循环依赖关系。
这里是 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TEST</groupId>
<artifactId>CLIENT-PARENT</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>../TEST-SHARED-CORE</module>
<module>../TEST-CLIENT-CLI</module>
</modules>
</project>
这是child一个(核心)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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TEST</groupId>
<artifactId>TEST-SHARED-CORE</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
...
</dependencies>
</project>
和第二个 child 模块 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 http://maven.apache.org/xsd/maven- 4.0.0.xsd">
<modelVersion>4.0.0
</modelVersion>
<groupId>TEST</groupId>
<artifactId>TEST-CLIENT-CLI</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>TEST</groupId>
<artifactId>CLIENT-PARENT</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../TEST-CLIENT-PARENT</relativePath>
</parent>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>TEST</groupId>
<artifactId>TEST-SHARED-CORE</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
我的印象是我的结构非常不正确,那么正确的结构是什么?
编辑:
当这样做时 package/build 这是当 JAR 为 运行 时发生的情况,它没有在 shared-core 中找到 类。
Exception in thread "main" java.lang.NoClassDefFoundError: com/shared/networking/contexts/NetworkContext
at com.Main.main(Main.java:23)
Caused by: java.lang.ClassNotFoundException
你的基本结构看起来没问题。我会将包装元素添加到 CORE 和 CLI 模块
<packaging>jar</packaging>
所以 maven 知道要构建一个 jar 文件。
编辑 2
我相信你目前的结构是
parent ( a pom with modules )
- core ( a jar )
- cli ( a jar )
我认为这个相对路径不正确
<relativePath>../TEST-CLIENT-PARENT</relativePath>
因为这表明
root ( a pom with only modules )
- parent ( a pom with common jar dependencies)
- core ( a jar )
- cli ( a jar )
虽然您希望将所有内容构建为一个多模块项目,但我认为在第一次构建时您将单独构建 parent/root 子模块。
的根 pom