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.
This time, we will use formatter-maven-plugin.
<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
<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>
--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>
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>
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/checkout@v2
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: format
run: |
mvn formatter:format
mvn formatter:validate
- uses: stefanzweifel/[email protected]
with:
commit_message: Formatted code
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.