google style guide

基于 ubuntu 18.04 maven 3.6.1 intellij idea 2019.2.2

pom.xml 配置,需要引入spotless-maven-plugin google-java-format

<properties>
    <spotless.version>1.25.1</spotless.version>
</properties>


<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.1.6.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>com.google.googlejavaformat</groupId>
            <artifactId>google-java-format</artifactId>
            <version>1.7</version>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>com.diffplug.spotless</groupId>
            <artifactId>spotless-maven-plugin</artifactId>
            <version>${spotless.version}</version>
            <configuration>
                <formats>
                    <format>
                        <indent>
                            <spaces>true</spaces>
                            <tabs>false</tabs>
                            <spacesPerTab>4</spacesPerTab>
                        </indent>
                        <trimTrailingWhitespace/>
                        <endWithNewline/>
                    </format>
                </formats>
                <java>
                    <includes>
                        <include>src/main/java/**/*.java</include>
                    </includes>
                    <excludes>
                        <exclude>src/test/java/**/*.java</exclude>
                    </excludes>
                    <googleJavaFormat>
                        <version>1.7</version>
                        <style>AOSP</style>
                    </googleJavaFormat>
                </java>
            </configuration>
        </plugin>
    </plugins>
</build>

在项目根目录下执行

  • mvn spotless:check
  • mvn spotless:apply

idea 配置 google java format 方式

  1. 安装 google java format , File -> Settings -> Plugins -> search google java format -> install
  2. 加载 intellij-java-google-style.xml , File -> Settings -> Editor -> Code Style -> schema -> import schema -> Intellij IDEA code style XML
  3. 启用 google java format , File -> Settings -> Other Settings -> google-java-format Settings -> check Enable google-java-format , 此处可以选择 default google java style 或者 Android Open Source Project(AOSP) style

参考

  1. github - google styleguide
  2. google-java-format
  3. github - google-java-format
  4. maven - spotless maven plugin
  5. Spotless: Keep your code spotless with Maven
  6. spotless-maven-plugin java代码自动格式化mvn spotless:apply -fn
  • qq_43638135
    妲己再美究为妃: 博主没有想过自己接一些私活干吗?我现在还没毕业,但是我也确实听说外挂市场自动化游戏脚本市场挺火热的,并且报酬也很丰厚,但是具体的我也不是很清楚,求解答。 (1个月前 #47楼) 查看回复(2) 举报 回复
    22