建造

Spring Cloud Build 是 Spring Cloud 的一个通用实用程序项目,用于插件和依赖项管理。

构建和部署

本地安装:

$ mvn install -s .settings.xml

并将快照部署到 repo.spring.io:

$ mvn deploy -DaltSnapshotDeploymentRepository=repo.spring.io::default::https://repo.spring.io/snapshot

用于 RELEASE 构建使用

$ mvn deploy -DaltReleaseDeploymentRepository=repo.spring.io::default::https://repo.spring.io/release

并供 jcenter 使用

$ mvn deploy -DaltReleaseDeploymentRepository=bintray::default::https://api.bintray.com/maven/spring/jars/org.springframework.cloud:build

并供 Maven Central 使用

$ mvn deploy -P central -DaltReleaseDeploymentRepository=sonatype-nexus-staging::default::https://oss.sonatype.org/service/local/staging/deploy/maven2

(“central”配置文件适用于 Spring Cloud 中的所有项目,它设置 gpg jar 签名,并且必须为此项目单独指定存储库,因为它是启动器父级的父级,而用户又将其作为其父级自己的父母)。

贡献

Spring Cloud 在非限制性 Apache 2.0 许可证下发布,并遵循非常标准的 Github 开发流程,使用 Github 跟踪器处理问题并将拉取请求合并到 master 中。如果您想贡献一些微不足道的东西,请不要犹豫,但请遵循以下准则。

签署贡献者许可协议

在我们接受重要的补丁或拉取请求之前,我们需要您签署 贡献者许可协议。签署贡献者协议并不授予任何人对主存储库的提交权,但这确实意味着我们可以接受您的贡献,如果我们这样做,您将获得作者信用。活跃的贡献者可能会被要求加入核心团队,并被赋予合并拉取请求的能力。

行为守则

该项目遵守贡献者契约行为准则。通过参与,您应该遵守此准则。请向spring-code-of-conduct@pivotal.io报告不可接受的行为。

代码约定和内务管理

这些对于拉取请求来说都不是必需的,但它们都会有所帮助。它们也可以在原始拉取请求之后、合并之前添加。

  • 使用 Spring 框架代码格式约定。如果您使用 Eclipse,则可以使用 Spring Cloud Buildeclipse-code-formatter.xml项目中的文件 导入格式化程序设置。如果使用 IntelliJ,您可以使用 Eclipse Code Formatter 插件导入相同的文件。

  • 确保所有新.java文件都有一个简单的 Javadoc 类注释,其中至少有一个 @author标识您的标签,最好至少有一段说明该类的用途。

  • 将 ASF 许可证头注释添加到所有新.java文件(从项目中的现有文件复制)

  • 将您自己添加到@author您进行大量修改(不仅仅是外观更改)的 .java 文件中。

  • 添加一些 Javadocs,如果更改命名空间,还添加一些 XSD 文档元素。

  • 一些单元测试也会有很大帮助——必须有人来做。

  • 如果没有其他人在使用您的分支,请根据当前主分支(或主项目中的其他目标分支)重新调整其基础。

  • 编写提交消息时请遵循以下约定,如果您要修复现有问题,请Fixes gh-XXXX在提交消息末尾添加(其中 XXXX 是问题编号)。

格子风格

Spring Cloud Build 附带了一组 checkstyle 规则。您可以在模块中找到它们spring-cloud-build-tools。该模块下最值得注意的文件是:

spring-cloud-build-tools/
└── src
    ├── checkstyle
    │   └── checkstyle-suppressions.xml (3)
    └── main
        └── resources
            ├── checkstyle-header.txt (2)
            └── checkstyle.xml (1)
1 默认 Checkstyle 规则
2 文件头设置
3 默认抑制规则

检查样式配置

默认情况下禁用Checkstyle 规则。要将 checkstyle 添加到您的项目中,只需定义以下属性和插件。

