1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

lint Java in pre-commit

1
Posted at

1. add spotless plugin to pom.xml

<plugin>
  <groupId>com.diffplug.spotless</groupId>
  <artifactId>spotless-maven-plugin</artifactId>
  <version>2.43.0</version>
  <executions>
     <execution>
      <goals>
       <goal>check</goal>
       <goal>apply</goal>
      </goals>
     </execution>
    </executions>
</plugin>

here, update <version>2.43.0</version> appropriately
google-java-format versions

2. add spotless configuratioin

<configuration>
    <java>
        <includes>
            <include>src/main/java/**/*.java</include>
            <include>src/test/java/**/*.java</include>
        </includes>
        <googleJavaFormat>
            <version>1.28.0</version>
            <style>AOSP</style>
        </googleJavaFormat>
        <importOrder />
        <removeUnusedImports />
    </java>
</configuration>

more spotless format configuration

3. add hooks/pre-commit

#!/usr/bin/env bash
mvn spotless:apply

4. configure maven plugin to install a pre-commit hook

<plugin>
   <groupId>com.rudikershaw.gitbuildhook</groupId>
   <artifactId>git-build-hook-maven-plugin</artifactId>
   <version>3.1.0</version>
   <configuration>
      <installHooks>
         <pre-commit>hooks/pre-commit</pre-commit>
      </installHooks>
   </configuration>
   <executions>
      <execution>
         <goals>
            <goal>install</goal>
         </goals>
      </execution>
   </executions>
</plugin>

you should be good to go

things learned

based on https://github.com/google/google-java-format, it looks like we add

<dependency>
  <groupId>com.google.googlejavaformat</groupId>
  <artifactId>google-java-format</artifactId>
  <version>${google-java-format.version}</version>
</dependency>

why do we add it under <plugin> tag?

✏️

  • dependencies in pom.xml are for things application code needs at compile/run/test time
  • plugins are for tools that run during the build (formatters, linters, code generators, packagers)
  • google-java-format is a formatter engine, not something app code should ship with

all combined

<plugin>
    <groupId>com.diffplug.spotless</groupId>
    <artifactId>spotless-maven-plugin</artifactId>
    <version>2.43.0</version>
    <executions>
        <execution>
            <goals>
                <goal>check</goal>
                <goal>apply</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <java>
            <includes>
                <include>src/main/java/**/*.java</include>
                <include>src/test/java/**/*.java</include>
            </includes>
            <googleJavaFormat>
                <version>1.28.0</version>
                <style>AOSP</style>
            </googleJavaFormat>
            <importOrder />
            <removeUnusedImports />
        </java>
    </configuration>
</plugin>
<plugin>
    <groupId>com.rudikershaw.gitbuildhook</groupId>
    <artifactId>git-build-hook-maven-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
        <installHooks>
            <pre-commit>hooks/pre-commit</pre-commit>
        </installHooks>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>install</goal>
            </goals>
        </execution>
    </executions>
</plugin>
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?