编译所有Maven子模块 [英] Build all maven sub modules

查看:119
本文介绍了编译所有Maven子模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a maven web project which has multiple sub-modules that form a hierarchy. Dependency of the project on top is specified in my web project which links all of its sub-dependencies and make them available to the web project. But the issue is when Before building the web project I need to run mvn install for every sub-modules starting from the jar project which is lowest on the hierarchy, I want some maven configuration which will build all the sub modules automatically when I build the web project.

示例:

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

A(Web project) -> (Sub-Modules) -> B -> C -> D -> E -> F

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

When A is build all the jars should be rebuild and installed on my computer in order to reflect the updated changes done in my sub modules and are available to Web project A.

推荐答案

一个多模块项目是由parent POM引用一个或多个子模块定义的.对于您的情况,我想说您需要为所有模块创建一个并在顶层添加,但是让我提供一个更通用的示例.

A multi-module project is defined by a parent POM referencing one or more submodules. In your case, I would say you need to create one and add at the top level for all your modules, but let me provide just a more general example.

首先,您需要将父级pom.xml称为父级POM,并且应如下所示:

First, you need parent pom.xml will be called parent POM and should look like this:

<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:

Then let's get back to concrete modules which depends on each other. First, our library that will be JAR:

sub-module-some-lib

<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应该先编译!):

Then our webapp which depends on our lib (means lib should be compiled first !):

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.

To make it all work you just need to run mvn clean install command from the parent project (on the topmost pom.xml) and Maven will build the JAR first and then webapp as WAR.

可以扩展此方法以创建具有更多模块甚至更复杂的项目树的项目结构(例如,A依赖于B,B依赖于C等)

This approach can be extended to create project structure with a lot more modules and even more complex project trees (e.g. A depends on B, B depends on C , etc.)

希望这很有道理,也有指南您可以在其中找到更多详细信息以及完整的示例,但这可能需要您首先熟悉Maven基础.

Hope this makes some sense, there is also a guide where you can find more details as well as complete example, but it may require you to get familiar with Maven basics first.

快乐黑客:)

这篇关于编译所有Maven子模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