18. 贡献

Spring Cloud 在非限制性 Apache 2.0 许可证下发布, 并遵循非常标准的 Github 开发流程,使用 Github tracker 和 merge pull requests 到 master 中。如果需要帮助, 即使是微不足道的事情,也请不要犹豫,但是 请遵循以下指南。spring-doc.cadn.net.cn

18.1. 签署贡献者许可协议

在我们接受重要的补丁或拉取请求之前,我们需要您签署贡献者许可协议。 签署贡献者协议不会授予任何人对主 仓库,但这确实意味着我们可以接受您的贡献,并且您将获得一个 如果我们这样做,请注明作者。可能会要求活跃的贡献者加入核心团队,并且 赋予合并拉取请求的能力。spring-doc.cadn.net.cn

18.2. 行为准则

本项目遵循 行为。参与即表示您同意遵守此准则。请举报 不可接受的行为 [email protected]。spring-doc.cadn.net.cn

18.3. 代码约定和内务管理

这些对于拉取请求都不是必需的,但它们都会有所帮助。他们也可以是 在原始拉取请求之后但在合并之前添加。spring-doc.cadn.net.cn

18.4. Checkstyle

Spring Cloud Build 带有一组 checkstyle 规则。您可以在spring-cloud-build-tools模块。该模块下最值得注意的文件是:spring-doc.cadn.net.cn

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

18.4.1. Checkstyle 配置

默认情况下,Checkstyle 规则处于禁用状态。要将 checkstyle 添加到您的项目中,只需定义以下属性和插件。spring-doc.cadn.net.cn

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 格式插件,该插件将重新格式化您的代码以通过大多数 Checkstyle 格式规则
5 将 checkstyle 插件添加到您的构建和报告阶段

如果你需要隐藏一些规则(例如,行长需要更长),那么你在${project.root}/src/checkstyle/checkstyle-suppressions.xml与你的压制。例:spring-doc.cadn.net.cn

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添加到您的项目中。这样,将应用一些默认格式规则。您可以通过运行以下脚本来执行此作:spring-doc.cadn.net.cn

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

18.5. IDE 设置

18.5.1. Intellij IDEA

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

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 应用大多数 Checkstyle 规则的 Intellij 的项目默认值
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文件。spring-doc.cadn.net.cn

代码样式
图 2.检查配置文件

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

Checkstyle

要让 Intellij 与 Checkstyle 一起使用,您必须安装Checkstyle插件。建议同时安装Assertions2Assertj自动转换 JUnit 断言spring-doc.cadn.net.cn

Checkstyle

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

记得设置Scan ScopeAll sources因为我们对生产和测试源应用 checkStyle 规则。