第二篇:Spring Boot对构建系统的支持

一、构建系统

      强烈推荐您选择一个支持依赖管理 的构建系统, 还可以将artifacts发布到“Maven Central”仓库。我们建议您选择Maven或者Gradle。虽然可以让Spring Boot与其它构建系统(如Ant)配合工作,但它们不会得到特别好的支持。

   1.1 依赖管理 

       每一个版本的SpringBoot提供了一个它所支持的依赖内置清单。实际上,您不需要为构建配置提供任何版本的依赖,因为Spring Booot正在为您管理。当您升级Spring Boot时,这些依赖也将以一致的方式进行升级。

      如果您觉得有必要,您仍然可以指定一个版本并覆盖Spring Boot的推荐。

     管理的列表中包含可以使用Spring Boot的所有Spring模块以及第三方库的精简列表。 该列表可作为标准的物料(Materials)清单(spring-boot-dependencies)使用,并且还提供了对 Maven 和 Gradle 的额外支持。

    Spring Boot的每个版本与Spring Framework的基本版本相关联,因此我们强烈建议您不要自己指定其版本。


二、Maven

Maven用户可以从spring-boot-starter-parent-parent 项目中继承,以获得合理的默认值。父项目提供以下功能:

  • (Spring Boot 2.x)Java 1.8作为默认编译器级别。(Spring Boot 1.x)Java 1.6作为默认编译器级别。
  • 源代码UTF-8编码。
  • 依赖关系管理,允许您省略常见依赖的标签,其默认版本继承自spring-boot-dependencies POM。
  • 更合理的资源过滤
  • 更合理的插件配置(exec pluginsurefireGit commit IDshade)。
  • 针对application.properties和application.yml的更合理的资源过滤,包括特定的文件(例如application-foo.properties和application-foo.yml)
  • 最后一点:由于默认的配置文件接受Spring样式占位符(${…}),Maven过滤更改为使用 @..@ 占位符(您可以使用Maven属性resource.delimiter覆盖它)。

2.1 继承启动器parent

要将项目配置为继承spring-boot-starter-parent,只需设置标签如下:

<!-- Inherit defaults from Spring Boot -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.2.RELEASE</version>
</parent>

您只需要在此依赖上指定Spring Boot版本号。如果您导入其它的starter,则可以安全地省略版本号。

 

通过该设置,您还可以通过覆盖自己项目中的配置属性来覆盖单个依赖。例如,要升级到另一个Spring Data发行版本,您需要将以下内容添加到您的 pom.xml文件中。

 

<properties>
    <spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
</properties>

 

检查 spring-boot-dependenciespom 以获取受支持的属性列表


2. 2 使用没有父POM的Spring Boot

不是每个人都喜欢从 spring-boot-starter-parent 继承POM配置。您可能需要使用自己公司标准的父POM,或者您可能只是希望明确地声明所有的Maven配置。

如果您不想使用 spring-boot-starter-parent,则仍然可以通过使用 scope=import 依赖来维持依赖管理(但不是插件管理)的好处:

<dependencyManagement>
     <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.0.2.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

该设置不允许您使用如上属性来覆盖单个依赖。要实现相同的效果,您需要在您项目的 dependencyManagement 配置项中的 spring-boot-dependencies 依赖配置之前 添加配置。比如,要升级到另一个Spring Data发行版,您需要将以下内容添加到您的 pom.xml文件中。

<dependencyManagement>
    <dependencies>
        <!-- Override Spring Data release train provided by Spring Boot -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-releasetrain</artifactId>
            <version>Fowler-SR2</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.0.2.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

以上示例中,我们指定了一个 BOM,但是任何的依赖类型都可以用这个方法来覆盖。

2.3 变更Java版本

spring-boot-starter-parent 选择比较保守的Java兼容性。如果您想听取我们的建议使用更高的Java版本,您可以添加 java.version 属性:

<properties>
    <java.version>1.8</java.version>
</properties>

 2.4  使用Spring Boot Maven插件

Spring Boot包括了一个 Maven插件,它可以将项目打包成一个可执行jar。如果要使用它,请将插件添加到您的 <plugins> 中:

 

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

 

如果您使用Spring Boot starter的父pom,则只需要添加插件。除非您要修改父级中定义的设置,否则不需要进行配置。

三、Gradle

Gradle用户可以直接在其依赖关系部分导入启动器不像Maven,没有超级父导入来共享一些配置。

repositories {
    jcenter()
}
 
dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:1.5.10.RELEASE")
}

spring-boot-gradle-plugin也是可用的,它提供了从源代码创建可执行jar并运行项目的任务。它还提供依赖关系管理,除其他功能外,还允许您省略由Spring Boot管理的任何依赖关系的版本号:

 

