All Projects → Cosium → Git Code Format Maven Plugin

Cosium / Git Code Format Maven Plugin

Licence: mit
A maven plugin that automatically deploys https://github.com/google/google-java-format code formatter as a pre-commit git hook

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Git Code Format Maven Plugin

Hsweb Payment
基于hsweb的开源聚合支付系统,多商户,多渠道,多配置.
Stars: ✭ 112 (-7.44%)
Mutual labels:  maven
Libsass Maven Plugin
libsass wrapper for maven
Stars: ✭ 114 (-5.79%)
Mutual labels:  maven
Ibase4j
Spring,SpringBoot 2.0,SpringMVC,Mybatis,mybatis-plus,motan/dubbo分布式,Redis缓存,Shiro权限管理,Spring-Session单点登录,Quartz分布式集群调度,Restful服务,QQ/微信登录,App token登录,微信/支付宝支付;日期转换、数据类型转换、序列化、汉字转拼音、身份证号码验证、数字转人民币、发送短信、发送邮件、加密解密、图片处理、excel导入导出、FTP/SFTP/fastDFS上传下载、二维码、XML读写、高精度计算、系统配置工具类等等。
Stars: ✭ 1,548 (+1179.34%)
Mutual labels:  maven
M1 Test
Java编译速度对比测试: M1 MacBook Air vs Intel Core i9 MacBook Pro
Stars: ✭ 111 (-8.26%)
Mutual labels:  maven
Confluence Publisher
Maven plugin and Docker image to convert AsciiDoc and publish it to Confluence
Stars: ✭ 112 (-7.44%)
Mutual labels:  maven
Next Sanity
Sanity.io toolkit for Next.js
Stars: ✭ 115 (-4.96%)
Mutual labels:  hook
Docker Maven Plugin
Maven plugin for running and creating Docker images
Stars: ✭ 1,535 (+1168.6%)
Mutual labels:  maven
Easyprotector
一行代码检测XP/调试/多开/模拟器/root
Stars: ✭ 1,732 (+1331.4%)
Mutual labels:  hook
Spring Cloud Build
Common build concerns, shared plugin configuration, etc. for Spring Cloud modules
Stars: ✭ 114 (-5.79%)
Mutual labels:  maven
Kft Activiti Demo
Demo for workflow framework and BPM platform -- Activiti
Stars: ✭ 1,548 (+1179.34%)
Mutual labels:  maven
React Smooth Scroll Hook
A React Hook for using smooth scroll in React Component
Stars: ✭ 114 (-5.79%)
Mutual labels:  hook
Taotao Store
淘淘网上商城是一个综合性的B2C平台,类似京东商城、天猫商城。会员可以在商城浏览商品、下订单,以及参加各种活动。商家可以在入住淘淘商城,在该平台上开店出售自己的商品,并且得到淘淘商城提供的可靠的服务。管理员、运营可以在平台后台管理系统中管理商品、订单、会员等。客服可以在后台管理系统中处理用户的询问以及投诉。
Stars: ✭ 114 (-5.79%)
Mutual labels:  maven
Hooks
Async middleware for JavaScript and TypeScript
Stars: ✭ 117 (-3.31%)
Mutual labels:  hook
Java Comment Preprocessor
The Most Powerful Multi-Pass Java Preprocessor
Stars: ✭ 111 (-8.26%)
Mutual labels:  maven
React Timer Hook
React timer hook
Stars: ✭ 118 (-2.48%)
Mutual labels:  hook
Xseries
Library for cross-version Minecraft Bukkit support and various efficient API methods.
Stars: ✭ 109 (-9.92%)
Mutual labels:  maven
Maven S3 Wagon
Multi-threaded wagon to connect Maven with Amazon S3
Stars: ✭ 114 (-5.79%)
Mutual labels:  maven
Aem Guides Wknd
Tutorial Code companion for Getting Started Developing with AEM Sites WKND Tutorial
Stars: ✭ 121 (+0%)
Mutual labels:  maven
Webdrivermanager
WebDriverManager (Copyright © 2015-2021) is a project created and maintained by Boni Garcia and licensed under the terms of the Apache 2.0 License.
Stars: ✭ 1,808 (+1394.21%)
Mutual labels:  maven
Jgitver Maven Plugin
maven core extension to automatically define versions using jgitver & git tags
Stars: ✭ 117 (-3.31%)
Mutual labels:  maven

Maven Central Latest Build Status

Git Code Format Maven Plugin

A maven plugin that automatically deploys google-java-format code formatter as a pre-commit git hook. On commit, the hook will automatically format staged java files.

