Automate format with maven-formatter

Introduction

Originally, each team member set the format in the IDE and performed the format. It would be nice if it was automatically formatted when pushed to github. So I introduced it, so I will summarize the method.

1. maven settings

This time, we will use formatter-maven-plugin.

1.1. Added 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 can execute format or validate with the following command.

//Run 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 target --exclus: Directories and files you want to exclude ** Note: Write properly up to / **

<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 make your own format settings, you need to specify an Eclipse format configuration file. The 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. Github Actions settings

2.1. Creating a YAML file

Create a YAML file in .github / workflows /. The execution contents are the following three. --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

With the above settings, a commit with format will be created when you push the code to github. This will prevent you from forgetting the format locally, and you can comfortably perform code reviews.

Recommended Posts

Automate format with maven-formatter
Format JSON with org.json
Format Ruby with VS Code
Prepare the format environment with "Rails" (VScode)
Use a named format with Ruby's format method
Format the contents of LocalDate with DateTimeFormatter
Automate integration testing with Maven Failsafe plugin