Maven 如何在编译时显示代码警告

Rus*_* Yu 7 java maven

我正在使用 maven 构建一个新项目。我的代码中有一些警告,用黄线标出。我希望 maven 可以在控制台中报告这些警告。我怎样才能做到这一点?我可以在命令中添加一些参数,例如 mvn -XXX clean compile 吗?

Lus*_*116 8

<showDeprecation>
设置是否显示使用已弃用 API 的源位置。

  • 默认值为: false
  • 用户属性是: maven.compiler.showDeprecation

<showWarnings>
设置为true显示编译警告。

  • 默认值为: false
  • 用户属性是: maven.compiler.showWarnings

——马文·杜库

就像mvn clean install -Dmaven.compiler.showDeprecation=true -Dmaven.compiler.showWarnings=true一行一样简单。

您甚至可以删除该=true部分,因为当您添加带有-D它的 Maven 参数时,它会自动将其值设置为true,如果未设置为其他值。


Gle*_*son 6

对于 maven 3.3.9,使用 maven-compiler-plugin 和 Java 1.8,您需要<configuration>如下所示的部分:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <executions>
        <execution>
            <id>compile</id>
            <phase>compile</phase>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
        <execution>
            <id>testCompile</id>
            <phase>test-compile</phase>
            <goals>
                <goal>testCompile</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <compilerArgument>-Xlint:all</compilerArgument>
        <showWarnings>true</showWarnings>
        <showDeprecation>true</showDeprecation>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

当上述方法都不适合我时,我从这里得到了这个答案:http://frequal.com/java/EnableWarningsInMaven.html


Vit*_*liy 0

我猜你正在使用编译器插件。你试过标签吗