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

发布于 2025-01-03 14:12:36 字数 705 浏览 2 评论 0原文

我正在努力:

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

但没有快乐。现在有任何关于此类错误的想法,如 这篇博文中所建议的

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

回忆凄美了谁 2025-01-10 14:12:36

2015 年更新,使用 Maven 3.3 和 Java 8。

这是一个最小的编译器配置,可以启用所有警告并使得
每当出现警告时构建都会失败。

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

值得注意的是:

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

Update for the year 2015, using Maven 3.3 and 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. For reasons unknown, Maven by default actively suppresses warnings with the -nowarn flag, so the -Xlint and -Werror flags would be ignored.
  • showDeprecation doesn't need to be enabled because -Xlint:all already emits deprecation warnings.
  • Experimentation shows that fork doesn't need to be enabled, even though the
    documentation says otherwise.
吐个泡泡 2025-01-10 14:12:36

maven-compiler-plugin 3.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>

请注意,我必须排除 processing lint,否则自动事项的注释会破坏构建,并出现神秘的“符号未找到”错误。

New in maven-compiler-plugin 3.6.0: the failOnWarning flag. 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.

So尛奶瓶 2025-01-10 14:12:36

2020年的更新,我使用的是Spring Boot 2.2.5。以下配置可以在出现警告、错误时停止mvn install

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

An update on 2020, I am using Spring Boot 2.2.5. The following config can stop the mvn install when warning, error happen.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <failOnError>true</failOnError>
        <failOnWarning>true</failOnWarning>
    </configuration>
 </plugin>
夜深人未静 2025-01-10 14:12:36

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

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

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. tag won't work either unfortunately.

红颜悴 2025-01-10 14:12:36

有另一种形式也许可以尝试一下?请注意 末尾的 s

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

There is an alternate form perhaps give it a try? Note the s on the end of <compilerArguments>

<configuration>
    <compilerArguments>
        <Werror />
    </compilerArguments>
</configuration>
旧伤慢歌 2025-01-10 14:12:36

通过使用 此评论针对 maven 编译器插件打开 jira 问题,构建可能因编译器警告而失败。

这对我有用:

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

By using the workaround in this comment in the open jira issue for maven compiler plugin, the build can be failed for compiler warning.

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>
忱杏 2025-01-10 14:12:36

当我正在寻找一个可行的解决方案来使我的 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>

有关更多详细说明,请参阅下面的链接

将警告视为错误

I arrived at this post when I was looking for a working solution to fail my maven builds for compiler warnings

<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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文