-Xlint:rawtypes在Maven中不起作用

jqn*_*qno 5 java maven

我在src/main/java/RawTypes.java

import java.util.*;

public class RawTypes {
  private List raw;

  public void sortRaw() {
    raw.addAll(raw);
  }
}
Run Code Online (Sandbox Code Playgroud)

如果运行javac -Xlint src/main/java/RawTypes.javarawtypes则会收到2条警告:警告和unchecked警告:

src/main/java/RawTypes.java:4: warning: [rawtypes] found raw type: List
      private List raw;
              ^
  missing type arguments for generic class List<E>
  where E is a type-variable:
    E extends Object declared in interface List
src/main/java/RawTypes.java:7: warning: [unchecked] unchecked call to addAll(Collection<? extends E>) as a member of the raw type List
        raw.addAll(raw);
                  ^
  where E is a type-variable:
    E extends Object declared in interface List
2 warnings
Run Code Online (Sandbox Code Playgroud)

这是我所期望的。

这是我的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>nl.jqno.rawtypes</groupId>
    <artifactId>rawtypes</artifactId>
    <version>0.1-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <encoding>UTF-8</encoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5</version>
                <configuration>
                    <compilerArgument>-Xlint</compilerArgument>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
Run Code Online (Sandbox Code Playgroud)

现在,当我运行时mvn clean compile,我只会得到unchecked警告:

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building rawtypes 0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ rawtypes ---
[INFO] Deleting /Users/jqno/javarawtypes/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ rawtypes ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/jqno/javarawtypes/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.5:compile (default-compile) @ rawtypes ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/jqno/javarawtypes/target/classes
[WARNING] /Users/jqno/javarawtypes/src/main/java/RawTypes.java:[7,19] unchecked call to addAll(java.util.Collection<? extends E>) as a member of the raw type java.util.List
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.903 s
[INFO] Finished at: 2016-02-06T23:03:37+01:00
[INFO] Final Memory: 15M/267M
[INFO] ------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

但是我没有得到rawtypes警告。

我正在运行OS X,这在Java 1.7.0_79和1.8.0_45上都发生。我已经试过各种版本maven-compiler-plugin与指定参数的各种组合(<compilerArgument><compilerArgs>-Xlint-Xlint:all-Xlint:rawtypes),但他们没有工作。

这是怎么回事?到底该怎么做才能得到rawtypes警告?

Tun*_*aki 5

您需要明确设置showWarningstrue发出第一个警告。这是因为Maven默认将-nowarn选项添加到javac,从而禁用了第一个警告。

如果用

javac -Xlint -nowarn src/main/java/RawTypes.java
Run Code Online (Sandbox Code Playgroud)

您会看到不再发出第一个警告。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5</version>
    <configuration>
        <compilerArgument>-Xlint</compilerArgument>
        <showWarnings>true</showWarnings>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)