DEV Community

ababy003
ababy003

Posted on

Use formatter-maven-plugin to format source code

Write first

  I believe everyone must be familiar with the coding style. Everyone has their own coding style and follows certain specifications, which is undoubtedly a good thing for themselves; but it may be a big trouble for teamwork! Their different styles will lead to unnecessary conflicts and troubles when synchronizing code. Therefore, it is particularly important to unify the code style within the team. Today I recommend a plug-in, you only need to execute the maven command to format the code according to the established style.

Official website

  https://code.revelc.net/formatter-maven-plugin/

Environmental

  • JDK 1.8+
  • Maven 3.3.9+

pom.xml Configuration

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

Run the command: mvn formatter:format to perform formatting.

How to format source files during build ?

<project>
    ...
    <plugins>
      <plugin>
        <groupId>net.revelc.code.formatter</groupId>
        <artifactId>formatter-maven-plugin</artifactId>
        <version>2.11.0</version>
        <executions>
          <execution>
            <goals>
              <goal>format</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
    ...
</project>
Enter fullscreen mode Exit fullscreen mode
<project>
    ...
    <plugins>
      <plugin>
        <groupId>net.revelc.code.formatter</groupId>
        <artifactId>formatter-maven-plugin</artifactId>
        <version>2.11.0</version>
        <executions>
          <execution>
            <goals>
              <goal>validate</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
    ...
</project>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)