HowTo

How to configure spotless plugin to clean and format code

Spotless is a general-purpose formatting plugin used by 4,000 projects on GitHub (August 2020). Plugin requires a version of Maven higher or equal to 3.1.0. A clean and formatted code is what every developer needs to be delivered. It improves the readability of the code. And you leave a good impression in the community. So first of all I will show you the maven integration of the spotless plugin.

Set up with Maven project
See full documentation  https://github.com/diffplug/spotless/tree/main/plugin-maven
Checkout the latest version - https://mvnrepository.com/artifact/com.diffplug.spotless/spotless-maven-plugin

You have to add the below code snippet in between
<plugins></plugins> tag of your pom.xml

<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>2.25.0</version>
<configuration>
<formats>
<!-- you can define as many formats as you want, each is independent -->
<format>
<!-- define the files to apply to -->
<includes>
<include>src/main/java/**/*.java</include>
<include>src/test/java/**/*.java</include>
</includes>
<!-- define the steps to apply to those files -->
<trimTrailingWhitespace/>
<endWithNewline/>
<indent>
<tabs>true</tabs>
<spacesPerTab>4</spacesPerTab>
</indent>
</format>
</formats>
<!-- define a language-specific format -->
<java>
<toggleOffOn>
<off>@formatter:off</off>
<on>@formatter:on</on>
</toggleOffOn>
<!-- no need to specify files, inferred automatically, but you can if you want -->

<!-- apply a specific flavor of google-java-format and reflow long strings -->
<palantirJavaFormat>
<version>2.24.0</version>
</palantirJavaFormat>

<!-- make sure every file has the following copyright header.
optionally, Spotless can set copyright years by digging
through git history (see "license" section below) -->
<licenseHeader>
<content>
/***********************************************************
*
${organization.name} Pvt. Ltd. CONFIDENTIAL
*
* © 2022-2023 All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains the property
* of
${organization.name}. The intellectual and technical concepts contained
* herein are proprietary to ${organization.name} Incorporated may be covered by U.S.
* and Foreign Patents, patents in process, and are protected by trade
* secret or copyright law.
*
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from
${organization.name} Pvt. Ltd.
***********************************************************/
</content>
</licenseHeader>
</java>
</configuration>
</plugin>

Execute command  mvn spotless:apply  in your project directory to apply this plugin.


Set up with Gradle project
See full documentation  https://github.com/diffplug/spotless/tree/main/plugin-gradle
Checkout the latest version - https://mvnrepository.com/artifact/com.diffplug.spotless/spotless-plugin-gradle

update your build.gradle file as show below

plugins {
id "com.diffplug.spotless" version "6.11.0"
}
spotless {
java {
toggleOffOn('format:off', 'format:on')
target '*/**/*.java'
// You probably want an empty string at the end - all of the
// imports you didn't specify explicitly will go there.
removeUnusedImports()
googleJavaFormat()
importOrder('lombok', 'java', 'javax', 'com', 'com', 'org', 'com.example') // standard import order
licenseHeader '/***********************************************************\n' +
'* ${organization.name} Pvt. Ltd. CONFIDENTIAL\n' +
'*\n' +
'* © 2022-2023 All Rights Reserved.\n' +
'*\n' +
'* NOTICE: All information contained herein is, and remains the property\n' +
'* of ${organization.name}. The intellectual and technical concepts contained\n' +
'* herein are proprietary to ${organization.name} Incorporated may be covered by U.S.\n' +
'* and Foreign Patents, patents in process, and are protected by trade\n' +
'* secret or copyright law.\n' +
'*\n' +
'* Dissemination of this information or reproduction of this material\n' +
'* is strictly forbidden unless prior written permission is obtained\n' +
'* from ${organization.name} Pvt. Ltd.\n' +
'***********************************************************/'
}
}

Execute command  ./gradlew spotlessApply  in your project directory to apply this plugin.