18. 贡献
Spring Cloud 在非限制性 Apache 2.0 许可证下发布, 并遵循非常标准的 Github 开发流程,使用 Github tracker 和 merge pull requests 到 master 中。如果需要帮助, 即使是微不足道的事情,也请不要犹豫,但是 请遵循以下指南。
18.1. 签署贡献者许可协议
在我们接受重要的补丁或拉取请求之前,我们需要您签署贡献者许可协议。 签署贡献者协议不会授予任何人对主 仓库,但这确实意味着我们可以接受您的贡献,并且您将获得一个 如果我们这样做,请注明作者。可能会要求活跃的贡献者加入核心团队,并且 赋予合并拉取请求的能力。
18.2. 行为准则
本项目遵循 行为。参与即表示您同意遵守此准则。请举报 不可接受的行为 [email protected]。
18.3. 代码约定和内务管理
这些对于拉取请求都不是必需的,但它们都会有所帮助。他们也可以是 在原始拉取请求之后但在合并之前添加。
-
使用 Spring Framework 代码格式约定。如果您使用 Eclipse 您可以使用
eclipse-code-formatter.xml
来自 Spring 的文件 Cloud Build 项目。如果使用 IntelliJ,则可以使用 Eclipse 代码格式化程序 Plugin 导入相同的文件。 -
确保所有新的
.java
文件,以包含一个简单的 Javadoc 类注释,其中至少包含一个@author
标签标识你,最好至少有一个关于类是什么的段落 为。 -
将 ASF 许可证标头注释添加到所有新的
.java
文件(从现有文件复制 在项目中) -
将您自己添加为
@author
到您大幅修改的 .java 个文件(更多 比外观变化)。 -
添加一些 Javadocs,如果更改命名空间,还可以添加一些 XSD doc 元素。
-
一些单元测试也会有很大帮助 — 必须有人来做。
-
如果没有其他人正在使用你的分支,请针对当前的 master 进行变基(或 主项目中的 other target branch)。
-
编写提交消息时,请遵循以下约定: 如果您正在修复现有问题,请添加
Fixes gh-XXXX
在提交结束时 消息(其中 XXXX 是问题编号)。
18.4. Checkstyle
Spring Cloud Build 带有一组 checkstyle 规则。您可以在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 添加到您的项目中,只需定义以下属性和插件。
<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
与你的压制。例:
<?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
18.5. IDE 设置
18.5.1. Intellij IDEA
为了设置 Intellij,您应该导入我们的编码约定、检查配置文件并设置 checkstyle 插件。 可以在 Spring Cloud Build 项目中找到以下文件。
└── 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 的项目样式约定 |

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

转到File
→Settings
→Editor
→Inspections
.点击旁边的图标Profile
部分。在那里,单击Import Profile
并导入spring-cloud-build-tools/src/main/resources/intellij/Intellij_Project_Defaults.xml
文件。
要让 Intellij 与 Checkstyle 一起使用,您必须安装Checkstyle
插件。建议同时安装Assertions2Assertj
自动转换 JUnit 断言

转到File
→Settings
→Other settings
→Checkstyle
.然后单击+
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
).我们需要提供以下变量:
-
checkstyle.header.file
- 请将其指向 Spring Cloud Build 的spring-cloud-build-tools/src/main/resources/checkstyle-header.txt
文件,或者通过raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/spring-cloud-build-tools/src/main/resources/checkstyle-header.txt
URL 的 URL 中。 -
checkstyle.suppressions.file
- 默认抑制。请将其指向 Spring Cloud Build 的spring-cloud-build-tools/src/checkstyle/checkstyle-suppressions.xml
文件,或者通过raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/spring-cloud-build-tools/src/checkstyle/checkstyle-suppressions.xml
URL 的 URL 中。 -
checkstyle.additional.suppressions.file
- 此变量对应于本地项目中的 suppressions。例如,您正在处理spring-cloud-contract
.然后指向project-root/src/checkstyle/checkstyle-suppressions.xml
文件夹。示例spring-cloud-contract
将:/home/username/spring-cloud-contract/src/checkstyle/checkstyle-suppressions.xml
.
记得设置Scan Scope 自All sources 因为我们对生产和测试源应用 checkStyle 规则。 |
18.6. 重复查找器
Spring Cloud Build 带来了basepom:duplicate-finder-maven-plugin
,这样就可以在 Java 类路径上标记重复和冲突的类和资源。
18.6.1. 重复的 Finder 配置
重复查找器默认处于启用状态,并将在verify
阶段,但只有在添加duplicate-finder-maven-plugin
到build
部分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.skip
自true
为了跳过重复项,请检查您的构建。
如果您需要添加ignoredClassPatterns
或ignoredResourcePatterns
添加到您的设置中,请确保将它们添加到项目的 Plugin configuration 部分中:
<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>