Spring Boot 依赖管理

本文将介绍 Spring Boot 的依赖管理

本文将使用 Spring Boot 2.0.0.RELEASE 版本为例,简单介绍 Spring Boot 的依赖管理。

通常我们创建的 Spring Boot 项目直接使用 <parent> 标签从 Spring Boot 2.0.0.RELEASE 继承依赖和插件配置信息。例如:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

如果你使用的 IDEA 或 Eclipse 按住 Ctrl,点击 “spring-boot-starter-parent” 导航到  spring-boot-starter-parent-2.0.0.RELEASE.pom 文件,该文件内容如下(忽略了插件内容):

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath>../../spring-boot-dependencies</relativePath>
    </parent>
    <artifactId>spring-boot-starter-parent</artifactId>
    <packaging>pom</packaging>
    <name>Spring Boot Starter Parent</name>
    <description>Parent pom providing dependency and plugin management for applications
built with Maven</description>
    <url>https://projects.spring.io/spring-boot/#/spring-boot-starter-parent</url>
    <properties>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <resource.delimiter>@</resource.delimiter>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>${java.version}</maven.compiler.target>
    </properties>
    <build>
        <resources>
            <resource>
                <filtering>true</filtering>
                <directory>${basedir}/src/main/resources</directory>
                <includes>
                    <include>**/application*.yml</include>
                    <include>**/application*.yaml</include>
                    <include>**/application*.properties</include>
                </includes>
            </resource>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <excludes>
                    <exclude>**/application*.yml</exclude>
                    <exclude>**/application*.yaml</exclude>
                    <exclude>**/application*.properties</exclude>
                </excludes>
            </resource>
        </resources>
        <pluginManagement>
            <!-- 忽略插件配置 -->
        </pluginManagement>
    </build>
</project>

上面文件中,在 <resource> 标签中包含了 src/main/resources 目录下面的  application*.yml、application*.yaml 和 application*.properties 文件。该 pom 文件同样也使用了 <parent> 标签,从 spring-boot-dependencies 中继承依赖等配置。spring-boot-dependencies-2.0.0.RELEASE.pom 文件内容如下:

<?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>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.0.0.RELEASE</version>
    <packaging>pom</packaging>
    <name>Spring Boot Dependencies</name>
    <description>Spring Boot Dependencies</description>
    <url>https://projects.spring.io/spring-boot/#</url>
    <licenses>
        <license>
            <name>Apache License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0</url>
        </license>
    </licenses>
    <developers>
        <developer>
            <name>Pivotal</name>
            <email>info@pivotal.io</email>
            <organization>Pivotal Software, Inc.</organization>
            <organizationUrl>http://www.spring.io</organizationUrl>
        </developer>
    </developers>
    <scm>
        <url>https://github.com/spring-projects/spring-boot</url>
    </scm>
    <properties>
        <activemq.version>5.15.3</activemq.version>
        <antlr2.version>2.7.7</antlr2.version>
        <appengine-sdk.version>1.9.62</appengine-sdk.version>
        <!-- 忽略其他属性 -->
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot</artifactId>
                <version>2.0.0.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-test</artifactId>
                <version>2.0.0.RELEASE</version>
            </dependency>
            <!-- 忽略其他依赖 -->
        </dependencies>
    </dependencyManagement>
    <repositories>
        <repository>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <id>spring-milestone</id>
            <name>Spring Milestone</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
        <repository>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <id>spring-snapshot</id>
            <name>Spring Snapshot</name>
            <url>https://repo.spring.io/snapshot</url>
        </repository>
        <repository>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <id>rabbit-milestone</id>
            <name>Rabbit Milestone</name>
            <url>https://dl.bintray.com/rabbitmq/maven-milestones</url>
        </repository>
    </repositories>
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <artifactId>kotlin-maven-plugin</artifactId>
                    <version>${kotlin.version}</version>
                </plugin>
                <plugin>
                    <groupId>org.jooq</groupId>
                    <artifactId>jooq-codegen-maven</artifactId>
                    <version>${jooq.version}</version>
                </plugin>
                <!-- 忽略其他插件 -->
        </pluginManagement>
    </build>
</project>

Spring Boot 每次发布时都会提供一个它所支持的精选依赖列表(更新上面文件的 <dependencyManagement> 标签)。实际上,在构建配置里你不需要提供任何依赖的版本,因为 Spring Boot 已经替你管理好了。当更新 Spring Boot 时,那些依赖也会一起更新。

如果要彻底理解这种设计带来的好处,你可以先看看 maven 的 <dependencyManagement> 和 <pluginManagement> 标签含义及用法。

注意:如果有必要,你可以指定依赖的版本来覆盖 Spring Boot 默认版本。精选列表包括所有能够跟 Spring Boot 一起使用的 Spring 模块及第三方库,该列表可以在上面文件中的 <properties> 标签中获取到,也可以找到一些支持 Maven 和 Gradle 的资料。Spring Boot每次发布都关联一个Spring框架的基础版本,所以强烈建议你不要自 己指定Spring版本。

一个不注意小事情的人,永远不会成功大事业。——戴尔·卡耐基
0 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号