如何在不失败Jenkins构建的情况下通过pom运行pmd?

时间:2017-12-06 09:30:37

标签: java maven jenkins pmd

我正在通过我的pom.xml中的maven插件运行pmd(checkstyle和findbugs)。由于pmd报告错误,Jenkins的构建失败。

构建阶段并行运行6个模块,之后我运行发布者和另一个阶段。如果pmd失败,则整个构建失败并立即停止。

这是我的pom.xml的一个片段:

</properties>
    <failOnChecks>true</failOnChecks>
</properties>
<!-- ...... -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-pmd-plugin</artifactId>
    <version>3.8</version>
    <dependencies>
        <dependency>
            <groupId>net.sourceforge.pmd</groupId>
            <artifactId>pmd-core</artifactId>
            <version>${version.pmd}</version>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.pmd</groupId>
            <artifactId>pmd-java</artifactId>
            <version>${version.pmd}</version>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.pmd</groupId>
            <artifactId>pmd-javascript</artifactId>
            <version>${version.pmd}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.pmd</groupId>
            <artifactId>pmd-jsp</artifactId>
            <version>${version.pmd}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>my.software</groupId>
            <artifactId>build-tools</artifactId>
            <version>${version.build-tools}</version>
        </dependency>
    </dependencies>
    <configuration>
        <rulesets>
            <ruleset>pmd/ruleset.xml</ruleset>
        </rulesets>
        <failOnViolation>${failOnChecks}</failOnViolation>
    </configuration>
    <executions>
        <execution>
            <phase>process-resources</phase>
            <goals>
                <goal>check</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Jenkinsfile有这两个阶段:

stage('modules') {
  gitlabCommitStatus('modules') {
    parallel Config.stepsForParallel
  }
}

stage('Jenkins Code Analysis') {
  pmd canRunOnFailed: true, canComputeNew: false, defaultEncoding: '', healthy: '', pattern: '', unHealthy: ''
  checkstyle canRunOnFailed: true, canComputeNew: false, defaultEncoding: '', healthy: '', pattern: '', unHealthy: ''
  //findbugs canComputeNew: false, defaultEncoding: '', excludePattern: '', healthy: '', includePattern: '', pattern: '', unHealthy: ''
}

在第二阶段到达之前,它在构建中失败。

插件不应该停止管道,而是完成它然后失败,然后运行发布者,所以我可以在Jenkins中看到问题。

到目前为止,我通过属性设置 failOnViolation ,但我无法让构建在最后失败。我想我需要检查那里的状态并调用错误。

有没有更清洁的方法来实现这一目标?

1 个答案:

答案 0 :(得分:2)

您可以使用目标pmd代替check,它将分析代码并生成报告,但不会使构建失败。然后配置Jenkin的PMD PluginStatic Code Analysis Plugin,将构建标记为失败或不稳定,具体取决于报告中的违规次数。

请注意,更改目标也会在手动运行时停止Maven构建失败。我们通常在<pluginManagement>中配置maven-pmd-plugin而不使用<executions>并创建两个Maven配置文件:运行maven-pmd-plugin且目标为check的默认配置文件和配置文件{目标为jenkins的{​​1}}。通过这种方式,开发人员可以手动运行构建,当PMD违规时它将失败,而在使用配置文件pmd的Jenkins上运行时它不会失败。