百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
M

modernizer-maven-plugin

> 包管理
开源

检测遗留Java API的用途

390 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

检测遗留Java API的用途

Modernizer Maven Plugin

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.sort
  • String.getBytes(String) instead of String.getBytes(Charset)
  • Guava 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.

Requirements

  • Maven 3.6.3 or newer
  • JDK 8 or newer to run the plugin
  • JDK 17 or newer to build the plugin from source

Configuration

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

xml

  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.
  • `` fail phase if Modernizer detects any violations. Defaults to true.
  • `` run Modernizer on test classes. Defaults to true.
  • `` user-specified violation file. Also disables standard violation checks. See Custom violations below.
  • user-specified violation files. Later files override violations from earlier ones, including and the default violations.
  • `` disables user-specified violations. This is a text file with one exclusion per line in the javap format: java/lang/String.getBytes:(Ljava/lang/String;)[B. Empty lines and lines starting with # are ignored.
  • `` violations to disable. Each exclusion should be in the javap format: 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).
  • `` classes annotated with an annotation whose retention policy is runtime or class and whose simple name is exactly "Generated" will be ignored. Individual methods and constructors carrying such an annotation are also ignored, e.g., members generated by Lombok when 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:

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 false

Output Formats

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

When `` 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.
    • `` Specify the log level of the logger: 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.
    • `` The full path the file to output to. Default is ${project.build.directory}/code-quality.json
    • `` Severity of Modernizer violations for CodeClimate: INFO, MINOR, MAJOR, CRITICAL or BLOCKER. Default is MINOR.

Custom violations

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:

  • `` API in javap format: java/util/Vector for a type, or java/lang/String.getBytes:(Ljava/lang/String;)[B for a specific method
  • `` lowest target Java version at which the API becomes legacy
  • `` recommended replacement, shown in violation output
  • `` (optional) target Java version, exclusive, at which the rule stops applying — useful when a newer replacement supersedes an older one
xml

  
    com/example/legacy/OldThing
    11
    Use com.example.modern.Thing instead
  

Ignoring elements

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:

xml

  
    org.gaul
    modernizer-maven-annotations
    3.4.0
  

Then annotate the element to ignore:

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

References

  • ASM provides Java bytecode introspection which enables Modernizer's checks
  • Checkstyle IllegalInstantiation and Regexp checks can mimic some of Modernizer's functionality
  • Google Error Prone JdkObsolete can mimic some of Modernizer's functionality
  • Gradle Modernizer Plugin provides a Gradle interface to Modernizer
  • javac -Xlint:deprecated detects uses of interfaces with @Deprecated annotations
  • Overstock.com library-detectors detects uses of interfaces with @Beta annotations
  • Policeman's Forbidden API Checker provides similar functionality to Modernizer

License

Copyright (C) 2014-2026 Andrew Gaul

Licensed under the Apache License, Version 2.0

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Javaapache-commonsguavajavajoda-time

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类包管理
定价开源

> 相关工具

N
npm
Node.js 默认包管理器
P
pnpm
高效节省磁盘的 Node 包管理器
I
is-gif
Check if a Buffer/Uint8Array is a GIF image