maven-git-code-format Maven Plugin

A maven plugin that automatically deploys https://github.com/google/google-java-format code formatter as a pre-commit git hook

License

License

Categories

Categories

Maven Build Tools Git Development Tools Version Controls ORM Data
GroupId

GroupId

com.cosium.code
ArtifactId

ArtifactId

maven-git-code-format
Last Version

Last Version

1.39
Release Date

Release Date

Type

Type

maven-plugin
Description

Description

maven-git-code-format Maven Plugin
A maven plugin that automatically deploys https://github.com/google/google-java-format code formatter as a pre-commit git hook
Project URL

Project URL

https://github.com/cosium/maven-git-code-format
Project Organization

Project Organization

Cosium
Source Code Management

Source Code Management

https://github.com/Cosium/maven-git-code-format

Download maven-git-code-format

How to add to project

<plugin>
    <groupId>com.cosium.code</groupId>
    <artifactId>maven-git-code-format</artifactId>
    <version>1.39</version>
</plugin>

Dependencies

compile (8)

Group / Artifact Type Version
commons-io : commons-io jar 2.6
org.apache.commons : commons-lang3 jar 3.9
org.apache.commons : commons-exec jar 1.3
com.google.googlejavaformat : google-java-format jar 1.7
org.eclipse.jgit : org.eclipse.jgit jar 5.4.0.201906121030-r
org.apache.maven : maven-core jar 3.3.9
org.apache.maven : maven-plugin-api jar 3.3.9
org.codehaus.plexus : plexus-utils jar 3.2.1

provided (1)

Group / Artifact Type Version
org.apache.maven.plugin-tools : maven-plugin-annotations jar 3.3

test (6)

Group / Artifact Type Version
junit : junit jar 4.12
org.assertj : assertj-core jar 3.8.0
io.takari.maven.plugins : takari-plugin-testing jar 2.9.2
io.takari.maven.plugins : takari-plugin-integration-testing pom 2.9.2
ch.qos.logback : logback-classic jar 1.2.3
commons-codec : commons-codec jar 1.13

Project Modules

There are no modules declared in this project.

Maven Central Latest Build Status

Git Code Format Maven Plugin

A maven plugin that automatically deploys google-java-format code formatter as a pre-commit git hook. On commit, the hook will automatically format staged java files.

Breaking changes between 1.x and 2.x

  • #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 are com.cosium.code:git-code-format-maven-plugin.

1.x documentation can be found here

Automatic code format and validation activation

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 java 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>
    </plugin>
  </plugins>
</build>

Manual code formatting

mvn git-code-format:format-code -Dgcf.globPattern=**/*

Manual code format validation

mvn git-code-format:validate-code-format -Dgcf.globPattern=**/*

Google Java Format options

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>
      <configuration>
        <googleJavaFormatOptions>
          <aosp>false</aosp>
          <fixImportsOnly>false</fixImportsOnly>
          <skipSortingImports>false</skipSortingImports>
          <skipRemovingUnusedImports>false</skipRemovingUnusedImports>
        </googleJavaFormatOptions>
      </configuration>
    </plugin>
  </plugins>
</build>

Documentation from the google-java-format CLI tool :

--aosp, -aosp, -a
  Use AOSP style instead of Google Style (4-space indentation).
--fix-imports-only
  Fix import order and remove any unused imports, but do no other formatting.
--skip-sorting-imports
  Do not fix the import order. Unused imports will still be removed.
--skip-removing-unused-imports
  Do not remove unused imports. Imports will still be sorted.

Frequently asked questions

If I have a multi-module project, do I need to install anything in the sub-projects?

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.

I'm not noticing anything happening.

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 java file. You can also manually format or validate any file.

I'd like to skip code formatting in a child project

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.

How the hook works

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 java files using google-java-format.

Advanced pre-commit pipeline hook

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>&gt;/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 &amp;&amp; exit 1</preCommitHookPipeline>
      </configuration>
com.cosium.code

Versions

Version
1.39
1.38
1.37
1.36
1.35
1.34
1.33
1.32
1.31
1.30
1.29
1.28
1.27
1.26
1.25
1.24
1.23
1.22
1.21
1.20
1.19
1.18
1.17
1.16
1.15
1.14
1.13
1.12
1.11
1.10
1.9
1.8
1.7
1.6
1.5
1.4
1.3
1.2
1.1