Skip to content

Commit 0678d80

Browse files
committed
ICU-23091 Add Java source formatting configs and automated checks
1 parent 18be9a0 commit 0678d80

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

.github/workflows/icu4j.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,33 @@ jobs:
6464
cd icu4j;
6565
mvn ${SHARED_MVN_ARGS} dependency:go-offline -P '!old_jdk_taglet'
6666
67+
# Using the Java style formatter google-java-style provided by the Spotless
68+
# plugin configured in the root pom.xml using 4-space indents (AOSP style).
69+
# Spotless is configured to run only on files in this branch (PR) that differ
70+
# from origin/main
71+
formatter:
72+
name: Formatter + Style checker
73+
needs: icu4j-mvn-init-cache
74+
runs-on: ubuntu-latest
75+
steps:
76+
- name: Checkout and setup
77+
uses: actions/checkout@v4
78+
with:
79+
fetch-depth: 0 # fetch all branches so that Spotless can resolve `origin/main`
80+
- name: Restore read-only cache of local Maven repository
81+
uses: actions/cache/restore@v4
82+
id: cache
83+
with:
84+
path: ~/.m2/repository
85+
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
86+
restore-keys: |
87+
${{ runner.os }}-maven-
88+
lookup-only: true
89+
- name: Check Java style
90+
run: |
91+
cd ic4j;
92+
mvn spotless:check || (echo "Style checker failed. Formatting changes can be applied by 'mvn spotless:apply'" && exit 1)
93+
6794
# ICU4J build and unit test using Maven
6895
# https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven
6996
icu4j-mvn-build-and-test:

docs/userguide/dev/codingguidelines.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,20 @@ ICU Java classes and methods.
14491449

14501450
### Code style
14511451

1452+
ICU uses a source formatter to ensure a consistent code style automatically,
1453+
and it uses a single common formatter to avoid spurious diff noise in code reviews.
1454+
This is now enforced via a
1455+
[formatter](https://github.com/google/google-java-format)
1456+
that is configured in the Maven build
1457+
via a [Maven plugin](https://github.com/diffplug/spotless/tree/main/plugin-maven)
1458+
and checked by continuous integration on pull requests.
1459+
1460+
When creating pull requests, you can check the formatting locally using the command `mvn spotless:check`. You can apply the formatter's changes using the command `mvn spotless:apply`. Continuous integration errors for formatting can be fixed by committing the changes resulting from applying the formatter locally and pushing the new commit.
1461+
1462+
The following are further guidelines for code style,
1463+
but they will be superceded by the automated formatter in CI that is mentioned above
1464+
if the guidelines differ in style.
1465+
14521466
The standard order for modifier keywords on APIs is:
14531467

14541468
* `public static final synchronized strictfp`

icu4j/pom.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@
116116
<mf.Manifest-Version>1.0</mf.Manifest-Version>
117117
<mf.Bundle-ManifestVersion>2</mf.Bundle-ManifestVersion>
118118
<mf.Bundle-Copyright>© 2016 and later: Unicode, Inc. and others. License &amp; terms of use: http://www.unicode.org/copyright.html</mf.Bundle-Copyright>
119+
120+
<formatter.plugin.spotless.version>2.43.0</formatter.plugin.spotless.version>
121+
<formatter.google-java-style.version>1.22.0</formatter.google-java-style.version>
119122
</properties>
120123

121124
<licenses>
@@ -466,6 +469,33 @@
466469
<artifactId>exec-maven-plugin</artifactId>
467470
<version>3.4.1</version>
468471
</plugin>
472+
<!--
473+
Spotless info:
474+
- How to preview what `mvn spotless:apply` will do: https://github.com/diffplug/spotless/tree/main/plugin-maven#how-do-i-preview-what-mvn-spotlessapply-will-do
475+
- Apply Spotless to specific files: https://github.com/diffplug/spotless/tree/main/plugin-maven#can-i-apply-spotless-to-specific-files
476+
-->
477+
<plugin>
478+
<groupId>com.diffplug.spotless</groupId>
479+
<artifactId>spotless-maven-plugin</artifactId>
480+
<version>${formatter.plugin.spotless.version}</version>
481+
<configuration>
482+
<!-- optional: limit format enforcement to just the files changed by this feature branch -->
483+
<!-- You can explicitly disable ratchet functionality by providing the value 'NONE': -->
484+
<ratchetFrom>NONE</ratchetFrom>
485+
<!-- define a language-specific format -->
486+
<java>
487+
<toggleOffOn />
488+
<!-- no need to specify files, inferred automatically, but you can if you want -->
489+
<!-- apply a specific flavor of google-java-format and reflow long strings -->
490+
<googleJavaFormat>
491+
<!-- version of google-java-style -->
492+
<version>${google-java-style.version}</version>
493+
<style>AOSP</style>
494+
<reflowLongStrings>false</reflowLongStrings>
495+
</googleJavaFormat>
496+
</java>
497+
</configuration>
498+
</plugin>
469499
</plugins>
470500
</pluginManagement>
471501

0 commit comments

Comments
 (0)