plugins {
    id 'org.springframework.boot' version '1.5.10.RELEASE'
    id 'java'
}
 
 
repositories {
    jcenter()
}
 
dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

四、Ant

可以使用ApacheAnt + Ivy构建SpringBoot项目。 spring-boot-antlib“AntLib”模块也可用于帮助Ant创建可执行文件。

要声明依赖关系,典型的ivy.xml文件将如下所示:

<ivy-moduleversion="2.0">
    <infoorganisation="org.springframework.boot"module="spring-boot-sample-ant" />
    <configurations>
        <confname="compile"description="everything needed to compile this module" />
        <confname="runtime"extends="compile"description="everything needed to run this module" />
    </configurations>
    <dependencies>
        <dependencyorg="org.springframework.boot"name="spring-boot-starter"
            rev="${spring-boot.version}"conf="compile" />
    </dependencies>
</ivy-module>

 

典型的build.xml将如下所示:

<project
    xmlns:ivy="antlib:org.apache.ivy.ant"
    xmlns:spring-boot="antlib:org.springframework.boot.ant"
    name="myapp"default="build">
 
    <propertyname="spring-boot.version"value="1.5.10.RELEASE" />
 
    <targetname="resolve"description="--> retrieve dependencies with ivy">
        <ivy:retrievepattern="lib/[conf]/[artifact]-[type]-[revision].[ext]" />
    </target>
 
    <targetname="classpaths"depends="resolve">
        <pathid="compile.classpath">
            <filesetdir="lib/compile"includes="*.jar" />
        </path>
    </target>
 
    <targetname="init"depends="classpaths">
        <mkdirdir="build/classes" />
    </target>
 
    <targetname="compile"depends="init"description="compile">
        <javacsrcdir="src/main/java"destdir="build/classes"classpathref="compile.classpath" />
    </target>
 
    <targetname="build"depends="compile">
        <spring-boot:exejardestfile="build/myapp.jar"classes="build/classes">
            <spring-boot:lib>
                <filesetdir="lib/runtime" />
            </spring-boot:lib>
        </spring-boot:exejar>
    </target>
</project>

 

请参见84.10Ant构建可执行存档,而不使用spring-boot-antlib”如果不想使用spring-boot-antlib模块,请参阅操作方法

五、Starter(启动器)

启动器是一组方便的依赖关系描述符,可以包含在应用程序中。您可以获得所需的所有Spring和相关技术的一站式服务,无需通过示例代码搜索和复制粘贴依赖配置。例如,如果要开始使用SpringJPA进行数据库访问,那么只需在项目中包含spring-boot-starter-data-jpa依赖关系即可。

启动器包含许多依赖关系,包括您需要使项目快速启动并运行,并具有一致的受支持的依赖传递关系。

名字的含义(What’s in a name)

所有正式起动器都遵循类似的命名模式: spring-boot-starter- * ,其中 * 是特定类型的应用程序。这个命名结构旨在帮助你快速找到一个启动器。许多IDE中的Maven插件允许您按名称搜索依赖项。例如,安装EclipseSTSMaven插件后,您可以简单地在POM编辑器中点击 Dependency Hierarchy,并在filter输入“spring-boot-starter”来获取完整的列表。 
创建自己的启动器部分所述,第三方启动程序不应该从Spring-boot开始,因为它是为正式的SpringBoot artifacts 保留的。 acme 第三方启动器通常被命名为acme-spring-boot-starter

 

Spring Bootorg.springframework.boot组下提供了以下应用程序启动器:

13.1.Spring Boot应用程序启动器

Name

Description

Pom

spring-boot-starter

Core starter, including auto-configuration support, logging and YAML

Pom

spring-boot-starter-activemq

Starter for JMS messaging using Apache ActiveMQ

Pom

spring-boot-starter-amqp

Starter for using Spring AMQP and Rabbit MQ

Pom

spring-boot-starter-aop

Starter for aspect-oriented programming with Spring AOP and AspectJ

Pom

spring-boot-starter-artemis

Starter for JMS messaging using Apache Artemis

Pom

spring-boot-starter-batch

Starter for using Spring Batch

Pom

spring-boot-starter-cache

Starter for using Spring Framework’s caching support

Pom

spring-boot-starter-cloud-connectors

Starter for using Spring Cloud Connectors which simplifies connecting to services in cloud platforms like Cloud Foundry and Heroku

Pom

spring-boot-starter-data-cassandra

Starter for using Cassandra distributed database and Spring Data Cassandra

Pom

spring-boot-starter-data-couchbase

Starter for using Couchbase document-oriented database and Spring Data Couchbase