pom.xml
<properties>
<maven-checkstyle-plugin.failsOnError>true</maven-checkstyle-plugin.failsOnError> (1)
        <maven-checkstyle-plugin.failsOnViolation>true
        </maven-checkstyle-plugin.failsOnViolation> (2)
        <maven-checkstyle-plugin.includeTestSourceDirectory>true
        </maven-checkstyle-plugin.includeTestSourceDirectory> (3)
</properties>

<build>
        <plugins>
            <plugin> (4)
                <groupId>io.spring.javaformat</groupId>
                <artifactId>spring-javaformat-maven-plugin</artifactId>
            </plugin>
            <plugin> (5)
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
            </plugin>
        </plugins>

    <reporting>
        <plugins>
            <plugin> (5)
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
            </plugin>
        </plugins>
    </reporting>
</build>
1 由于 Checkstyle 错误而导致构建失败
2 因 Checkstyle 违规而导致构建失败
3 Checkstyle 还分析测试源
4 添加 Spring Java Format 插件,该插件将重新格式化您的代码以通过大多数 Checkstyle 格式化规则
5 将 checkstyle 插件添加到您的构建和报告阶段

如果您需要抑制某些规则(例如,行长度需要更长),那么您只需在${project.root}/src/checkstyle/checkstyle-suppressions.xml抑制下定义一个文件就足够了。例子:

项目根目录/src/checkstyle/checkstyle-suppresions.xml
<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
		"-//Puppy Crawl//DTD Suppressions 1.1//EN"
		"https://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
<suppressions>
	<suppress files=".*ConfigServerApplication\.java" checks="HideUtilityClassConstructor"/>
	<suppress files=".*ConfigClientWatch\.java" checks="LineLengthCheck"/>
</suppressions>

建议将${spring-cloud-build.rootFolder}/.editorconfig和复制${spring-cloud-build.rootFolder}/.springformat到您的项目中。这样,将应用一些默认的格式规则。您可以通过运行以下脚本来执行此操作:

$ curl https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/.editorconfig -o .editorconfig
$ touch .springformat

IDE设置

Intellij IDEA

为了设置 Intellij,您应该导入我们的编码约定、检查配置文件并设置 checkstyle 插件。在Spring Cloud Build项目中可以找到以下文件。

spring-cloud-build-tools/
└── src
    ├── checkstyle
    │   └── checkstyle-suppressions.xml (3)
    └── main
        └── resources
            ├── checkstyle-header.txt (2)
            ├── checkstyle.xml (1)
            └── intellij
                ├── Intellij_Project_Defaults.xml (4)
                └── Intellij_Spring_Boot_Java_Conventions.xml (5)
1 默认 Checkstyle 规则
2 文件头设置
3 默认抑制规则
4 Intellij 的项目默认应用了大多数 Checkstyle 规则
5 应用大多数 Checkstyle 规则的 Intellij 项目样式约定
代码风格
图 1. 代码风格

转到FileSettingsEditorCode style。单击该Scheme部分旁边的图标。在那里,单击该Import Scheme值并选择该Intellij IDEA code style XML选项。导入spring-cloud-build-tools/src/main/resources/intellij/Intellij_Spring_Boot_Java_Conventions.xml文件。

代码风格
图 2. 检查概况

转到FileSettingsEditorInspections。单击该Profile部分旁边的图标。在那里,单击Import Profile并导入spring-cloud-build-tools/src/main/resources/intellij/Intellij_Project_Defaults.xml文件。

格子风格

要让 Intellij 与 Checkstyle 配合使用,您必须安装该Checkstyle插件。建议还安装Assertions2Assertj自动转换 JUnit 断言

格子风格

转到FileSettingsOther settingsCheckstyle。单击+Configuration file部分中的图标。在那里,您必须定义应从何处选择检查样式规则。在上图中,我们从克隆的 Spring Cloud Build 存储库中选择了规则。但是,您可以指向 Spring Cloud Build 的 GitHub 存储库(例如checkstyle.xml: https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/spring-cloud-build-tools/src/main/resources/checkstyle.xml)。我们需要提供以下变量:

