[Java] Automate format using maven-formatter

1 minute read

Introduction

Originally, each team member set the format in the IDE and performed the format. I hope it will be formatted automatically when you push to github. I introduced it because it was a story, so I will summarize the method.

1. maven settings

This time, use formatter-maven-plugin.

1.1. Add plugin to pom.xml.

<project ...>
    ...
    <plugins>
      <plugin>
        <groupId>net.revelc.code.formatter</groupId>
        <artifactId>formatter-maven-plugin</artifactId>
        <version>2.11.0</version>
      </plugin>
    </plugins>
    ...
</project>

After setting, you will be able to execute format or validate with the following command.

// execute format
mvn formatter:format
// check
mvn formatter:validate

1.2. Directory settings

When specifying a directory other than src/main/java or src/test/java

<plugin>
  <groupId>net.revelc.code.formatter</groupId>
  <artifactId>formatter-maven-plugin</artifactId>
  <version>2.11.0</version>
  <configuration>
    <directories>
      <directory>${project.build.sourceDirectory}</directory>
      <directory>${project.build.directory}/generated-sources</directory>
    </directories>
  </configuration>
</plugin>

When specifying a directory or file in src/main/java or src/test/java

-includes: directories and files you want to include -excludes: directories and files you want to exclude **Note: Write properly until /

<plugin>
  <groupId>net.revelc.code.formatter</groupId>
  <artifactId>formatter-maven-plugin</artifactId>
  <version>2.11.0</version>
  <configuration>
    <includes>
      <include>jp/****/****/****/formatter/</include>
    </includes>
    <excludes>
      <exclude>jp/relativitas/maven/plugins/formatter/special/</exclude>
      <exclude>**/*Test.java</exclude>
    </excludes>
  </configuration>
</plugin>

1.3. Format file settings

To do your own format settings, you need to specify the Eclipse format settings file. Recommendation is format published by google.

<plugin>
  <groupId>net.revelc.code.formatter</groupId>
  <artifactId>formatter-maven-plugin</artifactId>
  <version>2.11.0</version>
  <configuration>
    <configFile>${project.basedir}/eclipse-java-google-style.xml</configFile>
  </configuration>
</plugin>

2. Setting up Github Actions

2.1. Creating a YAML file

Create a YAML file in .github/workflows/. The following three items are executed. -Code format -check after format -commit the formatted code

 name: auto-format
on:
  push:
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      -uses: actions/[email protected]
      -name: Set up JDK 1.8
        uses: actions/[email protected]
        with:
          java-version: 1.8
      -name: format
        run: |
          mvn formatter:format
          mvn formatter:validate
      -uses: stefanzweifel/[email protected]
        with:
          commit_message: Formatted code

in conclusion

If you do the above settings, a commit will be created in which format is performed at the timing of pushing the code to github. This will allow you to comfortably perform code reviews without forgetting local format.