【问题标题】:How to compile using -Xlint:unchecked in a Maven project?如何在 Maven 项目中使用 -Xlint:unchecked 进行编译?
【发布时间】:2012-09-09 01:53:08
【问题描述】:

在 NetBeans 7.2 中,我无法找到如何在 Maven 项目中使用 -Xlint:unchecked 进行编译。在 Ant 项目下,您可以通过转到 Project Properties -> Compiling 来更改编译器标志,但 Maven 项目似乎没有任何此类选项。

有没有办法配置 IDE 以使用 Maven 使用此类标志进行编译?

【问题讨论】:

  • 如果您需要传递多个参数,您可能会收到<compilerArgument> 的错误。看到这个答案替代<compilerArgs><arg></arg>...<compilerArgs>stackoverflow.com/a/23743186/257299

标签: java maven netbeans


【解决方案1】:

这对我有用...

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
            <source>11</source>
            <target>11</target>
            <compilerArguments>
                <endorseddirs>${endorsed.dir}</endorseddirs>
            </compilerArguments>
            <compilerArgs>
                <arg>-Xlint:unchecked</arg>   <-------this right here ---->
            </compilerArgs>
        </configuration>
    </plugin>
</plugins>

【讨论】:

    【解决方案2】:

    我想详细说明@Nishant 的回答。 compilerArgument 标签需要进入 plugin/configuration 标签内。这是一个完整的例子:

    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <testSource>1.8</testSource>
          <testTarget>1.8</testTarget>
          <compilerArgument>-Xlint:unchecked</compilerArgument>
        </configuration>
      </plugin>
    </plugins>
    

    【讨论】:

    • 提供需要进入的上下文参数是有用答案的重要组成部分。谢谢!
    【解决方案3】:

    pom 文件信息很准确。我遇到了在 Jenkins 中构建别人的 Maven 项目并且无法访问 pom 文件存储库的额外挑战。

    我创建了一个预编译步骤,例如从git下载后将编译器参数插入到pom文件中

    sed -i 's|/target> *$|/target>\n<compilerArgument>\n-Xlint:deprecation\n</compilerArgument>|' $WORKSPACE/pom.xml
    

    【讨论】:

    • 错误的解决方案。分叉项目。并向他们发送包含更改的拉取请求。
    【解决方案4】:

    我猜你可以在你的 pom.xml 中设置编译器参数。请参考这个http://maven.apache.org/plugins/maven-compiler-plugin/examples/pass-compiler-arguments.html

     <compilerArgument>-Xlint:unchecked</compilerArgument>
    

    【讨论】:

    • 我创建了一个小测试程序,它应该生成关于使用静态方法的警告,但我似乎无法让 maven 生成任何关于它的警告。此处发布示例程序和 pom 文件的要点 -> gist.github.com/influenza/5145598
    • @RonDahlgren:你为什么希望这会引发警告?
    • @haylem - 访问静态字段或方法应该会在启用的情况下生成警告。这是正在使用的 javac 的详细信息,但很常见:help.eclipse.org/juno/…pic.dhe.ibm.com/infocenter/dstudio/v3r1/…
    • @RonDahlgren:是的,这是一个常见的警告,但它不是 javac 报告的。它由 Eclipse 编译器和其他一些编译器以及许多其他代码样式检查器报告,但 javac 不会报告它。如果您愿意,您可以使用 Maven 编译器来代替。
    • 链接页面建议 条目应该放在 组中。这似乎合乎逻辑。根据下面的链接,它实际上位于 组中。 stackoverflow.com/questions/8215781/…
    猜你喜欢
    • 2012-01-03
    • 2012-06-05
    • 2015-12-21
    • 2016-10-01
    • 1970-01-01
    • 2015-06-04
    • 2015-06-02
    • 1970-01-01
    • 2014-07-17
    相关资源
    最近更新 更多