How to analyze a Maven pom.xml for mistakes (a pom linter for redundant or ignored properties?)
Asked Answered
K

3

10

My pom.xml is messy, having collected cruft over time. Is there any automated way to "clean up" a pom? Like a linter but for maven.

In IntelliJ I can run Analyze > Inspect Code and get a list of unused functions, silly iterators, and probable bugs. I think I'm looking for the same thing for my pom

  1. Unused repositories, pluginRepositories
  2. Unused properties
  3. properties that collide with settings specified within the plugin
  4. Dependencies with more recent versions (mvn versions:display-dependency-updates works, but doesn't restrict to non-breaking version updates)
Kusin answered 21/7, 2020 at 15:48 Comment(2)
1. Repositories are usually not defined in the POM, but in the settings.xml. 2. To determine whether a property is used or not would mean to look inside the plugin code. 3. Maybe this could be done 4. How do you know if a version is non-breaking? If you want to avoid major upgrades, there a parameter for that.Clansman
Maven itself warns you about certain mistakes. When you let your project build do you see some? An upgrade to maven 3.5 or so also helped because you get colored output... Eclipse gives you basic info when your pom has some redundant stuff inside. But I don't know anything to the extend what you describe/want...Froh
A
1

For a simple static analysis of your pom.xml, you can use the lint-maven-plugin.

  1. Add the following to your pom.xml:
<build>
  <plugins>
    <plugin>
      <groupId>com.lewisd</groupId>
      <artifactId>lint-maven-plugin</artifactId>
      <version>0.0.11</version>
    </plugin>
  </plugins>
</build>
  1. Run the lint:check goal to check your pom.xml for violations.
$ mvn lint:check

The plugin will report any violations in your pom.xml as demonstrated below.

[INFO] --- lint-maven-plugin:0.0.11:check (default-cli) @ maven-sample ---
[INFO] Writing summary report
[INFO] [LINT] Completed with 3 violations
[INFO] [LINT] OSSDevelopersSectionRule: missing <developers/> section : 0:0 : /Users/jdoe/workspace/maven-sample/pom.xml
[INFO] [LINT] OSSInceptionYearRule: missing <inceptionYear/> information : 0:0 : /Users/jdoe/workspace/maven-sample/pom.xml
[INFO] [LINT] GAVOrder: Found 'name' but was expecting 'packaging' : 19:8 : /Users/jdoe/workspace/maven-sample/pom.xml

You can see a list of available rules by running the lint:list goal.

Aweigh answered 23/12, 2021 at 15:59 Comment(1)
In a way this is a good answer, but that linter got its last commit in 2015 and the About text on GitHub says "No longer supported": github.com/lewisd32/lint-maven-pluginHaydenhaydn
W
0

One approach is to use Maven Enforcer Plugin to achieve this. It has many built in rules OOB & we can add custom rules for the mistakes that we want to catch that are not built in. Basically the plugin will prevent a build from succeeding if the configured rules (mistakes you want to avoid) are violated.

The advantage with this is that you not only catch mistakes & fix them as part of setting it up but once you set it up you can also prevent the mistakes from creeping in again (due to your enforced rules that will fail the build & force fixing of the mistake before delivery).

Check this for some examples to get started. This part on custom rules is what you want.

I myself use it & find it very useful since ours is a distributed team.

Thanks

Wiltonwiltsey answered 28/5, 2024 at 19:43 Comment(0)
K
-1

Sounds like there isn't a pom.xml linter like jshint.com that I can paste my pom into and get advice out.

Kusin answered 22/7, 2020 at 18:22 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.