请记住设置为,Scan Scope因为All sources我们对生产和测试源应用 checkstyle 规则。

重复查找器

Spring Cloud Build 带来了 basepom:duplicate-finder-maven-plugin, ,可以在 java 类路径上标记重复和冲突的类和资源。

重复查找器配置

重复查找器默认启用,并将在verify您的 Maven 构建阶段运行,但只有将 添加到duplicate-finder-maven-plugin项目buildpom.xml.

pom.xml
<build>
    <plugins>
        <plugin>
            <groupId>org.basepom.maven</groupId>
            <artifactId>duplicate-finder-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

对于其他属性,我们设置了插件文档中列出的默认值。

您可以轻松地覆盖它们,但设置以 为前缀的所选属性的值duplicate-finder-maven-plugin。例如,设置duplicate-finder-maven-plugin.skiptrue以便在构建中跳过重复检查。

如果您需要添加ignoredClassPatternsignoredResourcePatterns到您的设置中,请确保将它们添加到项目的插件配置部分:

<build>
    <plugins>
        <plugin>
            <groupId>org.basepom.maven</groupId>
            <artifactId>duplicate-finder-maven-plugin</artifactId>
            <configuration>
                <ignoredClassPatterns>
				    <ignoredClassPattern>org.joda.time.base.BaseDateTime</ignoredClassPattern>
					<ignoredClassPattern>.*module-info</ignoredClassPattern>
				</ignoredClassPatterns>
                <ignoredResourcePatterns>
                    <ignoredResourcePattern>changelog.txt</ignoredResourcePattern>
                </ignoredResourcePatterns>
            </configuration>
        </plugin>
    </plugins>
</build>

展平 POM

为了避免传播构建 Spring Cloud 项目所需的构建设置,我们使用 maven flatten 插件。它的优点是可以让您在将“干净”的 pom 发布到存储库时使用所需的任何功能。

为了添加它,请将 添加org.codehaus.mojo:flatten-maven-plugin到您的pom.xml.

<build>
	<plugins>
		<plugin>
			<groupId>org.codehaus.mojo</groupId>
			<artifactId>flatten-maven-plugin</artifactId>
		</plugin>
	</plugins>
</build>

重用文档

Spring Cloud Build 发布了其spring-cloud-build-docs模块,其中包含有用的脚本(例如 README 生成 ruby​​ 脚本)以及 Spring Cloud 文档的 css、xslt 和图像。如果您想遵循生成文档的相同约定方法,只需将这些插件添加到您的docs模块中

<properties>
	<upload-docs-zip.phase>deploy</upload-docs-zip.phase> (8)
</properties>
<profiles>
	<profile>
		<id>docs</id>
		<build>
			<plugins>
				<plugin>
					<groupId>pl.project13.maven</groupId>
					<artifactId>git-commit-id-plugin</artifactId> (1)
				</plugin>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-dependency-plugin</artifactId> (2)
				</plugin>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-resources-plugin</artifactId> (3)
				</plugin>
				<plugin>
					<groupId>org.codehaus.mojo</groupId>
					<artifactId>exec-maven-plugin</artifactId> (4)
				</plugin>
				<plugin>
					<groupId>org.asciidoctor</groupId>
					<artifactId>asciidoctor-maven-plugin</artifactId> (5)
				</plugin>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-antrun-plugin</artifactId> (6)
				</plugin>
				<plugin>
					<artifactId>maven-deploy-plugin</artifactId> (7)
				</plugin>
			</plugins>
		</build>
	</profile>
