如何使 Maven 构建(目标站点)因 Javadoc 警告而失败?

How to make Maven build (goal site) fail on Javadoc warnings?

我正在构建我的 Maven 项目,目标是 site。输出中有 Javadoc 警告。 在这种情况下,我的 Maven 构建必须失败。有办法吗?

这是我的 POM 代码片段(我使用的是 Maven 3.3):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-site-plugin</artifactId>
    <version>3.5</version>
    <configuration>
        <generateReports>true</generateReports>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-javadoc-plugin</artifactId>
    <version>2.10.3</version>
    <configuration>
        <show>private</show>
        <failOnError>true</failOnError>
    </configuration>
</plugin>

为此,您应该使用 Sonarqube. Here is no reason to check it as part of CI. So, this is job for SonarQube but if you want to still check something in Jenkinse, you can try to use Text Finder Plugin for Jenkinse 之类的东西。然后您可以设置包含 java 文档警告的任何独特部分的字符串,然后您的构建将被降级。

无法将 maven-javadoc-plugin 配置为在出现警告时使构建失败(仅在参数 failOnError 出现错误时)。

您真正想要的是使用 maven-checkstyle 插件。这是负责检查您的代码是否符合给定预定义样式的插件。在这种情况下,风格是 Javadoc 必须存在并且不能有警告。因此,像这样配置 Checkstyle 插件:

<plugin>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>2.17</version>
    <reportSets>
        <reportSet>
            <reports>
                <report>checkstyle</report>
            </reports>
        </reportSet>
    </reportSets>
    <configuration>
        <failsOnError>true</failsOnError>
        <configLocation>checkstyle.xml</configLocation>
    </configuration>
</plugin>

它引用了一个checkstyle.xml(相对于项目基目录)。要检查 Javadoc,您可以使用以下简单的 checkstyle 配置文件:

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
    "-//Puppy Crawl//DTD Check Configuration 1.2//EN"
    "http://www.puppycrawl.com/dtds/configuration_1_2.dtd">

<module name="Checker">
    <module name="TreeWalker">
        <module name="JavadocMethod"/>
        <module name="JavadocType"/>
        <module name="JavadocVariable"/>
        <module name="JavadocStyle"/>
    </module>
</module>

这将使构建因任何 Javadoc 警告而失败。 Javadoc 模块 are highly configurable;上面的示例配置将在每个方法、每个类型和每个变量上检查 Javadoc 及其正确性。

例如,您可以通过设置 scope property to the JavadocMethod and JavadocVariable 模块将其限制为仅 public 方法和 public 字段:

<module name="JavadocMethod">
    <property name="scope" value="public"/>
</module>
<module name="JavadocVariable">
    <property name="scope" value="public"/>
</module>

如果您在构建元素中为 maven-javadoc-plugin 使用以下代码片段,那么即使出现警告,它也会自动失败。

<executions>
    <execution>
        <id>attach-javadocs</id>
        <goals>
            <goal>jar</goal>
        </goals>
    </execution>
</executions>

3.0.1版插件添加了failOnWarnings选项,目前3.3.1版本仍然支持。您的配置应如下所示:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-javadoc-plugin</artifactId>
  <version>3.3.1</version>
  <executions>
    <execution>
      <id>attach-javadocs</id>
      <goals>
        <goal>jar</goal>
      </goals>
      <configuration>
        <failOnWarnings>true</failOnWarnings>
      </configuration>
    </execution>
  </executions>
</plugin>