Reputation: 1173
I have a Gradle build with the source sets configured like so:
sourceSets {
main {
groovy {
srcDir 'src'
}
}
}
I have a Jenkinsfile
in the root project directory that I would like to have compiled so that I can detect non-valid Groovy before it gets merged to the master branch and breaks the Jenkins build.
How can I add this single file to the source sets of the Gradle project for compilation? How can I ensure that Gradle compiles it even though it's not got a .groovy
extension?
Upvotes: 4
Views: 997
Reputation: 3912
You can add additional sourceSet
like this:
sourceSets {
jenkins {
groovy {
srcDir "$projectDir"
include "JenkinsFile"
}
}
main {
groovy {
srcDir 'src'
}
}
}
However, the problem is that you will not be able to compile a text file as Groovy. What you can do, however, is set up a task to create, compile, and delete a new .groovy
file containing the contents of the Jenkins file before the groovy compilation. The final build file doing these things would 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"
}
Upvotes: 1