简体   繁体   English

如何让 Java maven 构建因编译器警告而失败?

[英]How do I get a Java maven build to fail for compiler warnings?

I am trying:我在尝试:

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

but with no joy.但没有喜悦。 Any ideas now to get medieval on such errors as suggested at this blog post ?现在有什么想法可以解决此博客文章中建议的此类错误?

Update for the year 2015, using Maven 3.3 and Java 8. 2015 年更新,使用 Maven 3.3 和 Java 8。

Here's a minimal compiler configuration that enables all warnings and makes the build fail whenever warnings occur.这是一个最小的编译器配置,它启用所有警告并在出现警告时使构建失败。

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

Bits of note:注意事项:

  • <showWarnings>true</showWarnings> is required. <showWarnings>true</showWarnings>是必需的。 For reasons unknown, Maven by default actively suppresses warnings with the -nowarn flag, so the -Xlint and -Werror flags would be ignored.由于未知的原因,Maven 在默认情况下使用-nowarn标志主动抑制警告,因此-Xlint-Werror标志将被忽略。
  • showDeprecation doesn't need to be enabled because -Xlint:all already emits deprecation warnings. showDeprecation不需要启用,因为-Xlint:all已经发出弃用警告。
  • Experimentation shows that fork doesn't need to be enabled, even though the documentation says otherwise.实验表明,即使文档另有说明,也不需要启用fork

New in maven-compiler-plugin 3.6.0: the failOnWarning flag. maven-compiler-plugin 3.6.0 中的新功能: failOnWarning标志。 This worked for me:这对我有用:

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

Note that I had to exclude the processing lint or otherwise auto-matter's annotations would break the build with cryptic "symbol not found" errors.请注意,我必须排除processing lint 或以其他方式自动processing的注释会因神秘的“未找到符号”错误而破坏构建。

An update on 2020, I am using Spring Boot 2.2.5. 2020 年的更新,我使用的是 Spring Boot 2.2.5。 The following config can stop the mvn install when warning, error happen.以下配置可以在警告、错误发生时停止mvn install

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <failOnError>true</failOnError>
        <failOnWarning>true</failOnWarning>
    </configuration>
 </plugin>

EDIT: This answer is outdated however I can't delete it as it was an accepted answer at the time.编辑:这个答案已经过时了,但是我不能删除它,因为它当时是一个被接受的答案。

This a bug with Maven see: https://issues.apache.org/jira/browse/MCOMPILER-120 it's been fixed in 2.4 of the Maven-compiler-plugin but I don't believe that's been released yet.这是 Maven 的一个错误,请参阅: https : //issues.apache.org/jira/browse/MCOMPILER-120它已在 Maven-compiler-plugin 的 2.4 中修复,但我认为尚未发布。 tag won't work either unfortunately.不幸的是,标签也不起作用。

I arrived at this post when I was looking for a working solution to fail my maven builds for compiler warnings当我正在寻找一个可行的解决方案来使我的 Maven 构建失败以显示编译器警告时,我来到了这篇文章

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <source>${java.version}</source>
        <target>${java.version}</target>
        <fork>true</fork>
        <compilerArgs>
            <arg>-verbose</arg>
            <arg>-Werror</arg> 
            <arg>-Xlint:all</arg>
        </compilerArgs>
    </configuration>
</plugin>

For more explanation in detail please refer the link below有关详细说明,请参阅以下链接

Treat warnings As Errors 将警告视为错误

There is an alternate form perhaps give it a try?有一种替代形式也许可以尝试一下? Note the s on the end of <compilerArguments>注意<compilerArguments>末尾的 s

<configuration>
    <compilerArguments>
        <Werror />
    </compilerArguments>
</configuration>

By using the workaround in this comment in the open jira issue for maven compiler plugin, the build can be failed for compiler warning.通过在 maven 编译器插件的open jira 问题中使用此注释中的解决方法,构建可能会因编译器警告而失败。

This works for me:这对我有用:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
            <compilerId>javac</compilerId>
            <source>1.6</source>
            <target>1.6</target>
            <compilerArgument>-Werror</compilerArgument>
            <showDeprecation>true</showDeprecation>
        </configuration>

        <dependencies>
           <dependency>
                <groupId>org.codehaus.plexus</groupId>
                <artifactId>plexus-compiler-api</artifactId>
                <version>1.8.2</version>
                <exclusions>
                  <exclusion>
                    <groupId>org.codehaus.plexus</groupId>
                    <artifactId>plexus-component-api</artifactId>
                  </exclusion>
                </exclusions>
           </dependency>
           <dependency>
                <groupId>org.codehaus.plexus</groupId>
                <artifactId>plexus-compiler-manager</artifactId>
                <version>1.8.2</version>
                <exclusions>
                  <exclusion>
                    <groupId>org.codehaus.plexus</groupId>
                    <artifactId>plexus-component-api</artifactId>
                  </exclusion>
                </exclusions>
           </dependency>
           <dependency>
                <groupId>org.codehaus.plexus</groupId>
                <artifactId>plexus-compiler-javac</artifactId>
                <version>1.8.2</version>
                <scope>runtime</scope>
                <exclusions>
                  <exclusion>
                        <groupId>org.codehaus.plexus</groupId>
                        <artifactId>plexus-component-api</artifactId>
                   </exclusion>
                </exclusions>
          </dependency>
       </dependencies>
    </plugin>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM