如何使依赖于某个 jar 的 Jenkins 构建失败

How to fail a build in Jenkins dependent on a certain jar

如果在 Java Maven 项目中使用某个 jar,是否有办法使 Jenkins 中的构建失败?

例如,我知道 org.example:badartifact:1.0.1 存在安全漏洞。我把这件事告诉了每个人,他们修复了他们的项目......,但也许一些第三方工件将其作为传递性带来,但没有人意识到这一点。

或者也许有人忘记了这个老错误...

所以我想最好在 Jenkins 中进行最后一次检查,这样我们就不会以包含该特殊工件的项目结束。

你如何处理这样的情况,你使用什么工具? (白名单库?黑名单库?等)

如有任何建议,我们将不胜感激。

可能的 Maven 解决方案
你可以有一个公司超级 POM(parent POM of all Maven projects within the company/department/team) and in that super POM configure the Maven Enforcer Plugin, its bannedDependencies rule 禁止任何库、版本甚至范围。我个人使用这个选项,即使是微不足道的错误(即 junit 不在测试范围内会使构建失败)。

此解决方案是集中式解决方案,因此更易于维护,但要求所有项目都具有相同的父 POM,并且开发人员可以随时更改父 pom,因此可以跳过此治理。另一方面,集中式父 POM 对于依赖管理、通用配置文件、报告等非常有用。

注意:您不能通过默认配置文件在 Jenkins 服务器的 Maven 设置中配置它,例如,为了将它应用于所有 运行 Maven 构建,因为 Maven limits 在设置提供的配置文件中自定义构建(这是一种设计选择,以限制外部影响,因此更容易进行故障排除)。以前试过,碰壁

Profiles in external files

Profiles specified in external files (i.e in settings.xml or profiles.xml) are not portable in the strictest sense. Anything that seems to stand a high chance of changing the result of the build is restricted to the inline profiles in the POM. Things like repository lists could simply be a proprietary repository of approved artifacts, and won't change the outcome of the build. Therefore, you will only be able to modify the and sections, plus an extra section

可能的 Jenkins 解决方案
如果您想直接在 Jenkins 中集中管理,从而独立于 Maven 构建,我在过去应用了这些解决方案(它们非常有效):

  • Jenkins Text Finder Plugin:如果在构建输出中发现正则表达式或匹配文本,您可以使构建失败。在您的情况下,您可以让 Jenkins 构建步骤始终执行 mvn dependency:tree,因此构建输出的一部分是依赖项列表(甚至是可传递的)。与您禁止的依赖项匹配的文本查找器规则将匹配它并导致构建失败。
  • Fail The Build Jenkins Plugin:与上面的类似,但集中管理配置的故障原因。同样,失败基于匹配的文本,但不需要构建配置:默认情况下它将应用于所有构建。

这是完成这项工作的一种解决方案:)

使用 Maven License plugin,您可以扫描 Maven 项目的第 3 方依赖项并生成 THIRD_PARTY.txt 报告(在 target/generated-sources/license 文件夹中)。

Maven 命令行:

mvn license:aggregate-add-third-party

接下来,您可以使用TextFinder plugin搜索THIRD_PARTY.txt文件中的"unsafe"依赖项(例如:org.example:badartifact:1.0.1)并更改构建状态(如果需要)。

另一个解决方案是使用第 3 方工具来做到这一点。

我正在调查这个:http://www.whitesourcesoftware.com/

此工具可以提供存在漏洞问题的第 3 方依赖项列表。