创建自己的 Spring Boot Starter Parent

创建 Spring Boot 项目时,默认继承一个 parent。

1
2
3
4
5
6
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

spring-boot-starter-parent 具有以下作用:

  • 父级为 spring-boot-dependencies(项目url、license、developer、scm、依赖版本,依赖插件版本管理)
  • 默认项目编码
  • 默认 Java 版本
  • 默认 resource 设置
  • 依赖插件管理

自定义 parent 有以下优势:

  • 自定义开发规范:在自定义的 parent 中可以规范引入的依赖,统一配置信息
  • 提高开发效率:不用每个人都去关心共性的部分
  • 统一模块插件化管理:可以在 parent 实现需要的插件配置,如:redis、mysql、日志、参数校验等
  • 项目版本升级和依赖包升级更加方便统一
  • 日志收集等可以从切面获取各个项目数据和日志等
  • 目持续集成部署交付方便处理
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?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.example</groupId>
<artifactId>parent</artifactId>
<!-- 注意:packaging 必须为pom -->
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>Self Parent Project</name>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.17.RELEASE</version>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<java.version>1.8</java.version>
<commons-lang.version>3.4</commons-lang.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<finalName>${project.artifactId}</finalName>
<pluginManagement>
<plugins>
<!-- 发布插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<!--<preparationGoals>clean package</preparationGoals>-->
<autoVersionSubmodules>true</autoVersionSubmodules>
<!-- 不使用默认的profile -->
<tagNameFormat>v@{project.version}</tagNameFormat>
<useReleaseProfile>false</useReleaseProfile>
<!-- 在发布时不检查是否提交 svn 的文件过滤配置 -->
<allowReleasePluginSnapshot>true</allowReleasePluginSnapshot>
<allowTimestampedSnapshots>true</allowTimestampedSnapshots>
<checkModificationExcludes>
<checkModificationExclude>.project</checkModificationExclude>
<checkModificationExclude>.settings</checkModificationExclude>
<checkModificationExclude>.classpath</checkModificationExclude>
<checkModificationExclude>**\.project</checkModificationExclude>
<checkModificationExclude>**\.settings</checkModificationExclude>
<checkModificationExclude>**\.classpath</checkModificationExclude>
</checkModificationExcludes>
<!--<commitByProject>true</commitByProject>-->
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-provider-gitexe</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
</plugin>
<!-- 生成sources源码包的插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 用来校验约定遵守情况插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0-M2</version>
<executions>
<execution>
<id>enforce-banned-dependencies</id> <!-- 一个执行实例的ID -->
<goals>
<goal>enforce</goal> <!-- 执行的命令 -->
</goals>
<configuration>
<rules> <!-- 规则 -->
<bannedDependencies> <!-- 禁止依赖 -->
<excludes>
<exclude>
commons-lang:commons-lang <!-- 不允许出现这个依赖 -->
</exclude>
</excludes>
</bannedDependencies>
</rules>
<fail>true</fail> <!-- 不符合规则时就报错 -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>

<plugins>
<!-- maven-enforcer-plugin 这个插件会对项目环境进行检查 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
</plugin>
</plugins>
</build>

<distributionManagement>
<repository>
<id>Releases</id>
<name>Local Nexus Repository</name>
<url>http://192.168.1.177:8081/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>Snapshots</id>
<name>Local Nexus Repository</name>
<url>http://192.168.1.177:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>

</project>