貢獻

Spring Cloud 採用非限制性的 Apache 2.0 許可釋出,並遵循非常標準的 Github 開發流程,使用 Github 跟蹤器處理問題並將拉取請求合併到 main 分支。即使您想貢獻一些微不足道的東西,也請不要猶豫,但請遵循以下準則。

開發者原產地證書 (DCO)

所有提交必須在每個提交訊息的末尾包含一個 Signed-off-by 尾註,以表明貢獻者同意開發者原產地證書。有關更多詳細資訊,請參閱部落格文章 Hello DCO, Goodbye CLA: 簡化對 Spring 的貢獻

行為準則

本專案遵循貢獻者盟約 行為準則。透過參與,您需要遵守此準則。請將不可接受的行為報告給 [email protected]

程式碼約定和內務管理

這些對於拉取請求都不是必不可少的,但它們都會有所幫助。它們也可以在原始拉取請求之後但在合併之前新增。

  • 使用 Spring Framework 程式碼格式約定。如果您使用 Eclipse,可以使用來自 Spring Cloud Build 專案的 eclipse-code-formatter.xml 檔案匯入格式化程式設定。如果使用 IntelliJ,可以使用 Eclipse Code Formatter Plugin 匯入相同的檔案。

  • 確保所有新的 .java 檔案都包含一個簡單的 Javadoc 類註釋,至少包含一個標識您的 @author 標籤,並且最好至少有一個段落說明該類的用途。

  • 將 ASF 許可頭註釋新增到所有新的 .java 檔案中(從專案中的現有檔案複製)

  • 如果您對 .java 檔案進行了大量修改(不僅僅是表面上的更改),請將自己新增為 @author

  • 新增一些 Javadoc,如果您更改了名稱空間,還要新增一些 XSD 文件元素。

  • 一些單元測試也會有很大幫助——總得有人去做。

  • 如果沒有其他人使用您的分支,請將其重新基於當前的 main(或主專案中的其他目標分支)。

  • 編寫提交訊息時請遵循 這些約定,如果您正在修復現有問題,請在提交訊息末尾新增 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 預設 Checkstyle 規則
2 檔案頭設定
3 預設抑制規則

Checkstyle 配置

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 設定

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 適用於 IntelliJ 的專案樣式約定,應用大多數 Checkstyle 規則
Code style
圖 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 檔案。

Code style
圖 2. 檢查配置檔案

轉到 FileSettingsEditorInspections。在那裡點選 Profile 部分旁邊的圖示。然後,點選 Import Profile 並匯入 spring-cloud-build-tools/src/main/resources/intellij/Intellij_Project_Defaults.xml 檔案。

Checkstyle

為了使 IntelliJ 與 Checkstyle 配合使用,您必須安裝 Checkstyle 外掛。建議同時安裝 Assertions2Assertj 以自動轉換 JUnit 斷言。

Checkstyle

轉到 FileSettingsOther settingsCheckstyle。在那裡點選 Configuration file 部分中的 + 圖示。在那裡,您必須定義 Checkstyle 規則應該從哪裡獲取。在上面的影像中,我們從克隆的 Spring Cloud Build 倉庫中選擇了規則。但是,您可以指向 Spring Cloud Build 的 GitHub 倉庫(例如對於 checkstyle.xmlraw.githubusercontent.com/spring-cloud/spring-cloud-build/main/spring-cloud-build-tools/src/main/resources/checkstyle.xml)。我們需要提供以下變數

請記住將 Scan Scope 設定為 All sources,因為我們對生產和測試源應用 Checkstyle 規則。

重複查詢器

Spring Cloud Build 帶來了 basepom:duplicate-finder-maven-plugin,它能夠在 Java classpath 上標記重複和衝突的類和資源。

重複查詢器配置

重複查詢器預設啟用,並將在 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>

對於其他屬性,我們已設定了 外掛文件 中列出的預設值。

您可以透過設定所選屬性的值並加上 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>
© . This site is unofficial and not affiliated with VMware.