A maven plugin that automatically deploys code formatters as pre-commit
git hook.
On commit, the hook will automatically format staged files.
- If the plugin runs without any formatter dependency, it will fail. This is done to prevent silent misconfiguration from happening.
Google Java Format
is not enabled by default anymore.com.cosium.code:google-java-format
must be added as a dependency to the plugin to keep using it.Google Java Format
options declaration structure has changed. You will need to migrate any eventual existing declaration to the new structure described by the google-java-format-options chapter .
- #64
google-java-format 1.8
dropped support for java 8. The minimum supported runtime version for the plugin is JDK 11. i.e. Maven must run on JDK 11+ while the target project can still be built and run using JDK 8.
- #37 To prevent conflicts with other plugins all keys are now
prefixed with
gcf
. e.g.-DglobPattern=**/*
becomes-Dgcf.globPattern=**/*
- #38 To avoid infringement to Apache Maven Trademark,
the plugin was renamed to
git-code-format-maven-plugin
. Its new coordinates arecom.cosium.code:git-code-format-maven-plugin
.
1.x
documentation can be found here
Add this to your maven project root pom.xml :
<build>
<plugins>
<plugin>
<groupId>com.cosium.code</groupId>
<artifactId>git-code-format-maven-plugin</artifactId>
<version>${git-code-format-maven-plugin.version}</version>
<executions>
<!-- On commit, format the modified files -->
<execution>
<id>install-formatter-hook</id>
<goals>
<goal>install-hooks</goal>
</goals>
</execution>
<!-- On Maven verify phase, fail if any file
(including unmodified) is badly formatted -->
<execution>
<id>validate-code-format</id>
<goals>
<goal>validate-code-format</goal>
</goals>
</execution>
</executions>
<dependencies>
<!-- Enable https://github.com/google/google-java-format -->
<dependency>
<groupId>com.cosium.code</groupId>
<artifactId>google-java-format</artifactId>
<version>${git-code-format-maven-plugin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
mvn git-code-format:format-code -Dgcf.globPattern=**/*
mvn git-code-format:validate-code-format -Dgcf.globPattern=**/*
The plugin allows you to tweak Google Java Format options :
<build>
<plugins>
<plugin>
<groupId>com.cosium.code</groupId>
<artifactId>git-code-format-maven-plugin</artifactId>
<version>${git-code-format-maven-plugin.version}</version>
<executions>
<!-- ... -->
</executions>
<dependencies>
<!-- ... -->
</dependencies>
<configuration>
<formatterOptions>
<!-- Use AOSP style instead of Google Style (4-space indentation). -->
<googleJavaFormat.aosp>false</googleJavaFormat.aosp>
<!-- Format the javadoc -->
<googleJavaFormat.formatJavadoc>true</googleJavaFormat.formatJavadoc>
<!-- Fix import order and remove any unused imports, but do no other formatting. -->
<googleJavaFormat.fixImportsOnly>false</googleJavaFormat.fixImportsOnly>
<!-- Do not fix the import order. Unused imports will still be removed. -->
<googleJavaFormat.skipSortingImports>false</googleJavaFormat.skipSortingImports>
<!-- Do not remove unused imports. Imports will still be sorted. -->
<googleJavaFormat.skipRemovingUnusedImports>false</googleJavaFormat.skipRemovingUnusedImports>
</formatterOptions>
</configuration>
</plugin>
</plugins>
</build>
Since google-java-format uses JDK internal apis, if you need to run the plugin with JDK 16+, you must pass some additional arguments to the JVM. Those are described at https://github.com/google/google-java-format/releases/tag/v1.10.0.
Thanks to https://maven.apache.org/configure.html#mvn-jvm-config-file, you should be able to pass them to .mvn/jvm.config
as follow:
--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
Thanks to its code formatter SPI, this plugin can execute any code formatter.
Note that you can take inspiration from the google-java-format
module of this project.
- Implement
com.cosium.code.format_spi.CodeFormatterFactory
. This interface is provided bycom.cosium.code:git-code-format-maven-plugin-spi
. - Add your
com.cosium.code.format_spi.CodeFormatterFactory
implementation canonical name inMETA-INF/services/com.cosium.code.format_spi.CodeFormatterFactory
. - Pack this in a jar that you declare as a dependency in this plugin declaration.
Suppose:
- the chosen
configurationId
(declared bycom.cosium.code.format_spi.CodeFormatterFactory#configurationId()
) isaqme
- the formatter dependency is
com.aqme.formatter:formatter:1.0
A plugin declaration making use of this custom code formatter would look like this:
<build>
<plugins>
<plugin>
<groupId>com.cosium.code</groupId>
<artifactId>git-code-format-maven-plugin</artifactId>
<version>${git-code-format-maven-plugin.version}</version>
<executions>
<!-- ... -->
</executions>
<dependencies>
<dependency>
<groupId>com.aqme.formatter</groupId>
<artifactId>formatter</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<configuration>
<formatterOptions>
<aqme.option1>false</aqme.option1>
<aqme.option2>foo</aqme.option2>
</formatterOptions>
</configuration>
</plugin>
</plugins>
</build>
You only need to put the plugin in your root project pom.xml. By default all submodules will be handled.
Do I need to run mvn initialize or is that a stage that happens automatically when I run mvn compile or mvn test?
initialize
is the first phase of the Maven lifecycle. Any goal that you perform (e.g. compile
or test
) will automatically trigger initialize
and thus trigger the git pre-commit hook installation.
If after setting up the plugin in your pom, you just executed a maven goal, the only expected output is a pre-commit hook installed in your .git/hooks
directory. To trigger the automatic formatting, you have to perform a commit of a modified file.
You can also manually format or validate any file.
I inherit an enterprise parent pom, which I cannot modify, with formatting plugin specified, and I need to turn off formatting for my group's project.
Either use add a <skip>true</skip>
configuration in the inheriting project or set the gcf.skip
property to true.
On the initialize
maven phase, git-code-format:install-hooks
installs a git pre-commit
hook that looks like this :
#!/bin/bash
"./.git/hooks/${project.artifactId}.git-code-format.pre-commit.sh"
and .git/hooks/${project.artifactId}.git-code-format.pre-commit.sh
has the following content:
#!/bin/bash
set -e
"${env.M2_HOME}/bin/mvn" -f "${project.basedir}/pom.xml" git-code-format:on-pre-commit
On pre-commit
git phase, the hook triggers the git-code-format:on-pre-commit
which formats the code of the modified files.
If you wish to modify the output of the pre-commit hook, you can set the preCommitHookPipeline
configuration.
To completely ignore the hook output, you could use the following configuration:
<configuration>
<preCommitHookPipeline>>/dev/null</preCommitHookPipeline>
</configuration>
To display error lines from the maven output and fail build with any errors, you could use the following configuration:
<configuration>
<preCommitHookPipeline>| grep -F '[ERROR]' || exit 0 && exit 1</preCommitHookPipeline>
</configuration>