[transfer] Introduction to spring boot (4) – multi module Maven project

Time:2020-10-24

Please refer to: introduction to spring boot (4) – multi module Maven project

This article introduces a spring boot project with multiple modules. The parent module is used for the basic Maven configuration container, and the child module is the actual spring boot project, and inherits Maven attributes from the parent module.

1. Spring boot Maven parent module

The parent module is the entry module that triggers the construction process of all modules. It can be configured to generate deployment packages (such as ear, war, etc.), and pom.xml The file contains a list of all submodules, as well as the common dependencies and properties inherited by the submodules.

When creating a spring boot project, you need to add a spring boot starter parent dependency to provide dependency and plug-in management for spring boot based applications, including the default version of Java, the default version of spring boot dependency, and the default configuration of Maven plug-in.

Console command line:

$ mvn archetype:generate -DgroupId=com.howtodoinjava
                        -DartifactId=HelloWorldApp
                        -DarchetypeArtifactId=maven-archetype-quickstart
                        -DinteractiveMode=false

pom.xml

<?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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.howtodoinjava</groupId>
    <artifactId>HelloWorldApp</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>HelloWorldApp</name>
    <url>http://maven.apache.org</url>
 
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
 
    <modules>
        <module>HelloWorldApp-ear</module>
        <module>HelloWorldApp-service</module>
        <module>HelloWorldApp-rws</module>
    </modules>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
 
</project>

2. Sub modules – ear, war, jar

According to the different requirements of individuals and projects, sub modules can be any project, can be typed into any desired package format, create the required dependencies between sub modules, or package them together.

Sometimes you want to create ear files, war files, or jar files. Jar files can be packaged into war files, war files can be packaged into ear files, and ear files may be the final package that needs to be deployed to the application server.

Console command line:

$ cd HelloWorldApp
  
$ mvn archetype:generate -DgroupId=com.howtodoinjava
                        -DartifactId=HelloWorldApp-ear
                        -DarchetypeArtifactId=maven-archetype-quickstart
                        -DinteractiveMode=false
  
$ mvn archetype:generate -DgroupId=com.howtodoinjava
                        -DartifactId=HelloWorldApp-service
                        -DarchetypeArtifactId=maven-archetype-quickstart
                        -DinteractiveMode=false
  
$ mvn archetype:generate -DgroupId=com.howtodoinjava
                        -DartifactId=HelloWorldApp-rws
                        -DarchetypeArtifactId=maven-archetype-webapp
                        -DinteractiveMode=false

Add third-party libraries and dependencies as required.

2.1. Jar package


pom.xml

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
 
    <parent>
        <groupId>com.howtodoinjava</groupId>
        <artifactId>HelloWorldApp</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
 
    <artifactId>HelloWorldApp-service</artifactId>
    <name>HelloWorldApp-service</name>
    <url>http://maven.apache.org</url>
 
    <dependencies>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</project>

2.2. Pack war


pom.xml

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
 
    <parent>
        <groupId>com.howtodoinjava</groupId>
        <artifactId>HelloWorldApp</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
 
    <artifactId>HelloWorldApp-rws</artifactId>
    <packaging>war</packaging>
    <name>HelloWorldApp-rws Maven Webapp</name>
    <url>http://maven.apache.org</url>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 
        <!-- Package jar in the war file -->
        <dependency>
            <groupId>com.howtodoinjava</groupId>
            <artifactId>HelloWorldApp-service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
 
    </dependencies>
 
    <build>
        <finalName>HelloWorldApp-rws</finalName>
    </build>
</project>

2.3. Pack ear

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.howtodoinjava</groupId>
        <artifactId>HelloWorldApp</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>HelloWorldApp-ear</artifactId>
    <name>HelloWorldApp-ear</name>
    <url>http://maven.apache.org</url>
    <packaging>ear</packaging>
 
    <dependencies>
        <!-- Package war in the ear file -->
        <dependency>
            <groupId>com.howtodoinjava</groupId>
            <artifactId>HelloWorldApp-rws</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>war</type>
        </dependency>
    </dependencies>
 
    <!-- Plugin to bundle the ear file-->
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-ear-plugin</artifactId>
                    <version>3.0.1</version>
                    <configuration>
                        <finalName>HelloWorldApp-${project.version}</finalName>
                        <modules>
                            <webModule>
                                <groupId>com.howtodoinjava</groupId>
                                <artifactId>HelloWorldApp-rws</artifactId>
                                <uri>HelloWorldApp-rws-1.0-SNAPSHOT.war</uri>
                                <!-- Set custom context root -->
                                <contextRoot>/application</contextRoot>
                            </webModule>
                        </modules>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

3. Maven construction

To compile and build all modules from the command line, go to the parent module and run the “MVN clean install” command. This command will be generated with the name helloworldapp-1.0- SNAPSHOT.ear The ear file.

Console command line:

E:\HelloWorldApp>mvn clean install
...
...
...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] HelloWorldApp 1.0-SNAPSHOT ......................... SUCCESS [  0.428 s]
[INFO] HelloWorldApp-service .............................. SUCCESS [  1.000 s]
[INFO] HelloWorldApp-rws Maven Webapp ..................... SUCCESS [  1.322 s]
[INFO] HelloWorldApp-ear 1.0-SNAPSHOT ..................... SUCCESS [  0.813 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.063 s
[INFO] Finished at: 2018-11-19T09:28:31+05:30
[INFO] ------------------------------------------------------------------------

Happy coding!

more

Using the console console to run a multi module Maven project
Running multi module Maven projects with eclipse

Recommended Today

Elasticsearch learning one (basic introduction)

1、 Introduction to elasticsearch You can describe elasticsearch as follows: A distributed real-time document storage, each field can be indexed and searched A distributed real time analysis search engine It is capable of extending hundreds of service nodes and supports Pb level structured or unstructured data Elasticsearch is a real-time distributed search and analysis engine […]