如何让java maven构建失败以获得编译器警告?

sim*_*905 17 java compiler-construction lint build maven

我在尝试:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <compilerArgument>-Werror</compilerArgument>
                <fork>true</fork>
            </configuration>
        </plugin>
Run Code Online (Sandbox Code Playgroud)

但没有快乐.现在有什么想法可以在这篇博客文章中提出的这些错误中世纪 ?

glt*_*lts 25

使用Maven 3.3和Java 8进行2015年更新.

这是一个最小的编译器配置,它可以启用所有警告,并在发生警告时使构建失败.

<plugins>
    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <showWarnings>true</showWarnings>
            <compilerArgs>
                <arg>-Xlint:all</arg>
                <arg>-Werror</arg>
            </compilerArgs>
        </configuration>
    </plugin>
</plugins>
Run Code Online (Sandbox Code Playgroud)

注意事项:

  • <showWarnings>true</showWarnings>是必须的.由于未知原因,Maven默认使用-nowarn标志主动抑制警告,因此将忽略-Xlint-Werror标志.
  • showDeprecation不需要启用,因为-Xlint:all已经发出弃用警告.
  • 实验表明fork,即使文档另有说明,也不需要启用.


mpa*_*lov 7

maven-compiler-plugin3.6.0中的新功能:failOnWarning标志。这对我有用:

  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.0</version>
    <executions>
      <execution>
        <id>compile</id>
        <phase>process-sources</phase>
        <goals>
          <goal>compile</goal>
        </goals>
        <configuration>
          <compilerArgument>-Xlint:-processing</compilerArgument>
          <failOnWarning>true</failOnWarning>
        </configuration>
      </execution>
    </executions>
  </plugin>
Run Code Online (Sandbox Code Playgroud)

请注意,我必须排除processing皮棉,否则自动事项的注释会因隐含的“找不到符号”错误而破坏构建。

  • 上面的答案是有效的,尽管我尝试使其在没有`-Xlint`设置的情况下也可以工作,但这并不成功。我发现`&lt;failOnWarning&gt; true &lt;/ failOnWarning&gt;`与`&lt;compilerArgs&gt; &lt;arg&gt; -Werror &lt;/ arg&gt;`相同。就其本身而言,-Werror不执行任何操作,因为它需要与-Xlint:all配对。因此,`&lt;failOnWarning&gt; true &lt;/ failOnWarning&gt;`本身也不做任何事情。 (2认同)

ale*_*x.p 2

编辑:这个答案已经过时,但我无法删除它,因为它是当时接受的答案。

这是 Maven 的一个错误,请参阅: https: //issues.apache.org/jira/browse/MCOMPILER-120它已在 Maven-compiler-plugin 2.4 中修复,但我不认为它已经发布。不幸的是,标签也不起作用。

  • 需要明确的是,这个错误不再是问题,并且问题中指定的形式效果很好。 (2认同)