Configure Palantir Java Format with Spotless | Generated by AI

Home 2025.08

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>)

2. Indentation (<indent>)

3. Import Order (<importOrder>)

4. Other Useful Settings

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>

Running Spotless

Notes and Best Practices

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

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!


Back Donate