• Stars
    star
    341
  • Rank 119,714 (Top 3 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 9 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Detect uses of legacy Java APIs

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:

  • <javaVersion> enables violations based on target Java version, e.g., 8. For example, Modernizer will detect uses of Vector as violations when targeting Java 1.2 but not when targeting Java 1.1. Required parameter.
  • <failOnViolations> fail phase if Modernizer detects any violations. Defaults to true.
  • <includeTestClasses> run Modernizer on test classes. Defaults to true.
  • <violationsFile> user-specified violation file. Also disables standard violation checks. Can point to classpath using absolute paths, e.g. classpath:/your/file.xml.
  • <violationsFiles> user-specified violations file. The latter files override violations from the former ones, including violationsFile and the default violations. Can point to classpath using absolute paths, e.g. classpath:/your/file.xml.
  • <exclusionsFile> 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.
  • <exclusions> violations to disable. Each exclusion should be in the javap format: java/lang/String.getBytes:(Ljava/lang/String;)[B.
  • <exclusionPatterns> violation patterns to disable, specified using <exclusionPattern> child elements. Each exclusion should be a regular expression that matches the javap format: java/lang/.* of a violation.
  • <ignorePackages> package prefixes to ignore, specified using <ignorePackage> child elements. Specifying foo.bar subsequently ignores foo.bar.*, foo.bar.baz.* and so on.
  • <ignoreClassNamePatterns> full qualified class names (incl. package) to ignore, specified using <ignoreClassNamePattern> 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).
  • <ignoreGeneratedClasses> classes annotated with an annotation whose retention policy is runtime or class and whose simple name contain "Generated" will be ignored. (Note: both javax.annotation.Generated and javax.annotation.processing.Generated have retention policy SOURCE (aka discarded by compiler).)

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:

  • -Dmodernizer.failOnViolations - fail phase if violations detected, defaults to true
  • -Dmodernizer.skip - skip plugin execution, defaults to false

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:

  • CONSOLE List each violation using Maven's logger. This is the default format.
    • <violationLogLevel> 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.
    • <outputFile> The full path the file to output to. Default is ${project.build.directory}/code-quality.json
    • <codeClimateSeverity> Severity of Modernizer violations for CodeClimate: INFO, MINOR, MAJOR, CRITICAL or BLOCKER. Default is MINOR.

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

More Repositories

1

s3proxy

Access other storage backends via the S3 API
Java
1,604
star
2

awesome-ld-preload

List of resources related to LD_PRELOAD, a mechanism for changing application behavior at run-time
794
star
3

are-we-consistent-yet

Compare eventual consistency of object stores
Java
170
star
4

object-store-comparison

Compare cost, durability, and region support of public cloud object stores, e.g., Amazon S3
HTML
69
star
5

undocumented-s3-apis

Undocumented Amazon S3 APIs and third-party extensions
66
star
6

java-httpbin

A Java-based HTTP server that lets you locally test your HTTP client, retry logic, streaming behavior, timeouts, etc. with the endpoints of httpbin.org
Java
35
star
7

bay-area-transit-plans

San Francisco Bay Area Transit Plans
19
star
8

java-collection-overhead

Demonstrate overheads for various Java Collection implementations.
Java
17
star
9

libinput-force-middle-click-emulation

force libinput to emulate a middle click when pressing left and right buttons simultaneously
C
13
star
10

github-release-rss-feed

Watch starred GitHub repositories for new releases.
Python
7
star
11

mirrorfs

Mirror filesystem operations and check for consistency
C
6
star
12

x86lint

Examine x86 machine code to find suboptimal encodings and sequences
C
6
star
13

gkeep-fuse

FUSE interface for Google Keep
Python
6
star
14

yass-android

Yet Another Sync Software for Android
Java
4
star
15

libnoxattr

An LD_PRELOAD library that disables all forms of extended attribute access
C
4
star
16

desktop-everywhere

WebExtension which redirects mobile links to desktop equivalents
JavaScript
3
star
17

ptools

parallel implementations of Unix tools
Go
1
star
18

loudest-tweeters

show all the Twitter profiles in your timeline, sorted by number of tweets
Go
1
star
19

fatback

A *nix tool for recovering files from FAT file systems.
C
1
star