我使用 Jenkins 来运行 gradlew 作为构建步骤。 build.gradle
中有一个测试任务。当测试任务运行时,它会失败,因为它尝试编译的 Groovy 文件顶部有 5 个 import
语句。这些类是我在子文件夹中创建的类。但是,当我使用 Job DSL 插件使用相同的 Groovy 文件创建 Jenkins 作业时,它运行得很好。以下是我看到的错误:
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 满意?
最佳答案
将这些源代码放在 buildSrc/src/main/groovy
下,而不是 jobs/utilities
这是您的自定义构建源的位置,Gradle 将自动构建,并将在您的构建脚本中提供。用户指南对此进行了详细描述 - https://docs.gradle.org/current/userguide/custom_plugins.html
关于java - Jenkins 未通过 gradle 测试任务,找不到导入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36501980/