Springboot教程:Springboot多模块maven项目示例

本文学习创建具有多个模块的spring boot项目。父模块将作为基本maven配置容器。子模块将是实际的spring启动项目 - 从父模块继承maven属性。

1. Spring boot maven父模块

父模块是单点触发所有模块的构建过程,并可选择生成可部署的包(例如,ear,war等)。它的pom.xml文件包含所有模块的列表以及子模块继承的公共依赖项和属性。

在我们创建spring boot项目时,我们需要添加spring-boot-starter-parent依赖项。它是父POM,为基于Spring Boot的应用程序提供依赖性和插件管理。

它包含了默认版本的Java的使用,springboot使用的依赖关系的默认版本,并且默认配置的Maven插件的。

Console

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

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?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>

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

子模块:ear, war, jar

子模块可以是任何项目,可以创建任何packaging。我们可以在模块之间创建任何类型的依赖关系,并将它们捆绑在一起。这是根据实际开发中的实际需求决定的。

在通常的情况下,我们创建一个ear文件,一个war文件和一个jar文件。Jar文件被捆绑到war文件中,该文件被捆绑到ear文件中。ear文件是要部署到应用程序服务器上的最终包。

Console

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ 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

$ 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

请根据要求添加第三方库和依赖项。

2.1,jar包

 pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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>

<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,war包

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?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>

<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. ear 包

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?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>

<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构建

要在单个命令中编译和构建所有项目,请转到父项目并运行“mvn clean install”命令。此命令将生成一个带有名称的ear文件HelloWorldApp-1.0-SNAPSHOT.ear

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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] ------------------------------------------------------------------------

 

发表评论

登录后才能评论
关注我们