Contributing

Sign the Contributor License Agreement

Spring Cloud 在非限制性 Apache 2.0 许可下发布,遵循非常标准的 Github 开发流程,使用 Github tracker 处理问题并将 pull request 合并到 main。如果您想贡献一些事情,即使是小事,请不要犹豫,但是要遵循以下准则。

Sign the Contributor License Agreement

在接受非同小可的补丁程序或 pull request 之前,我们需要您签署贡献者许可协议。签署贡献者协议并不意味着任何人都有权向 main 存储库提交,但表示我们可以接受您的贡献,如果您这样做,您将获得作者的荣誉。可能会要求积极的贡献者加入核心团队,并获得合并 pull request 的能力。

Code of Conduct

本项目遵守贡献者守则。参与本项目,即表示您应该遵守该守则。请向 spring-code-of-conduct@pivotal.io 报告不可接受的行为。

Code Conventions and Housekeeping

对于 pull request 来说,这些都不是必需的,但它们都将提供帮助。它们还可以在原始 pull request 之后但在合并之前添加。

  • 使用 Spring 框架代码格式约定。如果你使用 Eclipse,你可以使用 @ {s0} 文件从 @ {s1} 项目导入格式化设定。如果你使用 IntelliJ,你可以使用 @ {s2} 导入相同的文件。

  • 确保所有新的 @ {s3} 文件都带有简单的 Javadoc 类注释,至少有一个 @ {s4} 标记标识你,最好至少有一个段落说明类是什么。

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

  • 将自己作为 @author 添加到您大幅修改(不仅仅是表面更改)的 .java 文件中。

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

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

  • 如果没有其他人使用您的分支,请将其重新定基到当前主分支(或主项目中的其他目标分支)。

  • 编写提交消息时,请按照 these conventions 进行操作,如果您正在修复现有问题,请在提交消息末尾附加 Fixes gh-XXXX(其中 XXXX 是问题编号)。

Checkstyle

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 Default Checkstyle rules
2 File header setup
3 Default suppression rules

Checkstyle configuration

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 文件即可。例如:

projectRoot/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/main/.editorconfig -o .editorconfig
$ touch .springformat

IDE setup

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 Default Checkstyle rules
2 File header setup
3 Default suppression rules
4 Intellij 的项目默认设置,它应用了大部分 Checkstyle 规则
5 Intellij 的项目样式约定,它应用了大部分 Checkstyle 规则
intellij code style
Figure 1. Code style

转到 文件设置编辑器代码样式。在那里单击 方案 部分旁边的图标。在那里,单击 导入方案 值并选择 Intellij IDEA 代码样式 XML 选项。导入 spring-cloud-build-tools/src/main/resources/intellij/Intellij_Spring_Boot_Java_Conventions.xml 文件。

intellij inspections
Figure 2. Inspection profiles

转到 文件设置编辑器检查。在那里单击 配置文件 部分旁边的图标。在那里,单击 导入配置文件 并导入 spring-cloud-build-tools/src/main/resources/intellij/Intellij_Project_Defaults.xml 文件。

Checkstyle

为了让 Intellij 与 Checkstyle 协作,你必须安装 Checkstyle 插件。建议也安装 Assertions2Assertj 来自动转换 JUnit 断言

intellij checkstyle

转到 文件设置其他设置Checkstyle。在那里单击 配置文件 部分中的 + 图标。在那里,你必须定义从中选取 checkstyle 规则的位置。在上图中,我们从克隆的 Spring Cloud Build 仓库中选择了规则。然而,你可以指向 Spring Cloud Build 的 GitHub 仓库(例如,对于 checkstyle.xmlhttps://raw.githubusercontent.com/spring-cloud/spring-cloud-build/main/spring-cloud-build-tools/src/main/resources/checkstyle.xml)。我们需要提供以下变量:

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

Duplicate Finder

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

Duplicate Finder configuration

重复项查找器 默认启用,并将在 Maven 构建的 verify 阶段运行,但它仅在你将 duplicate-finder-maven-plugin 添加到项目的 pom.xmlbuild 部分时才在你的项目中生效。

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

对于其他属性,我们已设置 plugin documentation中列出的默认值。

你可以通过设置以 duplicate-finder-maven-plugin 为前缀的选定属性值轻松覆盖它们。例如,将 duplicate-finder-maven-plugin.skip 设置为 true 以在你的构建中跳过重复检查。

如果你需要将 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>