Skip to content

Commit e4e0a26

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

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

.github/workflows/icu4j.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ permissions:
3939

4040
jobs:
4141

42+
# Using the Java style formatter google-java-style provided by the Spotless
43+
# plugin configured in the root pom.xml using 4-space indents (AOSP style).
44+
# Spotless is configured to run only on files in this branch (PR) that differ
45+
# from origin/main
46+
formatter:
47+
name: Formatter + Style checker
48+
runs-on: ubuntu-latest
49+
steps:
50+
- name: Checkout and setup
51+
uses: actions/checkout@v4
52+
with:
53+
fetch-depth: 0 # fetch all branches so that Spotless can resolve `origin/main`
54+
- name: Check Java style
55+
run: mvn spotless:check || (echo "Style checker failed. Formatting changes can be applied by 'mvn spotless:apply'" && exit 1)
56+
4257
# Initialize the Maven artifact cache
4358
#
4459
# This job is created according to the cache strategy of reuse from a single job:

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: 25 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,28 @@
466469
<artifactId>exec-maven-plugin</artifactId>
467470
<version>3.4.1</version>
468471
</plugin>
472+
<plugin>
473+
<groupId>com.diffplug.spotless</groupId>
474+
<artifactId>spotless-maven-plugin</artifactId>
475+
<version>${spotless.version}</version>
476+
<configuration>
477+
<!-- optional: limit format enforcement to just the files changed by this feature branch -->
478+
<!-- You can explicitly disable ratchet functionality by providing the value 'NONE': -->
479+
<ratchetFrom>NONE</ratchetFrom>
480+
<!-- define a language-specific format -->
481+
<java>
482+
<toggleOffOn />
483+
<!-- no need to specify files, inferred automatically, but you can if you want -->
484+
<!-- apply a specific flavor of google-java-format and reflow long strings -->
485+
<googleJavaFormat>
486+
<!-- version of google-java-style -->
487+
<version>${google-java-style.version}</version>
488+
<style>AOSP</style>
489+
<reflowLongStrings>false</reflowLongStrings>
490+
</googleJavaFormat>
491+
</java>
492+
</configuration>
493+
</plugin>
469494
</plugins>
470495
</pluginManagement>
471496

0 commit comments

Comments
 (0)