Skip to content

Commit 94d5e8b

Browse files
build: add spotless
1 parent 2bb3465 commit 94d5e8b

File tree

5 files changed

+112
-525
lines changed

5 files changed

+112
-525
lines changed

pom.xml

Lines changed: 87 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -188,25 +188,42 @@
188188
</plugin>
189189

190190
<plugin>
191-
<groupId>com.mycila.maven-license-plugin</groupId>
192-
<artifactId>maven-license-plugin</artifactId>
193-
<version>1.10.b1</version>
191+
<groupId>com.diffplug.spotless</groupId>
192+
<artifactId>spotless-maven-plugin</artifactId>
193+
<version>2.43.0</version>
194194
<configuration>
195-
<header>LICENSE</header>
196-
<strictCheck>true</strictCheck>
197-
<skipExistingHeaders>true</skipExistingHeaders>
198-
<includes>
199-
<include>src/main/java/**/*.java</include>
200-
</includes>
195+
<upToDateChecking>
196+
<enabled>true</enabled>
197+
<indexFile>tmp/spotless</indexFile>
198+
</upToDateChecking>
199+
<formats>
200+
<!-- you can define as many formats as you want, each is independent -->
201+
<format>
202+
<!-- define the files to apply to -->
203+
<includes>
204+
<include>*.java</include>
205+
</includes>
206+
<!-- define the steps to apply to those files -->
207+
<trimTrailingWhitespace/>
208+
<endWithNewline/>
209+
</format>
210+
</formats>
211+
<java>
212+
<googleJavaFormat>
213+
<reflowLongStrings>true</reflowLongStrings>
214+
</googleJavaFormat>
215+
216+
<formatAnnotations />
217+
218+
<importOrder>
219+
<order>java,javax,org,</order>
220+
</importOrder>
221+
222+
<licenseHeader>
223+
<file>LICENSE</file>
224+
</licenseHeader>
225+
</java>
201226
</configuration>
202-
<executions>
203-
<execution>
204-
<phase>verify</phase>
205-
<goals>
206-
<goal>format</goal>
207-
</goals>
208-
</execution>
209-
</executions>
210227
</plugin>
211228

212229
<!-- Deploy plugin -->
@@ -256,6 +273,57 @@
256273
</build>
257274
</profile>
258275

276+
<profile>
277+
<id>git-hooks</id>
278+
<activation>
279+
<file>
280+
<exists>${pre-commit-hook}</exists>
281+
</file>
282+
</activation>
283+
<build>
284+
<plugins>
285+
<plugin>
286+
<groupId>org.apache.maven.plugins</groupId>
287+
<artifactId>maven-antrun-plugin</artifactId>
288+
<version>${maven-antrun-plugin.version}</version>
289+
<executions>
290+
<execution>
291+
<id>formatter.sh</id>
292+
<phase>initialize</phase>
293+
<configuration>
294+
<target>
295+
<chmod file="${pre-commit-hook}" perm="755"/>
296+
</target>
297+
</configuration>
298+
<goals>
299+
<goal>run</goal>
300+
</goals>
301+
</execution>
302+
</executions>
303+
</plugin>
304+
<!-- Git Hooks -->
305+
<plugin>
306+
<groupId>com.rudikershaw.gitbuildhook</groupId>
307+
<artifactId>git-build-hook-maven-plugin</artifactId>
308+
<version>3.4.1</version>
309+
<configuration>
310+
<installHooks>
311+
<pre-commit>${pre-commit-hook}</pre-commit>
312+
</installHooks>
313+
</configuration>
314+
<executions>
315+
<execution>
316+
<id>install-hooks</id>
317+
<goals>
318+
<goal>install</goal>
319+
</goals>
320+
</execution>
321+
</executions>
322+
</plugin>
323+
</plugins>
324+
</build>
325+
</profile>
326+
259327
<profile>
260328
<id>sonatype</id>
261329
<build>
@@ -350,8 +418,10 @@
350418
<jackson2-version>2.14.0-rc2</jackson2-version>
351419
<jacoco.version>0.8.11</jacoco.version>
352420
<maven.build.timestamp.format>yyyy-MM-dd HH:mm:ssa</maven.build.timestamp.format>
421+
<maven-antrun-plugin.version>3.1.0</maven-antrun-plugin.version>
353422
<timestamp>${maven.build.timestamp}</timestamp>
354423
<maven.compiler.source>17</maven.compiler.source>
355424
<maven.compiler.target>17</maven.compiler.target>
425+
<pre-commit-hook>src${file.separator}etc${file.separator}formatter.sh</pre-commit-hook>
356426
</properties>
357427
</project>

src/etc/checkstyle.xml

Lines changed: 0 additions & 204 deletions
This file was deleted.

src/etc/formatter.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env sh
2+
stagedFiles=$(git diff --staged --diff-filter=d --name-only | grep ".*\(java\|kt\)$")
3+
if [ "$stagedFiles" != "" ]
4+
then
5+
# top/parent directory:
6+
projectDir=$(cd "$(dirname "$1")"; pwd)/$(basename "$1")
7+
# Generate a comma separated file from staged files and prepend the projectDir, required by spotless
8+
files=""
9+
count=0
10+
for file in $stagedFiles; do
11+
absfilepath="$projectDir$file"
12+
if [ -f "$absfilepath" ]
13+
then
14+
files+="$absfilepath,"
15+
let count++
16+
fi
17+
done
18+
19+
if (( $count > 0 ))
20+
then
21+
echo "formatting ${count} file(s)"
22+
mvn spotless:apply -DspotlessFiles="$files" -q
23+
git add $stagedFiles
24+
fi
25+
fi

0 commit comments

Comments
 (0)