gaul / modernizer-maven-plugin

Detect uses of legacy Java APIs
Apache License 2.0
368 stars 53 forks source link
apache-commons guava java joda-time maven-plugin static-analysis

Modernizer Maven Plugin

Maven Central

Modernizer Maven Plugin detects uses of legacy APIs which modern Java versions supersede. These modern APIs are often more performant, safer, and idiomatic than the legacy equivalents. For example, Modernizer can detect uses of Vector instead of ArrayList, String.getBytes(String) instead of String.getBytes(Charset), and Guava Objects.equal instead of Java 7 Objects.equals. The default configuration detects over 200 legacy APIs, including third-party libraries like Apache Commons, Guava, and Joda-Time.

Configuration

To run Modernizer, add the following to the <plugins> stanza in your pom.xml then invoke mvn modernizer:modernizer:

<plugin>
  <groupId>org.gaul</groupId>
  <artifactId>modernizer-maven-plugin</artifactId>
  <version>2.7.0</version>
  <configuration>
    <javaVersion>8</javaVersion>
  </configuration>
</plugin>

The <configuration> stanza can contain several elements:

To run Modernizer during the verify phase of your build, add the following to the modernizer <plugin> stanza in your pom.xml:

<executions>
  <execution>
    <id>modernizer</id>
    <phase>verify</phase>
    <goals>
      <goal>modernizer</goal>
    </goals>
  </execution>
</executions>

Command-line flags can override Modernizer configuration and ModernizerMojo documents all of these. The most commonly used flags:

Output Formats

The plugin can output Modernizer violations in one of many formats which can be configured with the <configuration> stanza using <outputFormat>.

The currently supported formats and their respective configuration options are outlined below:

Ignoring elements

Code can suppress violations within a class or method via an annotation. First add the following dependency to your pom.xml:

<dependencies>
  <dependency>
    <groupId>org.gaul</groupId>
    <artifactId>modernizer-maven-annotations</artifactId>
    <version>2.7.0</version>
  </dependency>
</dependencies>

Then add @SuppressModernizer to the element to ignore:

import org.gaul.modernizer_maven_annotations.SuppressModernizer;

public class Example {
    @SuppressModernizer
    public static void method() { ... }
}

References

License

Copyright (C) 2014-2022 Andrew Gaul

Licensed under the Apache License, Version 2.0