Configure Palantir Java Format with Spotless | Generated by AI
To configure the Palantir Java Format with the Spotless Maven Plugin in a pom.xml
file, you need to set up the plugin to use palantirJavaFormat
and specify formatting options such as tabspace, indentation, import order, and other settings. Below is a detailed guide on how to configure these settings and the available options based on the Spotless plugin and Palantir Java Format.
Adding Spotless Maven Plugin with Palantir Java Format
Include the Spotless Maven Plugin in your pom.xml
and configure it to use palantirJavaFormat
. Here’s a basic setup with common configurations for tabspace, indentation, import order, and other options:
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>2.43.0</version> <!-- Use the latest version -->
<configuration>
<java>
<!-- Specify files to format (optional, defaults to all .java files) -->
<includes>
<include>src/main/java/**/*.java</include>
<include>src/test/java/**/*.java</include>
</includes>
<!-- Palantir Java Format -->
<palantirJavaFormat>
<version>2.53.0</version> <!-- Specify desired version -->
<style>GOOGLE</style> <!-- Options: GOOGLE, AOSP, or PALANTIR -->
<formatJavadoc>true</formatJavadoc> <!-- Optional: Format Javadoc -->
</palantirJavaFormat>
<!-- Indentation settings -->
<indent>
<tabs>true</tabs> <!-- Use tabs instead of spaces -->
<spacesPerTab>4</spacesPerTab> <!-- Number of spaces per tab -->
</indent>
<!-- Import order configuration -->
<importOrder>
<order>java,javax,org,com,\\#</order> <!-- Custom import order -->
</importOrder>
<!-- Remove unused imports -->
<removeUnusedImports/>
<!-- Other optional settings -->
<trimTrailingWhitespace/>
<endWithNewline/>
<toggleOffOn/> <!-- Enable spotless:off and spotless:on tags -->
</java>
</configuration>
<executions>
<execution>
<goals>
<goal>apply</goal> <!-- Automatically format code -->
</goals>
<phase>validate</phase> <!-- Run during validate phase -->
</execution>
</executions>
</plugin>
Explanation of Configuration Options
Here’s a breakdown of the key configuration options for the java
section in Spotless with palantirJavaFormat
, focusing on tabspace, indentation, import order, and other relevant settings:
1. Palantir Java Format (<palantirJavaFormat>
)
<version>
: Specifies the version ofpalantir-java-format
to use. Check the latest version on Maven Repository or GitHub. Example:<version>2.53.0</version>
.<style>
: Defines the formatting style. Options are:<formatJavadoc>
: Boolean to enable/disable Javadoc formatting (requires Palantir Java Format version ≥ 2.39.0). Example:<formatJavadoc>true</formatJavadoc>
.- Custom Group Artifact: Rarely needed, but you can specify a custom group and artifact for the formatter. Example:
<groupArtifact>com.palantir.java-format:palantir-java-format</groupArtifact>
.
2. Indentation (<indent>
)
<tabs>
: Boolean to use tabs (true
) or spaces (false
) for indentation. Example:<tabs>true</tabs>
.<spacesPerTab>
: Number of spaces per tab (used when<tabs>
isfalse
or for mixed indentation). Common values are2
or4
. Example:<spacesPerTab>4</spacesPerTab>
.- Note: Palantir Java Format’s style (e.g.,
GOOGLE
,AOSP
,PALANTIR
) may influence indentation behavior. For example,GOOGLE
defaults to 2 spaces, whileAOSP
andPALANTIR
use 4 spaces. The<indent>
settings in Spotless can override or complement these defaults, but ensure consistency to avoid conflicts.
- Note: Palantir Java Format’s style (e.g.,
3. Import Order (<importOrder>
)
<order>
: Specifies the order of import groups, separated by commas. Use\\#
for static imports and an empty string (""
) for unspecified imports. Example:<order>java,javax,org,com,\\#</order>
sorts imports starting withjava
, thenjavax
, etc., with static imports last.<file>
: Alternatively, specify a file containing the import order. Example:<file>${project.basedir}/eclipse.importorder</file>
. The file format matches Eclipse’s import order configuration (e.g.,java|javax|org|com|\\#
).- Example file content:
#sort java javax org com \#
- Example file content:
4. Other Useful Settings
<removeUnusedImports>
: Removes unused imports. Optionally, specify the engine:<trimTrailingWhitespace>
: Removes trailing whitespace from lines. Example:<trimTrailingWhitespace/>
.<endWithNewline>
: Ensures files end with a newline. Example:<endWithNewline/>
.<toggleOffOn>
: Enables// spotless:off
and// spotless:on
comments to exclude sections of code from formatting. Example:<toggleOffOn/>
.<licenseHeader>
: Adds a license header to files. Example:<licenseHeader> <content>/* (C) $YEAR */</content> </licenseHeader>
You can also use a file:
<file>${project.basedir}/license.txt</file>
.<formatAnnotations>
: Ensures type annotations are on the same line as the fields they describe. Example:<formatAnnotations/>
.<ratchetFrom>
: Limits formatting to files changed relative to a Git branch (e.g.,origin/main
). Example:<ratchetFrom>origin/main</ratchetFrom>
.
5. POM-Specific Formatting (<pom>
)
To format the pom.xml
file itself, use the <pom>
section with sortPom
:
<pom>
<sortPom>
<nrOfIndentSpace>2</nrOfIndentSpace> <!-- Indentation for POM -->
<predefinedSortOrder>recommended_2008_06</predefinedSortOrder> <!-- Standard POM order -->
<sortDependencies>groupId,artifactId</sortDependencies> <!-- Sort dependencies -->
<sortPlugins>groupId,artifactId</sortPlugins> <!-- Sort plugins -->
<endWithNewline>true</endWithNewline>
</sortPom>
</pom>
- Options for
sortPom
:<nrOfIndentSpace>
: Number of spaces for indentation (e.g.,2
or4
).<predefinedSortOrder>
: Options likerecommended_2008_06
orcustom_1
for element order.<sortDependencies>
: Sort bygroupId
,artifactId
, or other criteria.<sortPlugins>
: Sort plugins similarly.<endWithNewline>
: Ensure POM ends with a newline.<expandEmptyElements>
: Expand empty XML tags (e.g.,<tag></tag>
vs<tag/>
).
Running Spotless
- Check formatting:
mvn spotless:check
– Validates code against the configured rules, failing the build if violations are found. - Apply formatting:
mvn spotless:apply
– Automatically formats code to comply with the rules.
Notes and Best Practices
- Consistency with IDE: To align IntelliJ or Eclipse with Spotless, install the
palantir-java-format
IntelliJ plugin or use an Eclipse formatter XML file. For IntelliJ, import a compatible style file (e.g.,intellij-java-google-style.xml
for Google style) or configure manually to match Palantir settings. - Version Compatibility: Ensure the
palantir-java-format
version supports your Java version. For Java 17+, use a recent version (e.g., 2.53.0). Some features like pattern matching may have limited support. - Custom Formatting: For advanced customization, use an Eclipse formatter XML file with
<eclipse>
instead of<palantirJavaFormat>
:<eclipse> <file>${project.basedir}/custom-style.xml</file> </eclipse>
Example
custom-style.xml
:<?xml version="1.0" encoding="utf-8"?> <profiles version="21"> <profile kind="CodeFormatterProfile" name="custom" version="21"> <setting id="org.eclipse.jdt.core.formatter.tabulation.char" value="space"/> <setting id="org.eclipse.jdt.core.formatter.indentation.size" value="4"/> <setting id="org.eclipse.jdt.core.formatter.tabulation.size" value="4"/> </profile> </profiles>
- Limitations: Palantir Java Format is less configurable than Eclipse’s formatter but is designed for consistency and modern Java features (e.g., lambdas). It may not handle all edge cases (e.g., deeply nested lambdas).
Available Options Summary
Option | Description | Example Values |
---|---|---|
<palantirJavaFormat> |
Configures Palantir Java Format. | <version>2.53.0</version> , <style>PALANTIR</style> |
<indent> |
Sets indentation style (tabs or spaces) and size. | <tabs>true</tabs> , <spacesPerTab>4</spacesPerTab> |
<importOrder> |
Defines import group order or uses a file. | <order>java,javax,org,com,\\#</order> |
<removeUnusedImports> |
Removes unused imports, with optional engine. | <engine>cleanthat-javaparser-unnecessaryimport</engine> |
<trimTrailingWhitespace> |
Removes trailing whitespace. | <trimTrailingWhitespace/> |
<endWithNewline> |
Ensures files end with a newline. | <endWithNewline/> |
<toggleOffOn> |
Enables spotless:off /spotless:on tags. |
<toggleOffOn/> |
<licenseHeader> |
Adds a license header. | <content>/* (C) $YEAR */</content> |
<formatAnnotations> |
Formats annotations to be on the same line as fields. | <formatAnnotations/> |
<sortPom> |
Formats pom.xml with sorting and indentation. |
<nrOfIndentSpace>2</nrOfIndentSpace> |
Sources
- Spotless Maven Plugin Documentation: GitHub
- Palantir Java Format: GitHub
- Stack Overflow and community examples for Spotless configuration
This configuration ensures consistent code formatting with Palantir Java Format, tailored to your preferences for tabspace, indentation, and import order. Let me know if you need further clarification or additional options!