百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
F

fmt-maven-plugin

> 编程语言
开源

一个具有意见的 Maven 插件,用于格式化 Java 代码。

381 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

一个具有意见的 Maven 插件,用于格式化 Java 代码。

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:

xml
    
        
            
                com.spotify.fmt
                fmt-maven-plugin
                VERSION
                
                    
                        
                            format
                        
                    
                
                
                    
                        com.google.googlejavaformat
                        google-java-format
                        1.27.0
                    
                
            
        
    

The inclusion of the google-java-format as a dependency allows you to control that version, which is generally recommended, so that you can align it with the exact version used in your IDE, pre-commit Git hook, etc. (If you are fine getting "whatever version fmt-maven-plugin uses" then you could omit it.)

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

xml
    
        
            
                com.spotify.fmt
                fmt-maven-plugin
                VERSION
                
                    
                        
                            check
                        
                    
                
            
        
    

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 for the.

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

xml
                    
                        validate
                        
                            check
                        
                    

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.

skipReflowingLongStrings is whether the plugin should skip reflowing long strings. It defaults to true.

skipRemovingUnusedImports is whether the plugin should skip removing unused imports. It defaults to false.

skipSortingImports is whether the plugin should skip sorting imports.

skipSourceDirectory is whether the plugin should skip formatting/checking the sourceDirectory. It defaults to false.

skipTestSourceDirectory is whether the plugin should skip formatting/checking the testSourceDirectory. It defaults to false.

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:

…

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:

xml

    
        
            com.spotify.fmt
            fmt-maven-plugin
            VERSION
            
                false
            
            
                
                    
                        check
                    
                
            
        
    

example to limit the display up to 10 files

xml

    
        
            com.spotify.fmt
            fmt-maven-plugin
            VERSION
            
                10
            
            
                
                    
                        check
                    
                
            
        
    

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

xml

    
        
            com.spotify.fmt
            fmt-maven-plugin
            VERSION
            
                false
            
            
                
                    
                        check
                    
                
            
        
    

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.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Java

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言