Jenkins 未通过 gradle 测试任务,无法找到导入文件

Jenkins failing gradle test task, can't find import files

我正在使用 Jenkins 运行 gradlew 作为构建步骤。 build.gradle里面有一个测试任务。当测试任务 运行s 时,它失败了,因为它试图编译的 Groovy 文件顶部有 5 个 import 语句。这些 类 是我在子文件夹中创建的。但是,当我使用 Job DSL 插件使用相同的 Groovy 文件创建 Jenkins Jobs 时,它 运行 就很好了。以下是我看到的错误:

09:32:31.930 [DEBUG] [TestEventLogger] debug.JobScriptsSpec > test script JenkinsJobs.groovy FAILED
09:32:31.930 [DEBUG] [TestEventLogger]     Expected no exception to be thrown, but got 'javaposse.jobdsl.dsl.DslException'
09:32:31.930 [DEBUG] [TestEventLogger]         at spock.lang.Specification.noExceptionThrown(Specification.java:118)
09:32:31.930 [DEBUG] [TestEventLogger]         at debug.JobScriptsSpec.test script #file.name(JobScriptsSpec.groovy:31)
09:32:31.930 [DEBUG] [TestEventLogger] 
09:32:31.930 [DEBUG] [TestEventLogger]         Caused by:
09:32:31.930 [DEBUG] [TestEventLogger]         javaposse.jobdsl.dsl.DslException: startup failed:
09:32:31.945 [DEBUG] [TestEventLogger]         script: 1: unable to resolve class utilities.GitConfiguration
09:32:31.945 [DEBUG] [TestEventLogger]          @ line 1, column 1.
09:32:31.945 [DEBUG] [TestEventLogger]            import utilities.GitConfiguration
09:32:31.945 [DEBUG] [TestEventLogger]

我正在模拟这里所做的事情:https://github.com/sheehan/job-dsl-gradle-example 请注意 "Testing" 部分。

我已经尝试添加到类路径中执行以下操作,但我仍然遇到相同的错误:

buildscript {
    dependencies {
        classpath files('/jobs/utilities')
    }
}

我的基本文件结构是这样的:

jenkins-jobs
  |-build.gradle
  |-jobs
     |-JenkinsJobs.groovy
         |-utilities
             |-GitConfiguration.groovy
             |-other groovy files I try to import

简而言之,我想问的问题是,在我的 Groovy 文件中尝试包含用户创建的导入时,如何让 Gradle 开心?

对于你的情况,我建议从 git clone job-dsl-gradle-example 开始,然后从那里开始工作。

而不是 jobs/utilities 将这些来源放在 buildSrc/src/main/groovy

这是您的自定义构建源的位置,Gradle 将自动构建它,并将在您的构建脚本中提供。此处的用户指南对此进行了详细描述 -https://docs.gradle.org/current/userguide/custom_plugins.html