【问题标题】:Build all maven sub modules构建所有 maven 子模块
【发布时间】:2018-10-31 11:28:28
【问题描述】:

我有一个 maven web 项目,它有多个子模块形成一个层次结构。顶部项目的依赖关系在我的 Web 项目中指定,它链接其所有子依赖项并使它们可用于 Web 项目。但问题是,在构建 Web 项目之前,我需要为从层次结构中最低的 jar 项目开始的每个子模块运行 mvn install,我想要一些 maven 配置,它会在我构建时自动构建所有子模块网络项目。

例子:

A(Web 项目) -> (子模块) -> B -> C -> D -> E -> F

当构建 A 时,所有 jar 都应该重新构建并安装在我的计算机上,以反映在我的子模块中所做的更新更改,并且可供 Web 项目 A 使用。

【问题讨论】:

  • 你在使用 Maven <module> 功能吗?
  • 对maven不太了解,请高手指点一下。
  • “子模块”到底是什么意思 C 是否在 D 上有 <dependency> 等等?
  • 右 D 是 C 使用的子模块,每个模块都通过接口调用它的子模块,所以如果我需要交换任何模块,它会很容易

标签: java maven jakarta-ee


【解决方案1】:

多模块项目由引用一个或多个子模块的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 基础知识。

黑客愉快:)

【讨论】:

  • 模块是否需要像parent一样在父目录中-> submodulejar , submodulewar, pom.xml(parent), 因为当我尝试时,它在安装时抛出了一些找不到项目异常的 pom
  • 如果我理解正确 - 是的。您创建一个目录(例如 my-app)。在里面放置 pom.xml(它是父 pom),并在同一级别放置模块。我更新了答案,也许现在更有意义(目录结构部分)
猜你喜欢
  • 2011-07-24
  • 2014-10-30
  • 1970-01-01
  • 1970-01-01
  • 2012-05-30
  • 1970-01-01
  • 2012-01-08
  • 1970-01-01
  • 2019-07-11
相关资源
最近更新 更多