java - -Xlint :rawtypes not working in Maven

标签 java maven

我有这门课,在src/main/java/RawTypes.java :

import java.util.*;

public class RawTypes {
  private List raw;

  public void sortRaw() {
    raw.addAll(raw);
  }
}

如果我运行 javac -Xlint src/main/java/RawTypes.java ,我收到 2 个警告:一个 rawtypes警告和 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

这是我所期望的。

这是我的 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>

现在,当我运行 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] ------------------------------------------------------------------------

但我没有得到 rawtypes警告。

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

这是怎么回事?我应该怎么做才能获得 rawtypes毕竟是警告?

最佳答案

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

如果你测试

javac -Xlint -nowarn src/main/java/RawTypes.java

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

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

关于java - -Xlint :rawtypes not working in Maven,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35247099/

相关文章:

java - EditText 中的 ClickableSpan 到文本末尾调用 click() 到行尾

java - jsp页面中如何像gridview这样的多行记录中删除单条记录

java - 在 Java 中根据可用内存管理线程数

java - IntelliJ : Automatic artifact discovery for an executable jar

java - 无法运行Spring maven项目,如何解决类路径包含多个SLF4J绑定(bind)?

java - 如何从字符串中提取值

java - 如何从 C++/Qt 调用 QtActivity 中的非静态 Java 方法

java - 将 *.jar 导入 Maven 项目时遇到问题

java - maven-shade-plugin 错误 : Cannot find setter, 加法器或 org.apache.maven.plugins.shade.resource.ManifestResourceTransformer 中的字段 'resource'

maven - Gradle中的Maven父POM依赖管理