Skip to content

Latest commit

 

History

History
363 lines (264 loc) · 29.3 KB

File metadata and controls

363 lines (264 loc) · 29.3 KB

13. Build Systems

13. 构建系统

It is strongly recommended that you choose a build system that supports dependecy management and that can consume artifacts published to the "Maven Central" repository. We would recommend that you choose Maven or Gradle. It is possible to get Spring Boot to work with other build systems (Ant, for example), but they are not particularly well supported.

它是非常推荐的你选择一个支持依赖关系管理的构建系统,并且她可以使用发布在“Maven Cetral”仓库里的组件。我们推荐你使选择Maven或者Gradle。 Spring Boot可能也可以与其他构建系统一起工作(如Ant),但是它们没有得到特别好的支持。

Dependency Management

依赖管理器

Each release of Spring Boot provides a curated list of dependencies that it supports. In practice, you do not need to provide a version for any of these dependencies in your build configuration, as Spring Boot itself, these dependencies are upgraded as well in a consistent way.

每个Spring Boot的发布版本都会提供一个支持的依赖列表。实际上,你不需要提供在构建配置文件里配置这些依赖项的版本,Spring Boot会自动帮你升级匹配适合的依赖包的版本。

Note
小提示

You can still specify a vesion and override Spring Boot's recommendations if you need to do so. 如果你需要,你仍然可以指定版本来覆盖Spring Boot的建议。

The curated list contains all the spring modules that you can use with Spring Boot as well as a refined list of third party libraries. The list is available as a standard Bills of Materials (spring-boot-dependencies) that can be used with both Maven and Gradle.

依赖支持列表包含了所有Spring的模块和Spring Boot的第三方精选类库。这个列表是一个可用的标准物料清单(spring-boot-dependencies),它可以在Maven和Gradle中使用。

Warning
警告

Each release of Spring Boot is associated with a base version of the Spring Framework. We highly recommend that you not specify its version.

任何一个Spring Boot发布版本是一个与Spring Framework基本版本对应的。我们强烈建议你在使用时不要指定它的版本。

Maven

Maven users can inherit from the spring-boot-starter-parent project to obtain sensible defaults. The parent project provides the following features:

