Skip to content

Recommended configuration

Björn Ekryd edited this page Jan 15, 2024 · 12 revisions

Recommended configuration

Simple Example

Maven users can add this plugin with the following addition to their pom.xml file. This will make sure that you pom file is sorted every time you compile the project.

<build>
  <plugins>
    <plugin>
      <groupId>com.github.ekryd.sortpom</groupId>
      <artifactId>sortpom-maven-plugin</artifactId>
      <version>3.3.0</version>
      <executions>
        <execution>
          <goals>
            <goal>sort</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Advanced configuration

WARNING: This operation does not create a backup file; it simply reorders the existing pom file. (Make sure you have committed your previous changes before proceeding.)

The plugin offers a range of parameters, and here's how I would configure it in a new project:

  <build>
    <plugins>
      <plugin>
        <groupId>com.github.ekryd.sortpom</groupId>
        <artifactId>sortpom-maven-plugin</artifactId>
        <version>3.3.0</version>
        <configuration>
          <createBackupFile>false</createBackupFile>
          <predefinedSortOrder>custom_1</predefinedSortOrder>
          <lineSeparator>\n</lineSeparator>
          <sortProperties>true</sortProperties>
          <sortDependencies>scope,groupId,artifactId</sortDependencies>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>sort</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <!-- ... (Other plugins) -->
    </plugins>
   
    <!-- ... (Other build configurations) -->
</build>

What will it do?

  • Maintains Pom File Order: Your pom file is sorted automatically every time you compile with Maven.
  • No Backup File: It does NOT create a backup file for the original pom file.
  • Predefined Sort Order: Adopts a predefined sort order: grouping parent, basic info, and properties on top; dependencies and plugins in the middle; other info and profiles at the end.
  • Unix-like Line Endings: Utilizes Unix-like line endings in the pom file.
  • Sorts Pom Properties Section.
  • Dependencies Sorting: Sorts dependencies first by scope (to group test dependencies), then by groupId (to keep dependencies from the same vendor together), and finally by artifactId (to ensure strict ordering of artifacts).

A more detailed description of the parameters. SortPom can also be configured for Build Servers.

Command-line

If you want to run the SortPom plugin from commandline, it will be more convenient if you add a Plugin group to your general Maven Settings. To do this, open up ~/.m2/settings.xml and add the following plugin group:

<settings>
	...
	<pluginGroups>
  		<pluginGroup>com.github.ekryd.sortpom</pluginGroup>
	</pluginGroups>
	...
</settings>

You can then omit the full specification for the plugin and run

  • mvn sortpom:sort

  • mvn sortpom:verify