本篇文章主要介绍了springboot+maven 多模块项目的构建、运行、打包,分享给大家,具体如下:
项目使用的工具:
- intellij idea
- jdk 1.8
- apache-maven-3.3.9
项目的目录:
- 主项目 springboot-multi
- 子模块 entity、dao、service、web
一、使用idea创建一个springboot项目 : file -> new -> project 项目名称为springboot-multi
二、删除项目中的src目录,把pom.xml中的项目打包方式改为pom,如下:
1
2
3
4
5
|
<groupid>com.example</groupid> <artifactid>springboot-multi</artifactid> <version> 0.0 . 1 -snapshot</version> <!-- 此处改为pom --> <packaging>pom</packaging> |
三、创建springboot-multi项目的子模块,在项目上右键单击,选择:new -> module。
四、创建四个子模块后,删除子模块中 src/main/java、src/main/java下的所有文件(如果没有文件跳过此操作),只保留web子模块的springboot的application主启动类。
五、主项目pom.xml (注意<modules>标签是否指定了子模块)
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
52
53
54
55
56
57
58
59
60
|
<modelversion> 4.0 . 0 </modelversion> <groupid>com.example</groupid> <artifactid>springboot-multi</artifactid> <version> 0.0 . 1 -snapshot</version> <!-- 此处改为pom --> <packaging>pom</packaging> <name>springboot-multi</name> <description>demo project for spring boot</description> <modules> <module>web</module> <module>service</module> <module>dao</module> <module>entity</module> </modules> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 1.5 . 10 .release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceencoding>utf- 8 </project.build.sourceencoding> <project.reporting.outputencoding>utf- 8 </project.reporting.outputencoding> <java.version> 1.8 </java.version> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> </dependencies> <!--指定使用maven打包--> <build> <plugins> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-compiler-plugin</artifactid> <version> 3.1 </version> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-surefire-plugin</artifactid> <version> 2.19 . 1 </version> <configuration> <skiptests> true </skiptests> <!--默认关掉单元测试 --> </configuration> </plugin> </plugins> </build> |
六、web子模块pom.xml(依赖service、dao、entity子模块)
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
52
53
54
55
|
<modelversion> 4.0 . 0 </modelversion> <groupid>com.example</groupid> <artifactid>web</artifactid> <version> 0.0 . 1 -snapshot</version> <packaging>jar</packaging> <name>web</name> <description>demo project for spring boot</description> <parent> <groupid>com.example</groupid> <artifactid>springboot-multi</artifactid> <version> 0.0 . 1 -snapshot</version> <relativepath>../pom.xml</relativepath> </parent> <dependencies> <dependency> <groupid>com.example</groupid> <artifactid>service</artifactid> <version> 0.0 . 1 -snapshot</version> </dependency> <dependency> <groupid>com.example</groupid> <artifactid>dao</artifactid> <version> 0.0 . 1 -snapshot</version> </dependency> <dependency> <groupid>com.example</groupid> <artifactid>entity</artifactid> <version> 0.0 . 1 -snapshot</version> </dependency> </dependencies> <!--spring boot打包的话需要指定一个唯一的入门--> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> <configuration> <!-- 指定该main class 为全局的唯一入口 --> <mainclass>com.example.webapplication</mainclass> <layout>zip</layout> </configuration> <executions> <execution> <goals> <goal>repackage</goal><!--可以把依赖的包都打包到生成的jar包中--> </goals> </execution> </executions> </plugin> </plugins> </build> |
七、service子模块pom.xml(依赖 dao 、entity子模块)
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
|
<modelversion> 4.0 . 0 </modelversion> <groupid>com.example</groupid> <artifactid>service</artifactid> <version> 0.0 . 1 -snapshot</version> <packaging>jar</packaging> <name>service</name> <description>demo project for spring boot</description> <parent> <groupid>com.example</groupid> <artifactid>springboot-multi</artifactid> <version> 0.0 . 1 -snapshot</version> <relativepath>../pom.xml</relativepath> </parent> <dependencies> <dependency> <groupid>com.example</groupid> <artifactid>dao</artifactid> <version> 0.0 . 1 -snapshot</version> </dependency> <dependency> <groupid>com.example</groupid> <artifactid>entity</artifactid> <version> 0.0 . 1 -snapshot</version> </dependency> </dependencies> |
八、dao子模块pom.xml (依赖entity子模块)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<modelversion> 4.0 . 0 </modelversion> <groupid>com.example</groupid> <artifactid>dao</artifactid> <version> 0.0 . 1 -snapshot</version> <packaging>jar</packaging> <name>dao</name> <description>demo project for spring boot</description> <parent> <groupid>com.example</groupid> <artifactid>springboot-multi</artifactid> <version> 0.0 . 1 -snapshot</version> <relativepath>../pom.xml</relativepath> </parent> <dependencies> <dependency> <groupid>com.example</groupid> <artifactid>entity</artifactid> <version> 0.0 . 1 -snapshot</version> </dependency> </dependencies> |
九、entity子模块
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<modelversion> 4.0 . 0 </modelversion> <groupid>com.example</groupid> <artifactid>entity</artifactid> <version> 0.0 . 1 -snapshot</version> <packaging>jar</packaging> <name>entity</name> <description>demo project for spring boot</description> <parent> <groupid>com.example</groupid> <artifactid>springboot-multi</artifactid> <version> 0.0 . 1 -snapshot</version> <relativepath>../pom.xml</relativepath> </parent> |
十、pom.xml文件中需要注意的就是:
- 主项目的modules标签指定的子模块是否正确
- 子模块之间的依赖
- 子模块的parent标签
十一、web子模块的application启动类:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@restcontroller @springbootapplication public class webapplication { public static void main(string[] args) { springapplication.run(webapplication. class , args); } @requestmapping (value = "/test" ,method = requestmethod.get) public string test(){ return "test success" ; } } |
十二、执行main方法启动项目,访问localhost:8080/test,出现如下页面表示项目搭建成功:
十三、项目打包命令: mvn clean package 或者 使用右边工具栏的图形化界面打包也可以:
十四、打包成功日志:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/zeketao/article/details/79413919