简体   繁体   English

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

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

I am building my Maven project with the goal site . 我正在使用目标site构建我的Maven项目。 There are Javadoc warnings in the output. 输出中有Javadoc警告。 In this case my Maven build has to fail. 在这种情况下,我的Maven构建必须失败。 Is there a way to do that? 有没有办法做到这一点?

Here is the code snippet of my POM (I am using Maven 3.3): 这是我的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>

The maven-javadoc-plugin cannot be configured to fail the build on warnings (only on errors with the parameter failOnError ). maven-javadoc-plugin不能配置为在构建警告时失败(仅限于带有参数failOnError错误)。

What you actually want is to use the maven-checkstyle plugin. 你真正想要的是使用maven-checkstyle插件。 This is the plugin that is responsible for checking that your code complies to a given predefined style. 这是负责检查您的代码是否符合给定预定义样式的插件。 In this case, the style is that Javadoc must be present and must not have warnings. 在这种情况下,样式是Javadoc必须存在且不得有警告。 As such, configure the Checkstyle Plugin like this: 因此,配置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>

It references a checkstyle.xml (located relative to the project base directory). 它引用了checkstyle.xml (相对于项目基目录)。 To check for Javadoc, you could have the following simple checkstyle configuration file: 要检查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>

This will make the build fail for any Javadoc warnings. 这将使任何Javadoc警告的构建失败。 The Javadoc module are highly configurable ; Javadoc模块具有高度可配置性 ; the sample configuration above will check for Javadoc and its correctness, on every method, every type and every variable. 上面的示例配置将在每个方法,每个类型和每个变量上检查Javadoc及其正确性。

As an example, you can restrict this to only public methods and public fields by setting the scope property to the JavadocMethod and JavadocVariable modules: 例如,您可以通过将scope属性设置为JavadocMethodJavadocVariable模块,将其限制为仅public方法和public字段:

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

For this, you should use something like Sonarqube . 为此,你应该使用像Sonarqube这样的东西 Here is no reason to check it as part of CI. 这里没有理由将其作为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 . 所以,这是SonarQube的工作,但如果你想在Jenkinse中查看一些内容,你可以尝试使用Jenkinse的Text Finder插件 Then you are able to set string containing any uniqeu part of java doc warning and then your build will be downgraded. 然后,您可以设置包含java doc警告的任何uniqeu部分的字符串,然后您的构建将被降级。

If you use following snippet for maven-javadoc-plugin in build element then it will automatically fail even for warnings. 如果您在构建元素中使用以下代码段用于maven-javadoc-plugin ,那么即使是警告也会自动失败。

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

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

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