【问题标题】:How to run pmd through pom without failing the build in Jenkins?如何通过 pom 运行 pmd 而不会在 Jenkins 中构建失败?
【发布时间】:2017-12-06 09:30:36
【问题描述】:

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

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

这是来自我的 pom.xml 的 sn-p:

</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,但我无法让构建最终失败。我想我需要在某处检查状态并调用错误。

有没有更简洁的方法来实现这一点?

【问题讨论】:

  • 首先为什么要定义插件的依赖关系?此外,您是否检查过通过以下方式致电 Maven:mvn --fail-at-end ...
  • 1:这是由一位同事完成的。我想他这样做是有他的理由的。我可能可以删除它们。 2:--fail-at-end 可能不起作用,因为每个构建都运行自己的 mvn 命令。 --fail-never 可能也不是那么好,因为它也可能抑制任何其他失败。
  • 将这些依赖项添加到插件中并没有真正意义,因为插件已经具有这些依赖项......您是否使用 CI 解决方案来运行这些构建?如果是,您可以更改 Maven 调用的配置。
  • -fae 不会停止管道。如果您构建有问题,则管道将停止。如果在这种情况下需要继续,则需要在管道中缓存错误...
  • 当您想要运行最新版本的 PMD 时,这些依赖项才有意义。 maven-pmd-plugin 通常落后于几个 PMD 版本,因为它的更新频率低于 PMD 本身。截至目前,maven-pmd-plugin 使用 PMD 5.6.1,而 PMD 的最新版本是 5.8.1。

标签: java maven jenkins pmd


【解决方案1】:

您可以使用目标pmd 而不是check,它将分析代码并生成报告,但不会使构建失败。然后配置 Jenkins 的 PMD PluginStatic Code Analysis Plugin 以根据报告中的违规数量将构建标记为失败或不稳定。

请注意,更改目标也会在手动运行时停止 Maven 构建失败。我们通常在 &lt;pluginManagement&gt; 中配置 maven-pmd-plugin 而不使用 &lt;executions&gt; 并创建两个 Maven 配置文件:一个运行 maven-pmd-plugin 的默认配置文件,目标为 check 和配置文件 jenkins,目标为 @ 987654329@。通过这种方式,开发人员可以手动运行构建,并且在违反 PMD 时会失败,而在使用配置文件 jenkins 的 Jenkins 上运行时不会失败。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    • 2019-10-17
    • 2016-08-13
    • 2019-09-20
    相关资源
    最近更新 更多