• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

spotify/fmt-maven-plugin: Opinionated Maven Plugin that formats your Java code.

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

spotify/fmt-maven-plugin

开源软件地址(OpenSource Url):

https://github.com/spotify/fmt-maven-plugin

开源编程语言(OpenSource Language):

Java 99.0%

开源软件介绍(OpenSource Introduction):

Build Status license Maven Central

fmt-maven-plugin

UPDATE 2022-02-14: This plugin has moved from coveooss to the spotify Github org. The new groupId will be com.spotify.fmt, and the master branch has been renamed to main.

Formats your code using google-java-format which follows Google's code styleguide.

The format cannot be configured by design.

If you want your IDE to stick to the same format, google-java-format also includes integrations for IntelliJ and Eclipse IDE's, following the installation instructions on the README.

Usage

Standard pom.xml

To have your sources automatically formatted on each build, add to your pom.xml:

    <build>
        <plugins>
            <plugin>
                <groupId>com.spotify.fmt</groupId>
                <artifactId>fmt-maven-plugin</artifactId>
                <version>VERSION</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>format</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

If you prefer, you can only check formatting at build time using the check goal:

    <build>
        <plugins>
            <plugin>
                <groupId>com.spotify.fmt</groupId>
                <artifactId>fmt-maven-plugin</artifactId>
                <version>VERSION</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Overriding the Default Lifecycle Phase

Both goals have a Maven lifecycle phase configured by default.

Goal Default Phase
format process-sources
check verify

You may prefer to run these goals in a different phase instead.
Maven allows you to override the default phase by specifying a <phase> for the <execution>.

For example, you may prefer that the check goal is performed in an earlier phase such as validate:

                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>

Options

sourceDirectory represents the directory where your Java sources that need to be formatted are contained. It defaults to ${project.build.sourceDirectory}

testSourceDirectory represents the directory where your test's Java sources that need to be formatted are contained. It defaults to ${project.build.testSourceDirectory}

additionalSourceDirectories represents a list of additional directories that contains Java sources that need to be formatted. It defaults to an empty list.

verbose is whether the plugin should print a line for every file that is being formatted. It defaults to false.

filesNamePattern represents the pattern that filters files to format. The defaults value is set to .*\.java.

skip is whether the plugin should skip the operation.

skipSortingImports is whether the plugin should skip sorting imports.

style sets the formatter style to be google or aosp. By default this is google. Projects using Android conventions may prefer aosp.

forkMode lets you specify whether to run google-java-format in a fork or in-process. Also adds JVM arguments to expose JDK internal javac APIs. Value default (which is the default) will fork (to avoid warnings for JDK9+ and to be able to run at all for JDK16+), never runs in-process, regardless of JDK version and always will always fork.

example:

<build>
    <plugins>
        <plugin>
            <groupId>com.spotify.fmt</groupId>
            <artifactId>fmt-maven-plugin</artifactId>
            <version>VERSION</version>
            <configuration>
                <sourceDirectory>some/source/directory</sourceDirectory>
                <testSourceDirectory>some/test/directory</testSourceDirectory>
                <verbose>true</verbose>
                <filesNamePattern>.*\.java</filesNamePattern>
                <additionalSourceDirectories>
                    <param>some/dir</param>
                    <param>some/other/dir</param>
                </additionalSourceDirectories>
                <skip>false</skip>
                <skipSortingImports>false</skipSortingImports>
                <style>google</style>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>format</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

check Options

displayFiles default = true. Display the list of the files that are not compliant.

displayLimit default = 100. Number of files to display that are not compliant.

failOnError default = true. Fail the build if non-compliant files are found.

example to not display the non-compliant files:

<build>
    <plugins>
        <plugin>
            <groupId>com.spotify.fmt</groupId>
            <artifactId>fmt-maven-plugin</artifactId>
            <version>VERSION</version>
            <configuration>
                <displayFiles>false</displayFiles>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>check</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

example to limit the display up to 10 files

<build>
    <plugins>
        <plugin>
            <groupId>com.spotify.fmt</groupId>
            <artifactId>fmt-maven-plugin</artifactId>
            <version>VERSION</version>
            <configuration>
                <displayLimit>10</displayLimit>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>check</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

example to only warn about non-compliant files instead of failing the build

<build>
    <plugins>
        <plugin>
            <groupId>com.spotify.fmt</groupId>
            <artifactId>fmt-maven-plugin</artifactId>
            <version>VERSION</version>
            <configuration>
                <failOnError>false</failOnError>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>check</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Command line

You can also use it on the command line

mvn com.spotify.fmt:fmt-maven-plugin:format

You can pass parameters via standard -D syntax. mvn com.spotify.fmt:fmt-maven-plugin:format -Dverbose=true

-Dfmt.skip is whether the plugin should skip the operation.

Using with Java 8

Starting from version 1.8, Google Java Formatter requires Java 11 to run. Incidently, all versions of this plugin starting from 2.10 inclusively also require this Java version to properly function. The 2.9.x release branch is the most up-to-date version that still runs on Java 8.

Deploy

  • git checkout main && git pull
  • mvn release:prepare - use x.y format for release version and x.y.z for SCM tag. (You can only do this as admin of the repo)
  • mvn release:perform -P release (make sure to use Maven settings which include credentials for the Sonatype staging repo)
  • git fetch - to make sure your local repo is up to date with the commits from the release plugin.
  • Create a GitHub release with merged PRs and other information.
  • Check that the release is available in Sonatype staging
  • Wait for the release to be available in Maven Central
  • Update version used for actual formatting in the POM.



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
mylandmarktechs/maven-web-apps发布时间:2022-08-16
下一篇:
alexnederlof/Jasper-report-maven-plugin: A fast jasper report maven plugin发布时间:2022-08-16
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap