Spotless + Java Google format vs Intellij + import file
Asked Answered
B

1

10

I am trying to configure a maven spring boot application to use spotless with google java format style. I would like as well to have Intellij formatting the code as the maven plugin does.

But i am noticing that the formmat applied differ a little bit.

One example: Intellij will not change the following 2 lines

@Mock 
private UserRepository userRepositoryMock;

But the maven plugin (using the command: mvn spotless:apply) will format like this:

@Mock private UserRepository userRepositoryMock;

POM CONFIG

<plugin>
                <groupId>com.diffplug.spotless</groupId>
                <artifactId>spotless-maven-plugin</artifactId>
                <version>${spotless.version}</version>
                <configuration>
                    <!-- optional: limit format enforcement to just the files changed by this feature branch -->
                    <ratchetFrom>origin/master</ratchetFrom>
                    <formats>
                        <!-- you can define as many formats as you want, each is independent -->
                        <format>
                            <!-- define the files to apply to -->
                            <includes>
                                <include>*.md</include>
                                <include>.gitignore</include>
                            </includes>
                            <!-- define the steps to apply to those files -->
                            <trimTrailingWhitespace/>
                            <endWithNewline/>
                            <indent>
                                <spaces>true</spaces>
                                <spacesPerTab>4</spacesPerTab>
                            </indent>
                        </format>
                    </formats>
                    <!-- define a language-specific format -->
                    <java>
                        <!-- no need to specify files, inferred automatically, but you can if you want -->
                        <importOrder>  <!-- or a custom ordering -->
                            <order>java,javax,org,com,microservices.book.multiplication,
                            </order>  <!-- or use <file>${project.basedir}/eclipse.importorder</file> -->
                            <!-- you can use an empty string for all the imports you didn't specify explicitly, and '\\#` prefix for static imports -->
                        </importOrder>

                        <removeUnusedImports/> <!-- self-explanatory -->

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

                        <googleJavaFormat>
                            <version>1.14.0</version>
                            <style>GOOGLE</style>
                            <reflowLongStrings>true</reflowLongStrings>
                        </googleJavaFormat>
                        <indent>
                            <tabs>true</tabs>
                            <spacesPerTab>2</spacesPerTab>
                        </indent>
                        <indent>
                            <spaces>true</spaces>
                            <spacesPerTab>4</spacesPerTab>
                        </indent>
                    </java>
                </configuration>
            </plugin>

On Intellij I added the the following code stile from google

Github site https://github.com/google/google-java-format

IntelliJ Java Google Style file https://raw.githubusercontent.com/google/styleguide/gh-pages/intellij-java-google-style.xml

Intellij code style window

Why the differences? What else may differ? How can I have Intellij formatting the code exactly as the google java code style format xml file?

Thank you so much for your help in advance

Regards

Biggin answered 3/3, 2022 at 15:20 Comment(2)
Did you ever figure this out? Running into the same issue right now.Reger
we decided to use spotless + google format only (while a small change using 4 spaces indentation instead of 2). On the top of this we created a git hook (pre-commit) which always apply the format before the commit to all the modified files, stage them and them commits. Applying the format is completely transparent :)Biggin
T
0

From documentation to fix the annotations problems:

<!-- fixes formatting of type annotations, see below -->
<formatAnnotations />
Tegan answered 8/12, 2023 at 18:39 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Depew

© 2022 - 2024 — McMap. All rights reserved.