Breaking changes between 2.x and 3.x

Breaking changes between 1.x and 2.x

  • #37 To prevent conflicts with other plugins all keys are now prefixed with gcf. e.g. -DglobPattern=**/* becomes -Dgcf.globPattern=**/*
  • #38 To avoid infringement to Apache Maven Trademark, the plugin was renamed to git-code-format-maven-plugin. Its new coordinates are com.cosium.code:git-code-format-maven-plugin.

1.x documentation can be found here

Automatic code format and validation activation

Add this to your maven project root pom.xml :

<build>
  <plugins>
    <plugin>
      <groupId>com.cosium.code</groupId>
      <artifactId>git-code-format-maven-plugin</artifactId>
      <version>${git-code-format-maven-plugin.version}</version>
      <executions>
        <!-- On commit, format the modified java files -->
        <execution>
          <id>install-formatter-hook</id>
          <goals>
            <goal>install-hooks</goal>
          </goals>
        </execution>
        <!-- On Maven verify phase, fail if any file
        (including unmodified) is badly formatted -->
        <execution>
          <id>validate-code-format</id>
          <goals>
            <goal>validate-code-format</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Manual code formatting

mvn git-code-format:format-code -Dgcf.globPattern=**/*

Manual code format validation

mvn git-code-format:validate-code-format -Dgcf.globPattern=**/*

Google Java Format options

The plugin allows you to tweak Google Java Format options :

<build>
  <plugins>
    <plugin>
      <groupId>com.cosium.code</groupId>
      <artifactId>git-code-format-maven-plugin</artifactId>
      <version>${git-code-format-maven-plugin.version}</version>
      <executions>
        <!-- ... -->
      </executions>
      <configuration>
        <googleJavaFormatOptions>
          <aosp>false</aosp>
          <fixImportsOnly>false</fixImportsOnly>
          <skipSortingImports>false</skipSortingImports>
          <skipRemovingUnusedImports>false</skipRemovingUnusedImports>
        </googleJavaFormatOptions>
      </configuration>
    </plugin>
  </plugins>
</build>

Documentation from the google-java-format CLI tool :

--aosp, -aosp, -a
  Use AOSP style instead of Google Style (4-space indentation).
--fix-imports-only
  Fix import order and remove any unused imports, but do no other formatting.
--skip-sorting-imports
  Do not fix the import order. Unused imports will still be removed.
--skip-removing-unused-imports
  Do not remove unused imports. Imports will still be sorted.

Frequently asked questions

If I have a multi-module project, do I need to install anything in the sub-projects?

You only need to put the plugin in your root project pom.xml. By default all submodules will be handled.

Do I need to run mvn initialize or is that a stage that happens automatically when I run mvn compile or mvn test?

initialize is the first phase of the Maven lifecycle. Any goal that you perform (e.g. compile or test) will automatically trigger initialize and thus trigger the git pre-commit hook installation.

I'm not noticing anything happening.

If after setting up the plugin in your pom, you just executed a maven goal, the only expected output is a pre-commit hook installed in your .git/hooks directory. To trigger the automatic formatting, you have to perform a commit of a modified java file. You can also manually format or validate any file.

I'd like to skip code formatting in a child project

I inherit an enterprise parent pom, which I cannot modify, with formatting plugin specified, and I need to turn off formatting for my group's project. Either use add a <skip>true</skip> configuration in the inheriting project or set the gcf.skip property to true.

How the hook works

On the initialize maven phase, git-code-format:install-hooks installs a git pre-commit hook that looks like this :

#!/bin/bash
"./.git/hooks/${project.artifactId}.git-code-format.pre-commit.sh"

and .git/hooks/${project.artifactId}.git-code-format.pre-commit.sh has the following content:

#!/bin/bash
set -e
"${env.M2_HOME}/bin/mvn" -f "${project.basedir}/pom.xml" git-code-format:on-pre-commit

On pre-commit git phase, the hook triggers the git-code-format:on-pre-commit which formats the code of the modified java files using google-java-format.

Advanced pre-commit pipeline hook

If you wish to modify the output of the pre-commit hook, you can set the preCommitHookPipeline configuration.

To completely ignore the hook output, you could use the following configuration:

      <configuration>
        <preCommitHookPipeline>&gt;/dev/null</preCommitHookPipeline>
      </configuration>

To display error lines from the maven output and fail build with any errors, you could use the following configuration:

      <configuration>
        <preCommitHookPipeline>| grep -F '[ERROR]' || exit 0 &amp;&amp; exit 1</preCommitHookPipeline>
      </configuration>
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].