我有一个 Gradle 构建,源集配置如下:
sourceSets {
main {
groovy {
srcDir 'src'
}
}
}
我有一个
Jenkinsfile
在我想要编译的根项目目录中,以便我可以在它合并到主分支并破坏 Jenkins 构建之前检测到无效的 Groovy。如何将这个单个文件添加到 Gradle 项目的源集中进行编译?我如何确保 Gradle 编译它,即使它没有
.groovy
延期?
最佳答案
您可以添加其他 sourceSet
像这样:
sourceSets {
jenkins {
groovy {
srcDir "$projectDir"
include "JenkinsFile"
}
}
main {
groovy {
srcDir 'src'
}
}
}
但是,问题是您将无法将文本文件编译为 Groovy。但是,您可以做的是设置一个任务来创建、编译和删除新的
.groovy
包含 Jenkins 文件在 groovy 编译之前的内容的文件。执行这些操作的最终构建文件如下所示:apply plugin: 'groovy'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
sourceSets {
jenkins {
groovy {
srcDir "$projectDir"
include "JenkinsFile.groovy"
}
}
main {
groovy {
srcDir 'src'
}
}
}
task(createGroovyFile) {
File jenkinsFile = new File("$projectDir/JenkinsFile")
File groovyFile = new File("$projectDir/JenkinsFile.groovy")
if(groovyFile.exists()) groovyFile.delete()
groovyFile.newDataOutputStream() << jenkinsFile.readBytes()
}
task(deleteGroovyFile, type: Delete) {
File groovyFile = new File("$projectDir/JenkinsFile.groovy")
delete(groovyFile)
}
compileGroovy.dependsOn deleteGroovyFile
deleteGroovyFile.dependsOn compileJenkinsGroovy
compileJenkinsGroovy.dependsOn createGroovyFile
configurations {
jenkinsCompile.extendsFrom compile
}
dependencies {
compile "org.codehaus.groovy:groovy-all:2.4.10"
}
关于gradle - 添加 Jenkinsfile 以供 Gradle 编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43142422/