Adding Jenkins file to compile with Gradle
I have a Gradle build with source settings configured like so:
sourceSets {
main {
groovy {
srcDir 'src'
}
}
}
I have Jenkinsfile
in the root of a project that I would like to compile so that I can detect invalid Groovy before it merges into the master branch and breaks the Jenkins build.
How can I add this single file to my Gradle project source kits for compilation? How can I guarantee that Gradle will compile it even though it didn't get the extension .groovy
?
source to share
You can add additional ones sourceSet
like this:
sourceSets { jenkins { groovy { srcDir "$projectDir" include "JenkinsFile" } } main { groovy { srcDir 'src' } } }
However, the problem is that you won't be able to compile the text file as Groovy. However, you can create a task to create, compile and delete a new file .groovy
containing the contents of the Jenkins file before compiling groovy. The final build file doing this will look like this:
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" }
source to share