# 13\. 构建系统 强烈建议您选择一个支持[依赖管理](http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/#using-boot-dependency-management)并可以使用“Maven Central”存储库的构建系统。 我们建议您选择Maven或Gradle。 Spring Boot 可以与其他构建系统(例如 Ant )配合使用,但是它们不会得到很好的支持。 ### 13.1 依赖管理 每个版本的Spring Boot提供了一个它所支持的依赖关系列表。 实际上,您不需要为构建配置文件中的这些依赖关系提供版本,因为Spring Boot会为您进行管理这些依赖的版本。 当您升级Spring Boot本身时,这些依赖关系也将以一致的进行升级。 > 如果您觉得有必要,您仍然可以指定一个版本并覆盖Spring Boot建议的版本。 管理的列表中包含可以使用Spring Boot的所有Spring模块以及第三方库的精简列表。 该列表可作为标准的[物料(Materials)清单(spring-boot-dependencies)](http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/#using-boot-maven-without-a-parent)使用,并且还提供了对 [Maven](http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/#using-boot-maven-parent-pom) 和 [Gradle](http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/#build-tool-plugins-gradle-dependency-management) 的额外支持。 > Spring Boot的每个版本与Spring Framework的基本版本相关联,因此我们强烈建议您不要自己指定其版本。 ### [](file:///C:/Users/geekidentity/AppData/Local/Youdao/YNote/markdown/index.html#132-maven)13.2 Maven Maven用户可以从 spring-boot-starter-parent-parent 项目中继承,以获得合理的默认值。 父项目提供以下功能: * Java 1.6作为默认编译器级别。 * 源代码UTF-8编码。 * [依赖关系管理](http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/#using-boot-dependency-management),允许您省略常见依赖的<version>标签,其默认版本继承自spring-boot-dependencies POM。 * 更合理的[资源过滤](https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html)。 * 更合理的插件配置([exec plugin](http://www.mojohaus.org/exec-maven-plugin/),[surefire](https://maven.apache.org/surefire/maven-surefire-plugin/),[Git commit ID](https://github.com/ktoso/maven-git-commit-id-plugin),[shade](https://maven.apache.org/plugins/maven-shade-plugin/))。 * 针对application.properties和application.yml的更合理的资源过滤,包括特定的文件(例如application-foo.properties和application-foo.yml) * 最后一点:由于默认的配置文件接受Spring样式占位符(${...}),Maven过滤更改为使用 @..@ 占位符(您可以使用Maven属性resource.delimiter覆盖它)。 #### [](file:///C:/Users/geekidentity/AppData/Local/Youdao/YNote/markdown/index.html#1321-继承启动器parent)13.2.1 继承启动器parent 要将项目配置为继承spring-boot-starter-parent,只需设置<parent>标签如下: ``` org.springframework.boot spring-boot-starter-parent 1.5.2.RELEASE ``` > 您只需要在此依赖项上指定Spring Boot版本号。 如果您导入其他起始器,则可以放心地省略他们的版本号。 通过该设置,您还可以通过覆盖自己的项目中的属性来覆盖单个依赖。 例如,要升级到另一个 Spring Data 版本序列,您需要将以下内容添加到您的pom.xml中。 ``` Fowler-SR2 ``` > 检查 [spring-boot-dependencies pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-dependencies/pom.xml) 以获取支持的属性列表。 #### [](file:///C:/Users/geekidentity/AppData/Local/Youdao/YNote/markdown/index.html#1322-使用没有父pom的-spring-boot)13.2.2 使用没有父POM的 Spring Boot 不是每个人都喜欢从spring-boot-starter-parent POM继承。 您公司可能有自己标准的父母,或者您可能只希望明确声明所有的Maven配置。 如果您不想使用spring-boot-starter-parent,则仍然可以通过使用scope=import依赖来保持依赖管理(但不能进行插件管理)的好处: ``` org.springframework.boot spring-boot-dependencies 1.5.2.RELEASE pom import ``` 该设置不允许您使用如13.2.1 所述的属性来覆盖单个依赖关系。 要实现相同的结果,您需要在spring-boot-dependencies条目之前在项目的dependencyManagement中添加一个条目。 例如,要升级到另一个Spring Data发行版本,您需要将以下内容添加到您的pom.xml中。 ``` org.springframework.data spring-data-releasetrain Fowler-SR2 import pom org.springframework.boot spring-boot-dependencies 1.5.2.RELEASE pom import ``` > 在上面的例子中,我们指定了一个BOM,但是任何依赖关系类型都可以被这样覆盖。 #### 13.2.3 更改Java版本 spring-boot-starter-parent选择相当保守的Java兼容性版本。 如果要遵循我们的建议并使用更高版本的Java版本,可以添加java.version属性: ``` 1.8 ``` #### [](file:///C:/Users/geekidentity/AppData/Local/Youdao/YNote/markdown/index.html#1324-使用spring-boot-maven插件)13.2.4 使用Spring Boot Maven插件 Spring Boot包括一个Maven插件,可以将项目打包成可执行jar。 如果要使用它,请将插件添加到<plugins>部分: ``` org.springframework.boot spring-boot-maven-plugin ``` > 如果您使用Spring Boot启动器 parent pom,则只需要添加这个插件,除非您要更改parent中定义的设置,否则不需要进行配置。 ### 13.3 Gradle Gradle用户可以直接在其依赖关系部分导入“启动器”。 不像Maven,没有“超级父”导入来共享一些配置。 ``` repositories { jcenter() } dependencies { compile("org.springframework.boot:spring-boot-starter-web:1.5.2.RELEASE") } ``` spring-boot-gradle-plugin也是可用的,它提供了从源代码创建可执行jar并运行项目的任务。 它还提供依赖关系管理,除其他功能外,还允许您省略由Spring Boot管理的任何依赖关系的版本号: ``` plugins { id 'org.springframework.boot' version '1.5.2.RELEASE' id 'java' } repositories { jcenter() } dependencies { compile("org.springframework.boot:spring-boot-starter-web") testCompile("org.springframework.boot:spring-boot-starter-test") } ``` ### 13.4 Ant 可以使用Apache Ant + Ivy构建Spring Boot项目。 spring-boot-antlib“AntLib”模块也可用于帮助Ant创建可执行文件。 要声明依赖关系,典型的ivy.xml文件将如下所示: ``` ``` 典型的build.xml将如下所示: ``` ``` > 请参见[第84.10节“从Ant构建可执行存档,而不使用spring-boot-antlib”](http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/#howto-build-an-executable-archive-with-ant)如果不想使用spring-boot-antlib模块,请参阅“操作方法”。 ### 13.5 启动器 启动器是一组方便的依赖关系描述符,可以包含在应用程序中。 您可以获得所需的所有Spring和相关技术的一站式服务,无需通过示例代码搜索和复制粘贴依赖配置。 例如,如果要开始使用Spring和JPA进行数据库访问,那么只需在项目中包含spring-boot-starter-data-jpa依赖关系即可。 启动器包含许多依赖关系,包括您需要使项目快速启动并运行,并具有一致的受支持的依赖传递关系。 What’s in a name 所有正式起动器都遵循类似的命名模式: spring-boot-starter- * ,其中 * 是特定类型的应用程序。 这个命名结构旨在帮助你快速找到一个启动器。 许多IDE中的Maven插件允许您按名称搜索依赖项。 例如,安装Eclipse或STS的Maven插件后,您可以简单地在POM编辑器中点击 Dependency Hierarchy,并在filter输入“spring-boot-starter”来获取完整的列表。 如[创建自己的启动器](http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/#boot-features-custom-starter)部分所述,第三方启动程序不应该从Spring-boot开始,因为它是为正式的Spring Boot artifacts 保留的。 acme 的 第三方启动器通常被命名为acme-spring-boot-starter。 Spring Boot在org.springframework.boot组下提供了以下应用程序启动器: 表13.1\. Spring Boot应用程序启动器 | 名称 | 描述 | Pom | | --- | --- | --- | | spring-boot-starter-thymeleaf | 使用Thymeleaf视图构建MVC Web应用程序的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-thymeleaf/pom.xml) | | spring-boot-starter-data-couchbase | 使用Couchbase面向文档的数据库和Spring Data Couchbase的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-data-couchbase/pom.xml) | | spring-boot-starter-artemis | 使用Apache Artemis的JMS启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-artemis/pom.xml) | | spring-boot-starter-web-services | Spring Web Services 启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-web-services/pom.xml) | | spring-boot-starter-mail | Java Mail和Spring Framework的电子邮件发送支持的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-mail/pom.xml) | | spring-boot-starter-data-redis | Redis key-value 数据存储与Spring Data Redis和Jedis客户端启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-data-redis/pom.xml) | | spring-boot-starter-web | 使用Spring MVC构建Web,包括RESTful应用程序。使用Tomcat作为默认的嵌入式容器的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-web/pom.xml) | | spring-boot-starter-data-gemfire | 使用GemFire分布式数据存储和Spring Data GemFire的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-data-gemfire/pom.xml) | | spring-boot-starter-activemq | 使用Apache ActiveMQ的JMS启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-activemq/pom.xml) | | spring-boot-starter-data-elasticsearch | 使用Elasticsearch搜索和分析引擎和Spring Data Elasticsearch的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-data-elasticsearch/pom.xml) | | spring-boot-starter-integration | Spring Integration 启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-integration/pom.xml) | | spring-boot-starter-test | 使用JUnit,Hamcrest和Mockito的库测试Spring Boot应用程序的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-test/pom.xml) | | spring-boot-starter-jdbc | 使用JDBC与Tomcat JDBC连接池的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-jdbc/pom.xml) | | spring-boot-starter-mobile | 使用Spring Mobile构建Web应用程序的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-mobile/pom.xml) | | spring-boot-starter-validation | 使用Java Bean Validation 与Hibernate Validator的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-validation/pom.xml) | | spring-boot-starter-hateoas | 使用Spring MVC和Spring HATEOAS构建基于超媒体的RESTful Web应用程序的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-hateoas/pom.xml) | | spring-boot-starter-jersey | 使用JAX-RS和Jersey构建RESTful Web应用程序的启动器。spring-boot-starter-web的替代方案 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-jersey/pom.xml) | | spring-boot-starter-data-neo4j | 使用Neo4j图数据库和Spring Data Neo4j的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-data-neo4j/pom.xml) | | spring-boot-starter-data-ldap | 使用Spring Data LDAP的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-data-ldap/pom.xml) | | spring-boot-starter-websocket | 使用Spring Framework的WebSocket支持构建WebSocket应用程序的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-websocket/pom.xml) | | spring-boot-starter-aop | 使用Spring AOP和AspectJ进行面向切面编程的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-aop/pom.xml) | | spring-boot-starter-amqp | 使用Spring AMQP和Rabbit MQ的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-amqp/pom.xml) | | spring-boot-starter-data-cassandra | 使用Cassandra分布式数据库和Spring Data Cassandra的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-data-cassandra/pom.xml) | | spring-boot-starter-social-facebook | 使用Spring Social Facebook 的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-social-facebook/pom.xml) | | spring-boot-starter-jta-atomikos | 使用Atomikos的JTA事务的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-jta-atomikos/pom.xml) | | spring-boot-starter-security | 使用Spring Security的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-security/pom.xml) | | spring-boot-starter-mustache | 使用Mustache视图构建MVC Web应用程序的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-mustache/pom.xml) | | spring-boot-starter-data-jpa | 使用Spring数据JPA与Hibernate的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-data-jpa/pom.xml) | | spring-boot-starter | 核心启动器,包括自动配置支持,日志记录和YAML | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter/pom.xml) | | spring-boot-starter-groovy-templates | 使用Groovy模板视图构建MVC Web应用程序的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-groovy-templates/pom.xml) | | spring-boot-starter-freemarker | 使用FreeMarker视图构建MVC Web应用程序的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-freemarker/pom.xml) | | spring-boot-starter-batch | 使用Spring Batch的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-batch/pom.xml) | | spring-boot-starter-social-linkedin | 使用Spring Social LinkedIn的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-social-linkedin/pom.xml) | | spring-boot-starter-cache | 使用Spring Framework缓存支持的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-cache/pom.xml) | | spring-boot-starter-data-solr | 使用Apache Solr搜索平台与Spring Data Solr的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-data-solr/pom.xml) | | spring-boot-starter-data-mongodb | 使用MongoDB面向文档的数据库和Spring Data MongoDB的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-data-mongodb/pom.xml) | | spring-boot-starter-jooq | 使用jOOQ访问SQL数据库的启动器。 spring-boot-starter-data-jpa或spring-boot-starter-jdbc的替代方案 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-jooq/pom.xml) | | spring-boot-starter-jta-narayana | Spring Boot Narayana JTA 启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-jta-narayana/pom.xml) | | spring-boot-starter-cloud-connectors | 使用Spring Cloud连接器,简化了与Cloud Foundry和Heroku等云平台中的服务连接的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-cloud-connectors/pom.xml) | | spring-boot-starter-jta-bitronix | 使用Bitronix进行JTA 事务的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-jta-bitronix/pom.xml) | | spring-boot-starter-social-twitter | 使用Spring Social Twitter的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-social-twitter/pom.xml) | | spring-boot-starter-data-rest | 通过使用Spring Data REST在REST上暴露Spring数据库的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-data-rest/pom.xml) | 除了应用程序启动器,以下启动器可用于添加[生产准备(production ready)](http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/#production-ready)功能: 表13.2 Spring Boot生产环境启动器 | 名称 | 描述 | Pom | | --- | --- | --- | | spring-boot-starter-actuator | 使用Spring Boot Actuator提供生产准备功能,可帮助您监控和管理应用程序的启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-actuator/pom.xml) | | spring-boot-starter-remote-shell | 使用CRaSH远程shell通过SSH监视和管理您的应用程序的启动器。 自1.5以来已弃用 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-remote-shell/pom.xml) | 最后,Spring Boot还包括一些启动器,如果要排除或替换特定的技术,可以使用它们: | 名称 | 描述 | Pom | | --- | --- | --- | | spring-boot-starter-undertow | 使用Undertow作为嵌入式servlet容器的启动器。 spring-boot-starter-tomcat的替代方案 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-undertow/pom.xml) | | spring-boot-starter-jetty | 使用Jetty作为嵌入式servlet容器的启动器。 spring-boot-starter-tomcat的替代方案 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-jetty/pom.xml) | | spring-boot-starter-logging | 使用Logback进行日志记录的启动器。 默认的日志启动器 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-logging/pom.xml) | | spring-boot-starter-tomcat | 使用Tomcat作为嵌入式servlet容器的启动器。 spring-boot-starter-web的默认servlet容器启动器 | [Pom](http://note.youdao.com/) | | spring-boot-starter-log4j2 | 使用Log4j2进行日志记录的启动器。 spring-boot-start-logging的替代方法 | [Pom](https://github.com/spring-projects/spring-boot/tree/v1.5.2.RELEASE/spring-boot-starters/spring-boot-starter-log4j2/pom.xml) | > 有关社区贡献的更多启动器的列表,请参阅GitHub上的spring-boot-startters模块中的[README文件](https://github.com/spring-projects/spring-boot/tree/master/spring-boot-starters/README.adoc)。