Maven的BOM

9/7/2021

# 考虑

虽然spring-boot与spring-cloud家族有默认的parent pom,但是由于spring-boot和spring-cloud系列更新太过频繁,所以对于一个技术团队而言,设置自己的独有的parent才能做到整个项目亦或整个团队的版本统一,技术统一。

# BOM

如果你只听过maven的pom,但是你没听过maven的bom的话,而且你还在用spring技术体系的话,那么你非常强烈建议你去了解bom。

使用BOM可以让使用者在子pom.xml声明依赖的时不需要指定版本号,最重要的是可以解决依赖冲突。

而不幸的是 spring 系列相关的依赖经常冲突,不用bom能行么?

# SmartAdmin Parent

该项目位于:

本项目使用了parent和bom形式,定义自己的parent。使用一个parent统一管理所有的技术版本,也方便将来扩展springcloud。

# 特点

# 1 依赖版本统一定义版本变量

在parent的pom.xml文件里定义了所有的版本变量,这样可以只关注此区域,不用再看下面,因为下面<dependencies>节点里很长,翻阅起来很累又不易查看,而且随着系统的变大也会越来越长,但是<properties>节点不会。

<properties>
        <java.version>1.8</java.version>
        <hibernate.annotations.version>3.3.0.ga</hibernate.annotations.version>
        <log4j.web.version>2.10.0</log4j.web.version>
        <smartadmin.springboot.version>2.0.5.RELEASE</smartadmin.springboot.version>
        <smartadmin.springcloud.version>Finchley.SR1</smartadmin.springcloud.version>
        <spring-framework-bom.version>5.0.9.RELEASE</spring-framework-bom.version>
...
...
</properties>
1
2
3
4
5
6
7
8
9
10
# 2 maven profile的定义

笔者认为所有的项目都应该有以下四个环境:

  • 开发环境 (dev,用于写代码)
  • 测试环境 (sit,测试人员测试)
  • 预发布环境(pre, 真实的数据,最真实的生产环境)
  • 生产环境(prod, 生产环境)

默认为dev环境,即开发环境。

配置如下:

 <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <profiles.active>dev</profiles.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>sit</id>
            <properties>
                <profiles.active>sit</profiles.active>
            </properties>
        </profile>
        <profile>
            <id>pre</id>
            <properties>
                <profiles.active>pre</profiles.active>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <profiles.active>prod</profiles.active>
            </properties>
        </profile>
    </profiles>
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
# 3 配置文件目录 resources

因为使用了maven profile,所以必须在resources目录下建立不同环境的配置文件夹,如下图。

--------\src\main\resources
--------\src\main\resources\dev
--------\src\main\resources\sit
--------\src\main\resources\pre
--------\src\main\resources\prod

1
2
3
4
5
6
# 4 打包配置build

maven profile的打包核心是对配置文件的过滤,如下

<resources>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
            <resource>
                <filtering>false</filtering>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>dev/*</exclude>
                    <exclude>sit/*</exclude>
                    <exclude>pre/*</exclude>
                    <exclude>prod/*</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources/${profiles.active}</directory>
                <filtering>true</filtering>
                <includes>
                    <include>*.properties</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources/${profiles.active}</directory>
                <filtering>false</filtering>
                <includes>
                    <include>*.*</include>
                </includes>
            </resource>
        </resources>		
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

作者简介: 卓大 (opens new window), 1024创新实验室主任,混迹于各个技术圈,熟悉点java,略懂点前端。

加“卓大”微信,入群 关注 1024创新实验室! 我要请 1024创新实验室 喝胡辣汤~
告白气球 (钢琴版)
JESSE T