检测遗留Java API的用途
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:
Collections.sort instead of List.sortString.getBytes(String) instead of String.getBytes(Charset)ByteStreams.toByteArray(InputStream) instead of InputStream.readAllBytes().The default configuration detects over 400 legacy APIs, including third-party libraries like Apache Commons, Guava, and Joda-Time.
To run Modernizer, add the following to the `` stanza in your pom.xml
then invoke mvn modernizer:modernizer:
org.gaul
modernizer-maven-plugin
3.4.0
${maven.compiler.release}
The `` stanza can contain several elements:
target Java version, e.g., `8` or `1.8` — both forms are accepted for any release. Modernizer reports a violation only when its is at or below this value, so targeting Java 1.2 flags Vector but targeting Java 1.1 does not. Required parameter; binding it to ${maven.compiler.release} (or ${maven.compiler.target}) keeps it in sync with the rest of the build.user-specified violation files. Later files override violations from earlier ones, including and the default violations.java/lang/String.getBytes:(Ljava/lang/String;)[B. Empty lines and lines starting with # are ignored.java/lang/String.getBytes:(Ljava/lang/String;)[B.violation patterns to disable, specified using child elements. Each exclusion should be a regular expression that matches the javap format: java/lang/.* of a violation.package prefixes to ignore, specified using child elements. Specifying foo.bar subsequently ignores foo.bar.*, foo.bar.baz.* and so on.full qualified class names (incl. package) to ignore, specified using child elements. Each exclusion should be a regular expression that matches a package and/or class; the package will be / not . separated (ASM's format).lombok.addLombokGeneratedAnnotation = true. (Note: both javax.annotation.Generated and javax.annotation.processing.Generated have retention policy SOURCE (aka discarded by compiler).)Invoking mvn modernizer:modernizer runs the goal once. For automatic
checks during normal builds, bind it to a lifecycle phase via an
. For example, to run Modernizer during the verify phase, add the following to the modernizer stanza in your pom.xml:
modernizer
verify
modernizer
Command-line flags can override Modernizer configuration and ModernizerMojo documents all of these. The most commonly used flags:
-Dmodernizer.failOnViolations - fail phase if violations detected, defaults to true-Dmodernizer.skip - skip plugin execution, defaults to falseFor each violation Modernizer emits one line containing the source location
and the recommended replacement. Using the default CONSOLE format, output
looks like:
[ERROR] src/main/java/org/example/Foo.java:42: Prefer java.util.ArrayList<>()
[ERROR] src/main/java/org/example/Foo.java:57: Prefer java.nio.charset.StandardCharsets.UTF_8When `` is true (the default), the build fails after the
listing with the total violation count.
The plugin can output Modernizer violations in one of many formats which can be configured with the stanza using.
The currently supported formats and their respective configuration options are outlined below:
CONSOLE List each violation using Maven's logger. This is the default format.ERROR, WARN, INFO or DEBUG.
Default is ERROR.CODE_CLIMATE Write the violations according to Code Climate's Spec.
GitLab uses this format for its code quality as shown here.${project.build.directory}/code-quality.jsonINFO, MINOR, MAJOR, CRITICAL or BLOCKER.
Default is MINOR.Modernizer reads its rules from an XML file in the same format as the
bundled modernizer.xml.
Point at your own file to replace the defaults, or list additional files in to layer rules on top (later files
override earlier ones). Either path can be prefixed with classpath:/ to
load from the classpath.
Each `` element accepts:
java/util/Vector for a type, or
java/lang/String.getBytes:(Ljava/lang/String;)[B for a specific method
com/example/legacy/OldThing
11
Use com.example.modern.Thing instead
Code can suppress violations on a class (or other type declaration),
constructor, method, or type use via the @SuppressModernizer annotation.
First add the following dependency to your pom.xml:
org.gaul
modernizer-maven-annotations
3.4.0
Then annotate the element to ignore:
import org.gaul.modernizer_maven_annotations.SuppressModernizer;
@SuppressModernizer
public class Example {
@SuppressModernizer
public Example() { ... }
@SuppressModernizer
public static void method() { ... }
}Modernizer matches the annotation by simple name, so any @SuppressModernizer
in any package will suppress violations — the modernizer-maven-annotations
dependency above is a convenient canonical copy but is not required. Fields
and packages cannot be suppressed because the annotation's @Target does not
include them; use or instead.
javac -Xlint:deprecated detects uses of interfaces with @Deprecated annotationsCopyright (C) 2014-2026 Andrew Gaul
Licensed under the Apache License, Version 2.0
暂无开放 Issues,或尚未同步最近议题。