0

I am using a gradle project to build a Jenkins shared library. I have a build.gradle with following content:

// Apply the groovy plugin to add support for groovy
apply plugin: 'groovy'

sourceSets {
    main {
        groovy {
            srcDirs = ['src', 'vars']
        }
    }
    test {
        groovy {
            srcDirs = ['test']
        }
    }
}
// In this section you declare where to find the dependencies of your project
repositories {
    // Use 'jcenter' for resolving your dependencies.
    jcenter()
    // You can declare any Maven/Ivy/file repository here.
    maven {
        url "https://repo.jenkins-ci.org/releases/"
    }
}

// In this section you declare the dependencies for your production and test code
dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.5.6'
    compile 'org.apache.ivy:ivy:2.4.0'
    testCompile 'junit:junit:4.12'
}

Inside my vars/ directory I have the following source code in it:

def call(Map params) {
  2     pipeline {
  3         agent none
  4 
  5         stages {
  6             stage('Build') {
  7                 agent { label 'maven'}
  8                 steps {
  9                     logError 'Hello world'
 10                 }
 11             }
 12         }
 13     }
 14 }

When I run ./gradlew build or ./gradlew compileGroovy the compilation goes through fine. It looks it is only doing syntax validation and does not care about linking the symbols such as pipeline or agent etc. Am I understanding it correctly?

sshekhar
  • 137
  • 10

1 Answers1

0

Okay I think I figured it out. This is happening because groovyc (in this case gradlew compileGroovy is producing a byte code that does not translate high level language to its bytecode equivalent. Instead each line is replaced by a method dispatch. So line 2 in the code above will get replaced with

getMetaClass().invokeMethod(this, 'pipeline', closure)

This causes compile time check on pipeline symbol to be promoted to a runtime error.

sshekhar
  • 137
  • 10