一尘不染+ Java Google 格式与 Intellij +导入文件

发布于 2025-01-11 09:20:55 字数 4328 浏览 0 评论 0原文

我正在尝试配置 Maven Spring Boot 应用程序以使用具有 google java 格式样式的一尘不染。我也希望 Intellij 像 Maven 插件一样格式化代码。

但我注意到应用的格式略有不同。

一个例子: Intellij 不会更改以下 2 行

@Mock 
private UserRepository userRepositoryMock;

但是 maven 插件(使用命令:mvn Spotless:apply)将格式化如下:

@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 添加了以下代码

我从 google Github 站点 https://github.com/google/google-java-format

IntelliJ Java Google 样式文件 https://raw.githubusercontent.com/google /styleguide/gh-pages/intellij-java-google-style.xml

Intellij 代码样式窗口

为什么会有差异?还有什么可能不同?如何让 Intellij 完全按照 google java 代码样式格式 xml 文件来格式化代码?

非常感谢您提前提供的帮助

问候

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞旧 2025-01-18 09:20:55

从文档中修复注释问题:

<!-- fixes formatting of type annotations, see below -->
<formatAnnotations />

From documentation to fix the annotations problems:

<!-- fixes formatting of type annotations, see below -->
<formatAnnotations />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文