多模块项目由引用一个或多个子模块的parent POM 定义。在您的情况下,我会说您需要为所有模块创建一个并在顶层添加,但让我提供一个更一般的示例。
首先,您需要父 pom.xml 将被称为父 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.my.group.id</groupId>
<artifactId>my-parent</artifactId>
<!-- The parent project doesn’t create a JAR or a WAR like our previous projects;
instead, it is simply a POM that refers to other Maven projects. -->
<packaging>pom</packaging>
<version>1.0</version>
<!-- ================================================================= -->
<!-- Put all your submodules here (for this example I add two) -->
<modules>
<!-- This is list of artifactId for all your sub-modules -->
<module>sub-module-some-lib</module>
<module>sub-module-my-webapp</module>
</modules>
<!-- ================================================================= -->
<!-- everything else -->
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<!-- dependencies, etc. -->
</dependencies>
</project>
然后让我们回到相互依赖的具体模块。首先,我们的 JAR 库:
子模块一些库
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- ================================================================= -->
<!-- Notice that we declare parent here -->
<parent>
<groupId>com.my.group.id</groupId>
<artifactId>my-parent</artifactId>
<version>1.0</version>
</parent>
<!-- ================================================================= -->
<artifactId>sub-module-some-lib</artifactId>
<packaging>jar</packaging>
<dependencies>
<!-- e.g. specific dependencies for this module -->
</dependencies>
</project>
然后是依赖于我们的 lib 的 webapp(意味着应该先编译 lib!):
sub-module-my-webapp
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- Notice that we declare parent here -->
<parent>
<groupId>com.my.group.id</groupId>
<artifactId>my-parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>sub-module-my-webapp</artifactId>
<packaging>war</packaging>
<dependencies>
<!-- some other dependencies -->
<!-- But our webapp depends on our sub-module-some-lib
so we need to add it as dependency ! -->
<dependency>
<groupId>com.my.group.id</groupId>
<artifactId>sub-module-some-lib</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<!-- build, plugins, etc. -->
</project>
目录结构如下:
my-parent // parent dir
pom.xml
sub-module-some-lib // module dir
src
pom.xml
sub-module-my-webapp // module dir
src
pom.xml
要使这一切正常运行,您只需从父项目(位于最顶层的 pom.xml)运行 mvn clean install 命令,Maven 将首先构建 JAR,然后将 webapp 构建为 WAR。
这种方法可以扩展到创建具有更多模块甚至更复杂的项目树的项目结构(例如 A 依赖于 B,B 依赖于 C 等)
希望这有点道理,there is also a guide 在这里您可以找到更多详细信息和完整示例,但可能需要您先熟悉 Maven 基础知识。
黑客愉快:)