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

时间:2016-04-08 14:18:06

标签: java jenkins gradle groovy jenkins-job-dsl

我使用Jenkins运行gradlew作为构建步骤。 build.gradle中有一个测试任务。当测试任务运行时,它会失败,因为它尝试编译的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注意"测试"部分。

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

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感到高兴?

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

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