Search code examples
javamavenintellij-ideapom.xmlgoogle-java-format

Google code style formats maven pom strange in Intellij


I use the Google code style in my Java projects. I installed the google-java-format plugin in IntelliJ, but the plugin does not cover all formatting rules (e.g java imports). The documentation suggests to additionally add the Google style guide as code style scheme in IntelliJ. I downloaded the intellij-java-google-style.xml and added the config: IntelliJ config

If the Google code style is enabled the the imports are formatted as expected, but the formatting of my Maven POM is broken.

Maven POM with Default scheme:

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

Maven POM with Google scheme:

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

The XML tags seem to be sorted lexicographically.

Does anyone share my experience and has found a solution for my problem?


Solution

  • I reproduced that behavior and it is defined in the XML Arrangement Tab:

    enter image description here arrange formatting

    I guess, the xml arrangement is optimized for android xml layouts as you can see from the namespace definitions.

    So, the coding style is for android and it behaves as expected. You can change the XML layout after your have imported that style.

    Therefore, the formatting of your pom is not broken, it's just different. The solution: Just change the arrangement or don't use that coding style.