Pom

spring-boot-starter-data-elasticsearch

Starter for using Elasticsearch search and analytics engine and Spring Data Elasticsearch

Pom

spring-boot-starter-data-gemfire

Starter for using GemFire distributed data store and Spring Data GemFire

Pom

spring-boot-starter-data-jpa

Starter for using Spring Data JPA with Hibernate

Pom

spring-boot-starter-data-ldap

Starter for using Spring Data LDAP

Pom

spring-boot-starter-data-mongodb

Starter for using MongoDB document-oriented database and Spring Data MongoDB

Pom

spring-boot-starter-data-neo4j

Starter for using Neo4j graph database and Spring Data Neo4j

Pom

spring-boot-starter-data-redis

Starter for using Redis key-value data store with Spring Data Redis and the Jedis client

Pom

spring-boot-starter-data-rest

Starter for exposing Spring Data repositories over REST using Spring Data REST

Pom

spring-boot-starter-data-solr

Starter for using the Apache Solr search platform with Spring Data Solr

Pom

spring-boot-starter-freemarker

Starter for building MVC web applications using FreeMarker views

Pom

spring-boot-starter-groovy-templates

Starter for building MVC web applications using Groovy Templates views

Pom

spring-boot-starter-hateoas

Starter for building hypermedia-based RESTful web application with Spring MVC and Spring HATEOAS

Pom

spring-boot-starter-integration

Starter for using Spring Integration

Pom

spring-boot-starter-jdbc

Starter for using JDBC with the Tomcat JDBC connection pool

Pom

spring-boot-starter-jersey

Starter for building RESTful web applications using JAX-RS and Jersey. An alternative to spring-boot-starter-web

Pom

spring-boot-starter-jooq

Starter for using jOOQ to access SQL databases. An alternative to spring-boot-starter-data-jpa or spring-boot-starter-jdbc

Pom

spring-boot-starter-jta-atomikos

Starter for JTA transactions using Atomikos

Pom

spring-boot-starter-jta-bitronix

Starter for JTA transactions using Bitronix

Pom

spring-boot-starter-jta-narayana

Spring Boot Narayana JTA Starter

Pom

spring-boot-starter-mail

Starter for using Java Mail and Spring Framework’s email sending support

Pom

spring-boot-starter-mobile

Starter for building web applications using Spring Mobile

Pom

spring-boot-starter-mustache

Starter for building MVC web applications using Mustache views

Pom

spring-boot-starter-security

Starter for using Spring Security

Pom

spring-boot-starter-social-facebook

Starter for using Spring Social Facebook

Pom

spring-boot-starter-social-linkedin

Stater for using Spring Social LinkedIn

Pom

spring-boot-starter-social-twitter

Starter for using Spring Social Twitter

Pom

spring-boot-starter-test

Starter for testing Spring Boot applications with libraries including JUnit, Hamcrest and Mockito

Pom

spring-boot-starter-thymeleaf

Starter for building MVC web applications using Thymeleaf views

Pom

spring-boot-starter-validation

Starter for using Java Bean Validation with Hibernate Validator

Pom

spring-boot-starter-web

Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container

Pom

spring-boot-starter-web-services

Starter for using Spring Web Services

Pom

spring-boot-starter-websocket

Starter for building WebSocket applications using Spring Framework’s WebSocket support

Pom

 

13.2Spring Boot生产环境启动器

名称

描述

Pom

spring-boot-starter-actuator

使用Spring Boot Actuator提供生产准备功能,可帮助您监控和管理应用程序的启动器

Pom

spring-boot-starter-remote-shell

使用CRaSH远程shell通过SSH监视和管理您的应用程序的启动器。1.5以来已弃用

Pom

 

最后,SpringBoot还包括一些启动器,如果要排除或替换特定的技术,可以使用它们:

名称

描述

Pom

spring-boot-starter-undertow

使用Undertow作为嵌入式servlet容器的启动器。 spring-boot-starter-tomcat的替代方案

Pom

spring-boot-starter-jetty

使用Jetty作为嵌入式servlet容器的启动器。 spring-boot-starter-tomcat的替代方案

Pom

spring-boot-starter-logging

使用Logback进行日志记录的启动器。默认的日志启动器

Pom

spring-boot-starter-tomcat

使用Tomcat作为嵌入式servlet容器的启动器。 spring-boot-starter-web的默认servlet容器启动器

Pom

spring-boot-starter-log4j2

使用Log4j2进行日志记录的启动器。 spring-boot-start-logging的替代方法

Pom

 

有关社区贡献的更多启动器的列表,请参阅 GitHub 上的 spring-boot-startters 模块中的 README文件

















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值