Build all maven sub modules

北战南征 提交于 2021-01-29 12:35:51

问题


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.

Example:

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

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.


回答1:


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.

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>

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>

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>

The directory structure for this will look like:

  my-parent               // parent dir 
    pom.xml
    sub-module-some-lib   // module dir
      src
      pom.xml
    sub-module-my-webapp  // module dir
      src
      pom.xml 

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.

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.)

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.

Happy hacking :)



来源:https://stackoverflow.com/questions/53082304/build-all-maven-sub-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!