maven模块化项目

1.如何理解软件设计的模块化

解决软件的复杂性问题,或说降低软件的复杂性。不至于随着变大而不可控二失败,使其可控、可维护、可扩展。
模块化是以分治法为依据。简单说就是把软件整体划分,划分后的块组成了软件。
这些块都相对独立,之间用接口(协议)通信,每个块完成一个功能,多个块组合可以完成一系列功能。

2.模块化的目的是什么?

2.1 提高工作效率

比如在项目A中你写一个模块,A完成后启动了项目B,在B中就可以直接复用项目A的模块化了。一个可复用的软件可以为将来节省费用,被复用的频率越高,组件的初始化成本就越低。

2.1 提高软件质量

可复用的软件总比不能复用的有更多的质量保障。因为可复用的软件在不断的复用过程中把一些bug,缺陷都很快的排除了。因此可复用的软件一定是有利于系统的可维护性。

3 如何设计模块化

3.1 IDEA创建maven项目

3.1.1 删除src、target目录,只保留pom.xml

父pom.xml增加的依赖可被子模块继承

1
2
3
4
5
6
7
8
9
10
11
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<modules>
<module>model</module>
<module>dao</module>
<module>service</module>
<module>webapi</module>
</modules>

3.2 创建子模块(module)

1—>: file -> new -> module 输入model
2—>: file -> new -> module 输入dao
3—>: file -> new -> module 输入service

3.3 修改子模块pom.xml配置

1
2
3
4
5
6
<parent>
<artifactId>test</artifactId>
<groupId>test</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

注意../pom.xml此段必须加上,用来继承父模块

如果要使用其他模块的程序,必须增加此模块的依赖

1
2
3
4
5
<dependency>
<groupId>test</groupId>
<artifactId>service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

至此模块化配置完成。

3.4 模块化打包

模块化打包和非模块化打包不一样,需要选择有启动项目(public static void main(String args[]){
SpringApplication.run(Application.class,args);
})的java文件所在模块为主包,需增加一下配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

打包命令和之前一样mvn package -DskipTests

我们运行的真正war包为带exec的。