</profiles>
1 该插件下载设置项目的所有git信息
2 spring-cloud-build-docs该插件下载模块的资源
3 spring-cloud-build-docs该插件解压模块的资源
4 该插件生成一个adoc包含类路径中所有配置属性的文件
5 解析 Asciidoctor 文档需要这个插件
6 需要此插件将资源复制到正确的最终目的地并生成主 README.adoc 并断言没有文件使用未解析的链接
7 该插件确保生成的 zip 文档将被发布
8 此属性打开 <7> 的“部署”阶段
插件声明的顺序很重要!

为了使构建生成adoc包含所有配置属性的文件,您的docs模块应包含类路径上的所有依赖项,您希望扫描这些依赖项以查找配置属性。该文件将输出到${docsModule}/src/main/asciidoc/_configprops.adoc文件(可通过configprops.path属性配置)。

如果您想修改表中放入的配置属性,您可以调整模式configprops.inclusionPattern以仅包含属性的子集(例如<configprops.inclusionPattern>spring.sleuth.*</configprops.inclusionPattern>)。

Spring Cloud Build Docs 附带了一组可以重用的 asciidoctor 属性。

<attributes>
	<docinfo>shared</docinfo>
	<allow-uri-read>true</allow-uri-read>
	<nofooter/>
	<toc>left</toc>
	<toc-levels>4</toc-levels>
	<sectlinks>true</sectlinks>
	<sources-root>${project.basedir}/src@</sources-root>
	<asciidoc-sources-root>${project.basedir}/src/main/asciidoc@</asciidoc-sources-root>
	<generated-resources-root>${project.basedir}/target/generated-resources@
	</generated-resources-root>
	<!-- Use this attribute the reference code from another module -->
	<!-- Note the @ at the end, lowering the precedence of the attribute -->
	<project-root>${maven.multiModuleProjectDirectory}@</project-root>
	<!-- It's mandatory for you to pass the docs.main property -->
	<github-repo>${docs.main}@</github-repo>
	<github-project>https://github.com/spring-cloud/${docs.main}@</github-project>
	<github-raw>
		https://raw.githubusercontent.com/spring-cloud/${docs.main}/${github-tag}@
	</github-raw>
	<github-code>https://github.com/spring-cloud/${docs.main}/tree/${github-tag}@
	</github-code>
	<github-issues>https://github.com/spring-cloud/${docs.main}/issues/@</github-issues>
	<github-wiki>https://github.com/spring-cloud/${docs.main}/wiki@</github-wiki>
	<github-master-code>https://github.com/spring-cloud/${docs.main}/tree/master@
	</github-master-code>
	<index-link>${index-link}@</index-link>

	<!-- Spring Cloud specific -->
	<!-- for backward compatibility -->
	<spring-cloud-version>${project.version}@</spring-cloud-version>
	<project-version>${project.version}@</project-version>
	<github-tag>${github-tag}@</github-tag>
	<version-type>${version-type}@</version-type>
	<docs-url>/${docs.main}/docs/${project.version}@</docs-url>
	<raw-docs-url>${github-raw}@</raw-docs-url>
	<project-version>${project.version}@</project-version>
	<project-name>${docs.main}@</project-name>
	<source-highlighter>highlight.js</source-highlighter>
</attributes>

更新指南

我们假设您的项目在该guides文件夹下包含指南。

.
└── guides
	├── gs-guide1
	├── gs-guide2
	└── gs-guide3

这意味着该项目包含 3 个指南,与 Spring Guides 组织中的以下指南相对应。

-Pguides如果您使用这样的配置文件部署项目

$ ./mvnw clean deploy -Pguides

对于 GA 项目版本,我们将克隆并gs-guide1使用您项目下的内容更新其内容。gs-guide2gs-guide3guides

您可以通过不添加guides配置文件或-DskipGuides在配置文件打开时传递系统属性来跳过此步骤。

guides-project.version您可以通过(默认为)配置传递给指南的项目版本${project.version}。指南更新的阶段可以通过配置guides-update.phase(默认为deploy)。