java - 如何通过 pom 运行 pmd 而不会在 Jenkins 中构建失败?

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

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

最佳答案

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

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

关于java - 如何通过 pom 运行 pmd 而不会在 Jenkins 中构建失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47670861/

相关文章:

java - 如何使用 Liquibase 创建数据库

java - 使用 jsoup 将 HTML 解析为格式化的纯文本

jenkins - 列出在特定从节点上运行的所有作业

Jenkins htmlpublisher 问题

asp.net-core - 无法发布 NUnit 测试结果报告,收到错误消息,无法读取 XSL XML 文件

java - 通过 Collections.synchronizedSet(...).forEach() 进行的迭代是否保证是线程安全的?

java - 从注释中获取值(value)

java - android.media.audiofx.Visualizer 每隔一段时间抛出异常

Java实现htonl

java - NoClassDefFoundError:注释处理期间的 org/junit/AfterClass