Google Intellij 中的代码样式格式 maven pom 很奇怪

Google code style formats maven pom strange in Intellij

我在 Java 项目中使用 Google 代码风格。我在 IntelliJ 中安装了 google-java-format 插件,但该插件并未涵盖所有格式规则(例如 java 导入)。文档建议在 IntelliJ 中额外添加 Google 样式指南作为代码样式方案。我下载了 intellij-java-google-style.xml 并添加了配置:

如果启用了 Google 代码样式,导入的格式会按预期进行,但我的 Maven POM 的格式已损坏。

默认方案的 Maven POM:

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.test</groupId>
    <artifactId>test</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <properties>
        <java.version>11</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven-git-code-format.version>1.39</maven-git-code-format.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.2</version>

                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>test.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.cosium.code</groupId>
                <artifactId>maven-git-code-format</artifactId>
                <version>${maven-git-code-format.version}</version>
                <executions>
                    <!-- On commit, format the modified java files -->
                    <execution>
                        <id>install-formatter-hook</id>
                        <goals>
                            <goal>install-hooks</goal>
                        </goals>
                    </execution>
                    <!-- On Maven verify phase, fail if any file
                    (including unmodified) is badly formatted -->
                    <execution>
                        <id>validate-code-format</id>
                        <goals>
                            <goal>validate-code-format</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

具有 Google 方案的 Maven POM:

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://maven.apache.org/POM/4.0.0"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <artifactId>test</artifactId>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <release>11</release>
        </configuration>
        <groupId>org.apache.maven.plugins</groupId>
        <version>3.8.1</version>
      </plugin>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>test.Main</mainClass>
            </manifest>
          </archive>
        </configuration>
        <groupId>org.apache.maven.plugins</groupId>

        <version>3.1.2</version>
      </plugin>
      <plugin>
        <artifactId>maven-git-code-format</artifactId>
        <executions>
          <!-- On commit, format the modified java files -->
          <execution>
            <goals>
              <goal>install-hooks</goal>
            </goals>
            <id>install-formatter-hook</id>
          </execution>
          <!-- On Maven verify phase, fail if any file
          (including unmodified) is badly formatted -->
          <execution>
            <goals>
              <goal>validate-code-format</goal>
            </goals>
            <id>validate-code-format</id>
          </execution>
        </executions>
        <groupId>com.cosium.code</groupId>
        <version>${maven-git-code-format.version}</version>
      </plugin>
    </plugins>
  </build>
  <groupId>com.test</groupId>
  <modelVersion>4.0.0</modelVersion>
  <packaging>jar</packaging>

  <properties>
    <java.version>11</java.version>
    <maven-git-code-format.version>1.39</maven-git-code-format.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <version>1.0</version>
</project>

XML 标签似乎是按字典顺序排序的。

有没有人分享我的经验并找到了解决我的问题的方法?

我重现了该行为,它在 XML 排列选项卡中定义:

安排格式

我猜,xml 排列针对 android xml 布局进行了优化,正如您从命名空间定义中看到的那样。

因此,编码风格适用于 android,它的行为符合预期。您可以在导入该样式后更改 XML 布局。

因此,您的 pom 格式没有损坏,只是不同而已。解决方案:只需更改安排或不使用该编码风格。

我找到了一个插件Pom Format plugin来解决这个问题。

记得在使用前将XML排列设置为默认值。