In this fourth part of SpringCloud Alibaba microservices series, we optimize the previous microservices framework and manage all component versions through Maven BOM mechanism.

Introduction of BOM

Bill of Materials (BOM) is a function provided by Maven. It defines a set of compatible JAR package versions. You only need to rely on the BOM file to use the required dependent JAR packages without specifying the version number. BOM maintainers are responsible for version upgrades and for ensuring compatibility between the VERSIONS of JAR packages defined in the BOM.

Why use BOM

The main reason for using BOM is that it resolves dependency conflicts and prevents NoSuchMethodError, ClassNotFoundException and other uncontrollable exceptions from occurring in your project.

Project reform

In the SpringCloud project system, we agreed to use the master POM file to control the version related to the SpringCloud version, and use the customized BOM to control the version of third-party components or public modules. Then, we reformed the original framework step by step.

  • This module is very simple. There is only one POM file, which defines the third-party components and public modules that the project module needs to rely on. The complete POM file is as follows:
<? The 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" > The < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Jianzh5. Cloud < / groupId > < artifactId > cloud - bom < / artifactId > < packaging > pom < / packaging > < version > 1.0.0 < / version > < properties > < mybatis - plus. Version > 3.1.1 < / mybatis - plus version > < mysql version > 5.1.47 < / mysql version > < cloud - alibaba. Version > 1.0.0 < / cloud - alibaba. Version > < / properties > <! <dependencyManagement> <dependencies> <! Cloud </groupId> <artifactId> Cloud-common </artifactId> <version>${cloud-alibaba.version}</version> </dependency> <dependency> <groupId>com.jianzh5.cloud</groupId> <artifactId>account-feign</artifactId> <version>${cloud-alibaba.version}</version> </dependency> <dependency> <groupId>com.jianzh5.cloud</groupId> <artifactId>product-feign</artifactId> <version>${cloud-alibaba.version}</version> </dependency> <! --database--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus.version}</version> </dependency> </dependencies> </dependencyManagement> <repositories> <repository> <id>nexus-aliyun</id> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </repository> </repositories> </project>Copy the code

Note that this module should not define dependencies, otherwise the project will report an exception (an infinite loop) after importing the BOM in the main POM file. The error is as follows:

The build could not read 1 project -> [Help 1] The project com.jianzh5.cloud:cloud-aliaba:1.0.0 (D:\project_jianzh5\cloud-aliaba\pom.xml) has 1 error The dependencies of type=pom and with scope=import form a cycle: Com.jianzh5. cloud:cloud-bom: [email protected]. cloud:cloud-bom:1.0.0 To see the full stack trace of the errors, re-run Maven with the -e switch. Re-run Maven using the -X switch to enable full debug logging. For more information about the errors and possible solutions, please read the following articles:Copy the code

  • Introduce the POM of the BOM module into the main POM file
<! Cloud </groupId> <artifactId> Cloud-bom </artifactId> <version>1.0.0</version> <type>pom</type> <scope>import</scope> </dependency>Copy the code

This paragraph needs to be placed first in dependencyManagement

  • Remove references to components from other modulesDefinitions, such as:
<dependency>
    <groupId>com.jianzh5.cloud</groupId>
    <artifactId>cloud-common</artifactId>
</dependency>Copy the code

  • The overall structure after transformation is as follows

  • After the transformation, the original project will be tested to ensure that the original functions will not be affected. It is recommended to introduce BOM for management in the early stage of the project, and the later transformation will be relatively troublesome.

So far we have completed the unified version management of the project, so the “SpringCloud Alibaba Micro-service practice four – version management” chapter is over, we will see you next time!

series

  • SpringCloud Alibaba micro-service practice I – basic environment preparation
  • SpringCloud Alibaba micro-service practice ii – service registration
  • SpringCloud Alibaba micro-service practice 3 – service invocation

Please scan the code to follow the wechat public account or personal blog