跳至主要內容

统一bom文件

杨轩-国实信息大约 5 分钟framework

git地址:

http://10.16.202.103:8089/component/component-ser/gosci-tech-bomopen in new window

一、bom文件解决了什么问题

bom实际上就是一个maven pom文件,通过定制化pom文件,作为所有项目的父pom,可以实现以下功能

1、统一java版本

<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>

通过该项配置,可以固定版本号为1.8以及编译级别也是1.8,否则默认编译级别为1.5会报错。

2、统一UTF-8编码

在windows平台下如果不指定编码类型,会默认使用GBK编码,这样就会造成中文乱码等问题,统一UTF-8编码可解决此问题

<!-- 基础编码字符集 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<charset.UTF8>UTF-8</charset.UTF8>

3、固定常用依赖版本号

由于使用了spring全家桶,为了避免各个开发人员使用的spring版本号不一致导致的jar包冲突,在bom文件里固定挑选好了spring版本号,可以解决这个问题。

除此之外,固定其它常用依赖的版本号,也能解决相似的问题。

4、集成常用Maven插件

通过继承,将常用插件传递给各个使用此bom的项目,项目里就不用再手动配置相关插件了,会默认生效。

此bom配置了默认跳过单元测试、强制java版本号、配置打包参数等默认配置。

5、统一jar包上传配置

    <distributionManagement>
        <repository>
            <id>releaseid</id>
            <name>Nexus Release Repository</name>
            <url>http://10.16.202.153:8091/repository/wb_test_release/</url>
        </repository>
        <snapshotRepository>
            <id>snapshotid</id>
            <name>Nexus Snapshot Repository</name>
            <url>http://10.16.202.153:8091/repository/wb_test_snapshot/</url>
        </snapshotRepository>
    </distributionManagement>

通过上述配置,可以指定maven上传的配置,通过继承传递给各个项目,各个直接使用命令mvn clean deploy命令即可上传到nexus私服。

二、使用bom文件

使用方式很简单,为项目指定父pom即可

    <parent>
        <artifactId>gosci-tech-bom</artifactId>
        <groupId>com.gosci.tech</groupId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

这样,项目就会继承完整的属性、依赖、plugin、distributionManagement等信息。

注意

不要使用:

<dependencies>
    <dependency>
        <groupId>com.gosci.tech</groupId>
        <artifactId>gosci-tech-bom</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
</dependencies>

这种方式引入项目,这样的方式无法通过继承获取properties属性配置以及默认的依赖配置、默认的plugin配置等,不推荐这种使用方式。

三、常用版本控制

<?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>com.gosci.tech</groupId>
    <artifactId>gosci-tech-bom</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <packaging>pom</packaging>

    <properties>
        <!-- JDK版本 -->
        <java.version>1.8</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>

        <!-- 基础编码字符集 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <charset.UTF8>UTF-8</charset.UTF8>

        <!-- spring boot& spring cloud -->
        <spring-cloud-alibaba.version>2021.0.4.0</spring-cloud-alibaba.version>
        <spring-cloud.version>2021.0.4</spring-cloud.version>
        <spring-boot.version>2.7.3</spring-boot.version>

        <!-- jwt -->
        <jwt.version>0.9.1</jwt.version>
        <spring-security.jwt.version>1.1.1.RELEASE</spring-security.jwt.version>

        <!-- apache common -->
        <commons-lang3.version>3.12.0</commons-lang3.version>
        <commons-io.version>2.11.0</commons-io.version>
        <commons-pool2.version>2.8.1</commons-pool2.version>
        <commons-collections4.version>4.4</commons-collections4.version>
        <commons-codec.version>1.15</commons-codec.version>
        <commons-text.version>1.9</commons-text.version>
        <commons-net.version>3.7.2</commons-net.version>

        <!-- 其它 -->
        <hutool.version>5.7.3</hutool.version>
        <swagger.version>1.6.2</swagger.version>
        <transmittable-thread-local.version>2.12.0</transmittable-thread-local.version>
        <lombok.version>1.18.12</lombok.version>
        <hibernate-validator.version>6.2.0.Final</hibernate-validator.version>
        <servlet-api.version>2.5</servlet-api.version>
        <okhttp3.version>4.8.1</okhttp3.version>
        <!-- capture -->
        <easy.capture.version>1.6.2</easy.capture.version>
        <elasticsearch.version>7.3.0</elasticsearch.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-jwt</artifactId>
                <version>${spring-security.jwt.version}</version>
            </dependency>
            <dependency>
                <groupId>io.jsonwebtoken</groupId>
                <artifactId>jjwt</artifactId>
                <version>${jwt.version}</version>
            </dependency>
            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
                <version>${hutool.version}</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>transmittable-thread-local</artifactId>
                <version>${transmittable-thread-local.version}</version>
            </dependency>
            <dependency>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-annotations</artifactId>
                <version>${swagger.version}</version>
            </dependency>
            <dependency>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-models</artifactId>
                <version>${swagger.version}</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate.validator</groupId>
                <artifactId>hibernate-validator</artifactId>
                <version>${hibernate-validator.version}</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>${servlet-api.version}</version>
            </dependency>
            <!-- okhttp3 -->
            <dependency>
                <groupId>com.squareup.okhttp3</groupId>
                <artifactId>okhttp</artifactId>
                <version>${okhttp3.version}</version>
            </dependency>
            <dependency>
                <groupId>com.squareup.okhttp3</groupId>
                <artifactId>logging-interceptor</artifactId>
                <version>${okhttp3.version}</version>
            </dependency>

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

            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-pool2</artifactId>
                <version>${commons-pool2.version}</version>
            </dependency>

            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>${commons-io.version}</version>
            </dependency>

            <dependency>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
                <version>${commons-codec.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-collections4</artifactId>
                <version>${commons-collections4.version}</version>
            </dependency>

            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-text</artifactId>
                <version>${commons-text.version}</version>
            </dependency>

            <dependency>
                <groupId>com.github.whvcse</groupId>
                <artifactId>easy-captcha</artifactId>
                <version>${easy.capture.version}</version>
            </dependency>

        </dependencies>

    </dependencyManagement>


    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <!-- 单元测试插件 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.12.4</version>
                    <configuration>
                        <argLine>-Dfile.encoding=UTF-8</argLine>
                    </configuration>
                </plugin>
                <!-- 强制java版本号 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <compilerArgument>-parameters</compilerArgument>
                        <encoding>UTF-8</encoding>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <!-- 上传源码 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <version>3.0.1</version>
                    <configuration>
                        <attach>true</attach>
                    </configuration>
                    <executions>
                        <execution>
                            <phase>compile</phase>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <!-- deploy插件 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>2.5.6</version>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <!-- 单元测试插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <argLine>-Dfile.encoding=UTF-8</argLine>
                    <skip>true</skip>
                </configuration>
            </plugin>
            <!-- 强制java版本号 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <compilerArgument>-parameters</compilerArgument>
                    <encoding>UTF-8</encoding>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <!-- 上传源码 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <configuration>
                    <attach>true</attach>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <distributionManagement>
        <repository>
            <id>releaseid</id>
            <name>Nexus Release Repository</name>
            <url>http://10.16.202.153:8091/repository/wb_test_release/</url>
        </repository>
        <snapshotRepository>
            <id>snapshotid</id>
            <name>Nexus Snapshot Repository</name>
            <url>http://10.16.202.153:8091/repository/wb_test_snapshot/</url>
        </snapshotRepository>
    </distributionManagement>

</project>