Skip to content

Commit

Permalink
Added Spotless as code formatter and automated it. (#165)
Browse files Browse the repository at this point in the history
Added Spotless as code formatter and automated it.

Will format on every build and check as part of the Git pre-commit hook.

Unfortunately, leads to the world's most giant commit, as the formatting
of the pre-existing code is "catching up" to the new Spotless configs.

Part of Issue #162.
  • Loading branch information
karlmdavis authored Nov 19, 2023
1 parent 34e509a commit 47f57a9
Show file tree
Hide file tree
Showing 291 changed files with 3,859 additions and 4,663 deletions.
39 changes: 39 additions & 0 deletions .github/git-hook-pre-commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash

##
# This Git pre-commit hook script verifies that the Java source code is formatted consistently, and errors
# (preventing commit) if it is not.
#
# Git will call this script from the repository root. A non-zero exit code will halt the commit, unless
# `--no-verify` is specified.
##


# Stop script on errors.
set -o pipefail # Non-zero returns from piped commands throw ERR.
set -o errtrace # Non-zero returns in functions throw ERR.
set -o nounset # Attempts to use an uninitialized variable throw ERR.
set -o errexit # Non-zero returns from the main script throw ERR.

# This function can be called to handle errors.
function error_handler() {
local CALLER_LINENO="$1"
local MESSAGE="$2"
local CODE="${3:-1}"

local FULL_MESSAGE="Error on or near line ${CALLER_LINENO}: \"${MESSAGE}\". Exiting with status: ${CODE}."
echo -e "${FULL_MESSAGE}"
exit "${CODE}"
}

# Trap any errors, calling error() when they're caught.
trap 'error_handler ${LINENO}' ERR

echo 'Running fmt plugin check.'
# TODO switch plugin group after we're past Java 8
if mvn -f apps/ spotless:check >/dev/null 2>&1; then
echo 'Verified Java source code format: a-okay.'
else
echo -e "Inconsistencies discovered in Java formatting check. Run 'mvn spotless:check' for details or 'mvn spotless:apply' to automatically apply the required formatting."
exit 1
fi
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Ignore Eclipse metadata files, but include .gitignore itself
/.*
!/.gitignore
!/.github/

# Ignore vim metadata files.
*.swp
Expand Down
401 changes: 401 additions & 0 deletions dev/eclipse-formatter-java.xml

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions dev/eclipse-formatter-xml.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
lineWidth=120
184 changes: 141 additions & 43 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<prerequisites>
<maven>3.0</maven>
</prerequisites>

<parent>
<groupId>com.justdavis.karl.jessentials</groupId>
<artifactId>jessentials-parent</artifactId>
Expand All @@ -19,9 +14,7 @@
<packaging>pom</packaging>

<name>rps-tourney-parent</name>
<description>
The parent POM for the "Rock-Paper-Scissors Tourney" games.
</description>
<description>The parent POM for the "Rock-Paper-Scissors Tourney" games.</description>
<url>https://github.com/karlmdavis/rps-tourney</url>
<organization>
<name>Karl M. Davis</name>
Expand All @@ -33,8 +26,30 @@
</license>
</licenses>

<prerequisites>
<maven>3.0</maven>
</prerequisites>

<modules>
<module>rps-tourney-service-api</module>
<module>rps-tourney-service-client</module>
<module>rps-tourney-service-app</module>
<module>rps-tourney-console</module>
<module>rps-tourney-webapp</module>
<module>rps-tourney-benchmarks</module>
<module>rps-tourney-deployment</module>
</modules>

<scm>
<!-- URL format taken from http://www.sonatype.com/people/2009/09/maven-tips-and-tricks-using-github/ -->
<connection>scm:git:[email protected]:karlmdavis/rps-tourney.git</connection>
<developerConnection>scm:git:[email protected]:karlmdavis/rps-tourney.git</developerConnection>
<tag>HEAD</tag>
<url>https://github.com/karlmdavis/rps-tourney</url>
</scm>

<issueManagement>
<!-- There doesn't seem to be any tooling support for this yet, but it
<!-- There doesn't seem to be any tooling support for this yet, but it
doesn't hurt to include it. -->
<system>GitHub Issues</system>
<url>https://github.com/karlmdavis/rps-tourney/issues</url>
Expand All @@ -45,14 +60,6 @@
<url>https://justdavis.com/jenkins/job/rps-tourney/</url>
</ciManagement>

<scm>
<!-- URL format taken from http://www.sonatype.com/people/2009/09/maven-tips-and-tricks-using-github/ -->
<connection>scm:git:[email protected]:karlmdavis/rps-tourney.git</connection>
<developerConnection>scm:git:[email protected]:karlmdavis/rps-tourney.git</developerConnection>
<url>https://github.com/karlmdavis/rps-tourney</url>
<tag>HEAD</tag>
</scm>

<distributionManagement>
<repository>
<id>justdavis.com-nexus</id>
Expand All @@ -64,22 +71,16 @@
</snapshotRepository>
</distributionManagement>

<modules>
<module>rps-tourney-service-api</module>
<module>rps-tourney-service-client</module>
<module>rps-tourney-service-app</module>
<module>rps-tourney-console</module>
<module>rps-tourney-webapp</module>
<module>rps-tourney-benchmarks</module>
<module>rps-tourney-deployment</module>
</modules>

<properties>
<!-- The version of the Spring Framework artifacts to use. This property
<!-- The path to the parent POM's `dev` directory'. Needs to be redefined in each project, so that POM
inheritance doesn't goof it up. -->
<rps.dev.path>${project.basedir}/dev</rps.dev.path>

<!-- The version of the Spring Framework artifacts to use. This property
isn't used directly here, but is rather intended for use in child projects. -->
<spring.version>${dependency.springframework.version}</spring.version>

<!-- The version of Hibernate to use. This property isn't used directly
<!-- The version of Hibernate to use. This property isn't used directly
here, but is rather intended for use in child projects. -->
<hibernate.version>5.2.12.Final</hibernate.version>

Expand All @@ -88,11 +89,11 @@

<!-- The version of the Jackson artifacts to use across the modules. -->
<jackson.version>2.9.0</jackson.version>

<!-- The version of Jetty to use in ITs across all the modules. -->
<jetty.version>9.2.24.v20180105</jetty.version>

<!-- The default number of parallel forks to use for running the ITs. Note
<!-- The default number of parallel forks to use for running the ITs. Note
that "C" stands for "core", but also seems to include hyperthreaded cores. -->
<forkCount.its>0.25C</forkCount.its>

Expand Down Expand Up @@ -127,40 +128,40 @@
<version>[4.0.1-SNAPSHOT,]</version>
</dependency>
<dependency>
<!-- Used to provision and manage Tomcat server instances, for integration
<!-- Used to provision and manage Tomcat server instances, for integration
tests and such. -->
<groupId>com.justdavis.karl.jessentials</groupId>
<artifactId>jessentials-tomcat</artifactId>
<version>[1.0.0-SNAPSHOT,]</version>
</dependency>

<dependency>
<!-- Logback can be used as the logging target/backend for SLF4J: all
<!-- Logback can be used as the logging target/backend for SLF4J: all
logging events would be sent to it, if this dependency is included. -->
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>

<dependency>
<!-- Many/most of this project's modules currently use various Spring
<!-- Many/most of this project's modules currently use various Spring
libraries. This BOM will help prevent version conflicts between them. -->
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<dependency>
<!-- JDBC drivers for the PostgreSQL open source RDBMS. This version supports
<!-- JDBC drivers for the PostgreSQL open source RDBMS. This version supports
Java 8 and above and PostgreSQL 8.2 and above. -->
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.6.0</version>
</dependency>
<dependency>
<!-- An in-memory embedded SQL DB. Used as a fast and easy-to-provision
<!-- An in-memory embedded SQL DB. Used as a fast and easy-to-provision
DB for manual development testing, as well as unit/integration tests. -->
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
Expand All @@ -172,11 +173,72 @@
<build>
<pluginManagement>
<plugins>
<plugin>
<!-- Used to manage Git hook scripts for developers. -->
<groupId>com.rudikershaw.gitbuildhook</groupId>
<artifactId>git-build-hook-maven-plugin</artifactId>
<version>3.4.1</version>
</plugin>
<plugin>
<!-- Used to format and verify the format of source files. -->
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>2.30.0</version>
<configuration>
<pom>
<!-- Format Maven POMs. -->
<sortPom>
<nrOfIndentSpace>-1</nrOfIndentSpace>
</sortPom>
<trimTrailingWhitespace></trimTrailingWhitespace>
<endWithNewline></endWithNewline>
</pom>
<java>
<!-- Format Java files. -->
<includes>
<include>rps-tourney-*/**/*.java</include>
</includes>
<!-- TODO: consider adding in <cleanthat /> after JDK/plugin upgrade. -->
<eclipse>
<file>${rps.dev.path}/eclipse-formatter-java.xml</file>
</eclipse>
<trimTrailingWhitespace></trimTrailingWhitespace>
<endWithNewline></endWithNewline>
</java>
<formats>
<format>
<!-- Format JavaScript files. -->
<includes>
<include>src/main/webapp/resources/js/*.js</include>
</includes>
<eclipseWtp>
<type>JS</type>
</eclipseWtp>
<trimTrailingWhitespace></trimTrailingWhitespace>
<endWithNewline></endWithNewline>
</format>
<format>
<!-- Format XML files. -->
<includes>
<include>src/**/*.xml</include>
</includes>
<eclipseWtp>
<type>XML</type>
<files>
<file>${rps.dev.path}/eclipse-formatter-xml.prefs</file>
</files>
</eclipseWtp>
<trimTrailingWhitespace></trimTrailingWhitespace>
<endWithNewline></endWithNewline>
</format>
</formats>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<configuration>
<!-- This setting is marked as experimental, but should prevent partial
<!-- This setting is marked as experimental, but should prevent partial
installs in multi-module builds and releases. -->
<installAtEnd>true</installAtEnd>
</configuration>
Expand All @@ -185,7 +247,7 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<!-- This setting is marked as experimental, but should prevent partial
<!-- This setting is marked as experimental, but should prevent partial
deploys in multi-module builds and releases. -->
<deployAtEnd>true</deployAtEnd>
</configuration>
Expand All @@ -198,7 +260,7 @@
tests (both unit and integration). -->
<destFile>${sonar.jacoco.reportPath}</destFile>
<append>true</append>

<!-- Used by the report goal. -->
<dataFile>${sonar.jacoco.reportPath}</dataFile>
</configuration>
Expand All @@ -207,8 +269,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<!-- Run the ITs in parallel by default, using a separate fork for
each runner. To override this, specify a value of "1" for the 'forkCount.its'
<!-- Run the ITs in parallel by default, using a separate fork for
each runner. To override this, specify a value of "1" for the 'forkCount.its'
property on the command line. -->
<forkCount>${forkCount.its}</forkCount>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
Expand Down Expand Up @@ -242,6 +304,42 @@
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<!-- Used to manage Git hook scripts for developers. -->
<groupId>com.rudikershaw.gitbuildhook</groupId>
<artifactId>git-build-hook-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>install</goal>
</goals>
<configuration>
<installHooks>
<!-- Will install/update the developer's local Git hooks, as part of the
build process. -->
<pre-commit>../.github/git-hook-pre-commit.sh</pre-commit>
</installHooks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<!-- Used to format and verify the format of source files. -->
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<!-- Use this plugin to run the formatter on every build, which will
auto-format source files per the configuation above. Note that we
have a Git pre-commit hook that will verify this formatting. -->
<goal>apply</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Loading

0 comments on commit 47f57a9

Please sign in to comment.