Maven用户可以从spring-boot-starter-parent项目获取到合理的默认配置。父类工程提供了如下特性:

  • Java 1.8 as the default compiler level.
  • Java 1.8 作为默认编译级别
  • UTF-8 source encoding.
  • UTF-8源码编码
  • A Dependency Management section, inherited from the spring-boot-dependencies pom, that manages the versions of common dependencies. This dependency management lets you omit tags for those dependencies when used in your own pom.
  • 依赖管理部分,继承自spring-boot-dependenciespom,它管理公有的依赖关系版本。依赖管理器让你不用关注你自己的pom中的依赖关系。
  • Sensible plugin configuration (exec plugin, Git commit ID, and shade).
  • 合理的插件配置器(exec plugin, Git commit ID, and shade)
  • Sensible resource filtering for application.properties and application.yml including profile-specific files (for example, application-dev.properties and application-dev.yml)
  • 合理的资源过滤器application.propertiesapplication.yml包含了特定环境的配置文件(如,application-dev.properties and application-dev.yml

Note that, since the application.properties and application.yml file accept Spring style placeholders ({...}), the Maven filtering is changed to use @..@ placeholders. (You can override that by setting a Maven property called resource.delimiter.)

最后一点,application.propertiesapplication.yml文件兼容Spring占位符({...}),Maven过滤器改用了@..@ 作为占位符。(你可以通过覆盖一个叫resource.dlimiter的文件来设置Maven设置配置)。

Inheriting the Starter parent

继承父类启动器

To configure your project to inherit from the spring-boot-starter-parent, set the parent as follows:

spring-boot-starter-parent继承来配置你的项目,设置在parent节点,如下:

<!-- Inherit defaults from Spring Boot -->
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.0.1.RELEASE</version>
</parent>
Note
小提示

You should need to specify only the Spring Boot version number on this dependency. If you import additional starters, you can safey omit the version number.

你需要在依赖里来指定Spring Boot的版本号。如果你导入额外的启动器,你可以安全地忽略版本号。

With that setup, you can also override individual dependencies by overrideing a property in your own project. For instance, to upgrade to another Spring Data release train, you would add the following to your pom.xml:

有了这个设置,你也在自己的项目里设置参数来覆盖个别依赖。例如,来升级另一个Spring Data的发布版本,你可以在你的pom.xml里添加如下内容:

<properties>
	<spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
</properties>
Tip
小提示

Check the spring-boot-dependencies pom for a list of supported properties.

检查spring-boot-dependenciespom里的下列依赖支持参数设置。

Using Spring Boot without the Parent POM

使用没有父POM的Spring Boot

Not everyone likes inheriting from the spring-boot-starter-parent POM. You may have your own corporate standard parent that you need to use or you may prefer to explicitly declare all your Maven configuration.

不是每个人都喜欢从spring-boot-starter-parent POM继承。你也许需要使用你们自己公司的标准父项,或者也许明确地声明所有的Maven配置。

If you do not want to use the spring-boot-starter-parent, you can still keep the benefit of the dependency management (but not the plugin management) by using a scope=import dependency, as follows:

如果你不想使用spring-boot-starter-parent,你可以保留使用scope=import依赖保持依赖管理的好处(而不是插件管理),如下所示:

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

The preceding sample setup does not let you override individual dependencies by using a property, as explained above. To achieve the same result, you need to add an entry in the dependencyManagement of your project before the spring-boot-dependencies entry. For instance, to upgrade to another Spring Data release train, you could add the following element to your pom.xml:

如上所示,上面示例设置不允许你通过使用参数来覆盖你的单个依赖项。实现同样的结果,你只需要在你的项目里spring-boot-dependencies实例前面项目的dependencyManagement里增加一个实例。例如,升级到另一个Spring Data的发行版本,你需要添加如下节点到你的pom.xml里:

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

In the preceding example, we specify a BOM, but any dependency type can be overridden in the same way.

在前面的例子里,我们制定一个BOM,但是不是所有的依赖项目都可以用这种方式覆盖。

Using the Spring Boot Maven Plugin

使用Spring Boot Maven插件

Spring Boot include a Maven plugin that can package the project as an executable jar. Add the plugin to you <plugins> section if you want to use it, as shown in the following example:

Spring Boot包含一个Maven插件,它可以将项目打包成一个可执行的jar包。如果你想使用它,添加这个插件到你的<plugins>节点,如下样例所示:

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
</build>
Note
小提示

If you use the Spring Boot starter parent pom, you need to add only the plugin. There is no need to configure it unless you want to change the setting defined in the parent.

如果你使用了Spring Boot启动起父pom,你需要添加这个插件。除非你要改变父级中的设置,否则你不需对其进行配置。

Gradle

To learn about using Spring Boot with Gradle, please refer to the documentation for Spring Boot's Gradle plugin:

要学习有关如何在Spring Boot使用Gradle,让参考Spring Boot的Gradel插件文档相关内容:

Ant

It is possible to build a Spring Boot project using Apache Ant+Ivy. The spring-boot-antlib "AntLib" module is also available to help Ant create executable jars.

可以使用Apache Ant + Ivy来构建Spring Boot项目。spring-boot-antlib "AntLib"模块也可以用来帮你使用Ant来创建可执行jar包。

To declare dependencies, a typical ivy.xml file looks something like to following example:

声明依赖关系,一个典型的ivy.xml文件如下面的例子所示:

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

A typical build.xml looks like the following example:

一个典型的build.xml内容如下面的例子所示:

<project
	xmlns:ivy="antlib:org.apache.ivy.ant"
	xmlns:spring-boot="antlib:org.springframework.boot.ant"
	name="myapp" default="build">

	<property name="spring-boot.version" value="2.0.1.RELEASE" />

	<target name="resolve" description="--> retrieve dependencies with ivy">
		<ivy:retrieve pattern="lib/[conf]/[artifact]-[type]-[revision].[ext]" />
	</target>

	<target name="classpaths" depends="resolve">
		<path id="compile.classpath">
			<fileset dir="lib/compile" includes="*.jar" />
		</path>
	</target>

	<target name="init" depends="classpaths">
		<mkdir dir="build/classes" />
	</target>

	<target name="compile" depends="init" description="compile">
		<javac srcdir="src/main/java" destdir="build/classes" classpathref="compile.classpath" />
	</target>

	<target name="build" depends="compile">
		<spring-boot:exejar destfile="build/myapp.jar" classes="build/classes">
			<spring-boot:lib>
				<fileset dir="lib/runtime" />
			</spring-boot:lib>
		</spring-boot:exejar>
	</target>
</project>
Tip

If you do not want to use the spring-boot-antlib module, see the the section called "Build an Executable Archive from Ant without Using spring-boot-antlib" "How-to".

如果你不想使用spring-boot-antlib模块,可以查看“用Ant构建一个非使用spring-boot-antlib的可执行归档包” “How-to”相关章节。

Starter

启动器

Starters are a set of convenient dependency descriptors that you can include in your application. You get a one-stop shop for all the Spring and related technologies that you need without having to hunt through sample code an copy-paste loads of dependency descriptors. For example, if you want to get started using Spring and JPA for database access, include the spring-boot-starter-data-jpa dependency in your project.

启动器是一套方便的依赖描述符,可以包含在应用程序中。你可以获得所有Spring和相关技术的一站式服务,而不需要从搜索样例代码和拷贝粘贴依赖描述的负担。例如,如果你想开始使用Spring和数据库访问JPA,在你们的项目里包含spring-boot-starter-data-jpa依赖关系。

The starters contain a lot of the dependencies that you need to get a project up and running quickly and with a consistent, supported set of managed transitive dependencies.

启动器包含很多依赖项,你可以快速来启动并运行项目,并且拥有一组管理传递依赖关系的集合。

What's in a name
什么是名字

All official starters follow a similar naming pattern; spring-boot-starter-*, where * is a particular type of application. This naming structure is intended to help when you need to find a starter. The Maven integration in many IDEs let you search dependencies by name. For example, with the appropriate Eclipse or STS plugin installed, you can press ctrl-space in the POM editor and type "spring-boot-starter" for a complete list.

所有官方启动器都遵循类似的命名规则;spring-boot-starter-*, *部分是一个特定类型的应用程序。这部分名称结构意在帮助你在需要的是找到它。许多IDE里都内置了Maven插件来帮你根据名称搜索依赖项。例如,通过安装相应的Eclipse或STS插件,你可以在pom编辑器里按ctrl-space输入spring-boot-starter来获取完整的列表。

As explained in the "Creating Your Own Starter" section, third party starters should not start with spring-boot, as it is reserved for official Spring Boot artifacts. Rather, a thrid-party starter typically starts with the name of the project. For example, a third-party starter project called thirdpartyproject would typically be named thirdpartyproject-spring-boot-starter.

如在"创建你自己的启动器"部分所述,第三方启动器不需要使用spring-boot,它是为官方Spring Boot工作所保留的。相反,一个典型的第三方启动器通常用项目名称作为开头。例如,一个名为thirdpartyproject的第三方启动器项目可以命名为thirdpartyproject-spring-boot-starter

The following application starters are provided by Spring Boot under the org.springframework.boot group:

下面是Spring Boot下面org.springframework.boot组提供的应用程序启动器:

Table 13.1. Spring Boot application starters
Name Description Pom
spring-boot-starter Core starter, including autoconfiguration 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-cassandra-reactive Starter for using Cassandra distributed database and Spring Data Cassandra Reactive Pom
reactive Spring Data Cassandra Reactive
spring-boot-starter-data-couchbase Starter for using Couchbase document-oriented database and Spring Data Couchbase Pom
spring-boot-starter-data-couchbase-reactive Starter for using Couchbase document-oriented database and Spring Data Couchbase Reactive Pom
spring-boot-starter-data-elasticsearch Starter for using Elasticsearch search and analytics engine and Spring Data Elasticsearch 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-mongodb-reactive Starter for using MongoDB document-oriented database and Spring Data MongoDB Reactive 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 Lettuce client Pom
spring-boot-starter-data-redis-reactive Starter for using Redis key-value data store with Spring Data Redis reactive and the Lettuce 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 HikariCP 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-json Starter for reading and writing json 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 Starter for JTA transactions using Narayana Pom
spring-boot-starter-mail Starter for using Java Mail and Spring Framework’s email sending support Pom
spring-boot-starter-mustache Starter for building web applications using Mustache views Pom
spring-boot-starter-quartz Starter for using the Quartz scheduler Pom
spring-boot-starter-security Starter for using Spring Security 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-webflux Starter for building WebFlux applications using Spring Framework’s Reactive Web support Pom
spring-boot-starter-websocket Starter for building WebSocket applications using Spring Framework’s WebSocket support Pom

In addition to the application starters, the following starters can be used to add production ready features:

在附加应用程序启动器中,下列启动器可以用到生产新的功能中:

Table 13.2. Spring Boot production starters
Name Description Pom
spring-boot-starter-actuator Starter for using Spring Boot’s Actuator which provides production ready features to help you monitor and manage your application Pom

Finally, Spring Boot also includes the following starters that can be used if you want to exclude or swap specific technical facets:

最后,Spring Boot也包含了下列启动期,他们可以用在你想要排出或交换特殊的技术方面:

Table 13.3. Spring Boot technical starters
Name Description Pom
spring-boot-starter-jetty Starter for using Jetty as the embedded servlet container. An alternative to spring-boot-starter-tomcat Pom
spring-boot-starter-log4j2 Starter for using Log4j2 for logging. An alternative to spring-boot-starter-logging Pom
spring-boot-starter-logging Starter for logging using Logback. Default logging starter Pom
spring-boot-starter-reactor-netty Starter for using Reactor Netty as the embedded reactive HTTP server. Pom
spring-boot-starter-tomcat Starter for using Tomcat as the embedded servlet container. Default servlet container starter used by spring-boot-starter-web Pom
spring-boot-starter-undertow Starter for using Undertow as the embedded servlet container. An alternative to spring-boot-starter-tomcat Pom
Tip
小提示

For a list of additional community contributed starters, see the README file in the spring-boot-starters module on GitHub.

这里是社区贡献的附加启动器列表,可以在spring-boot-starters模块的Github里查看README file