1/ 场景
有很多小应用,有一些公共的特点,比如说都依赖了spring-boot、log4j2、lombok、fastjson等等,为了方便开发,可以使用Maven定制一个parent来简化开发,统一管理第三方依赖的版本。(为后续的微服务做准备哦!)
2/ 构建 parent
2.1. 新建一个Maven工程,any-root。
2.2. 修改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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.ictgu</groupId>
<artifactId>any-root</artifactId>
<version>1.0.RELEASE</version>
<packaging>pom</packaging>
<properties>
<spring-boot.version>1.5.3.RELEASE</spring-boot.version>
<java.version>1.8</java.version>
<lombock.version>1.16.16</lombock.version>
<fastjson.version>1.2.32</fastjson.version>
<commons-lang3.version>3.5</commons-lang3.version>
<jsoup.version>1.10.2</jsoup.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Web 容器 undertow -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<!-- 日志 Log4j2 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- 面向切面 aop -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- 代码简化 Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombock.version}</version>
</dependency>
<!-- JSON fastJSON -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- 通用工具类 Apache Commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>${jsoup.version}</version>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
附加一篇文章: Maven实战(六)--- dependencies与dependencyManagement的区别
2.3. 依赖了log4j2,需要在resources目录下添加log4j2的配置文件
项目结构<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Properties>
<Property name="PID">????</Property>
<Property name="LOG_PATTERN">%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n%xwEx</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="${LOG_PATTERN}"/>
</Console>
</Appenders>
<Loggers>
<Logger name="org.hibernate.validator.internal.util.Version" level="warn" />
<Logger name="org.apache.coyote.http11.Http11NioProtocol" level="warn" />
<Logger name="org.apache.tomcat.util.net.NioSelectorPool" level="warn" />
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
2.4 将父工程安装到本地仓库
方式一:使用 IDEA
方式二:使用命令行,进入pom.xml所在目录,执行
mvn clean install
至此,父工程已安装完毕,其它工程依赖此工程即可添加父工程所有依赖。
3/ 快速构建子工程
3.1 新建一个Maven工程:avatar
3.2 修改pom.xml,继承父工程any-root
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.ictgu</groupId>
<artifactId>avatar</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>cn.ictgu</groupId>
<artifactId>any-root</artifactId>
<version>1.0.RELEASE</version>
</parent>
</project>
IDEA 会自动导入父工程中的依赖:
相关依赖3.3 添加启动类,测试lombok/log4j2
启动程序4/ 子项目添加其它依赖,以spring-boot-actuator 为例:
4.1 在avatar项目的基础上,修改pom.xml,添加actuator的依赖
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.ictgu</groupId>
<artifactId>avatar</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>cn.ictgu</groupId>
<artifactId>any-root</artifactId>
<version>1.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
4.2 添加配置文件,在resources下添加application.yml
management:
security:
enabled: false
作用是可以随意访问actuator下的所有端点
4.3 启动项目
启动日志4.4 验证actuator下的端点,访问http://localhost:8080/beans
查看整个应用所有bean的依赖关系:
5/ 总结
将公共依赖定制为父工程,有如下好处:
- 避免手动添加依赖带来的失误操作;
- 统一管理依赖的版本号;
- 加快子工程的构建,一个继承即可获得大量公共服务。
网友评论