trouble following example "Import other files (i.e. with class definitions) into your script"

524 views
Skip to first unread message

Taco Witte

unread,
Nov 27, 2015, 11:29:56 AM11/27/15
to job-dsl-plugin
Hi,


I've put the first snippet in a file called utilities/MyUtilities.groovy and the second snippet in a file called dsl.groovy.

This is the output:
Processing DSL script dsl.groovy
Processing DSL script MyUtilities.groovy
java.lang.ClassCastException: org.codehaus.groovy.runtime.InvokerHelper$2 cannot be cast to javaposse.jobdsl.dsl.JobParent
	at javaposse.jobdsl.dsl.DslScriptLoader.runDslEngineForParent(DslScriptLoader.java:70)
	at javaposse.jobdsl.dsl.DslScriptLoader.runDslEngine(DslScriptLoader.java:132)
	at javaposse.jobdsl.plugin.ExecuteDslScripts.perform(ExecuteDslScripts.java:220)
	at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
	at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:785)
	at hudson.model.Build$BuildExecution.build(Build.java:205)
	at hudson.model.Build$BuildExecution.doRun(Build.java:162)
	at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:537)
	at hudson.model.Run.execute(Run.java:1741)
	at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
	at hudson.model.ResourceController.execute(ResourceController.java:98)
	at hudson.model.Executor.run(Executor.java:410)
ERROR: org.codehaus.groovy.runtime.InvokerHelper$2 cannot be cast to javaposse.jobdsl.dsl.JobParent
Finished: FAILURE

When I put both snippets in the same file and leave out the "package" and "import" lines then it works. That is, this works:
class MyUtilities {
    static void addMyFeature(def job) {
        job.with {
            description('Arbitrary feature')
        }
    }
}
def myJob = job('example')
MyUtilities.addMyFeature(myJob)

What surprises me is that this (removing the last two lines) doesn't work (and results in the same error as above):
class MyUtilities {
    static void addMyFeature(def job) {
        job.with {
            description('Arbitrary feature')
        }
    }
}


I'm using Jenkins 1.635 with Job DSL 1.40. The same happens with Job DSL 1.38.

Is anyone else having this error? Does the example work for others?

Thanks!

Kind regards,
Taco

Victor Martinez

unread,
Nov 27, 2015, 2:55:04 PM11/27/15
to job-dsl-plugin
Can you run the below dsl script and run it?

job('myexample'){
   steps
{
      shell
'''
mkdir -p utilities
cd utilities

cat > MyUtilities.groovy <<- EOM
package utilities

class MyUtilities {
    static void addMyFeature(def job) {
        job.with {
            description('Arbitrary feature')
        }
    }
}
EOM'''
      shell '''
cat > dsl.groovy <<- EOM
import utilities.MyUtilities

def myJob = job('example')
MyUtilities.addMyFeature(myJob)
EOM'''
            dsl {
                external( 'dsl.groovy')
             }
   
}
}

Then it will generate another job, called myexample, which contains the dsl example, if you run that job it should work as expected.

Cheers

Taco Witte

unread,
Dec 1, 2015, 3:42:04 AM12/1/15
to job-dsl-plugin
Hi Victor,

Thanks, that works.

Now I understand what went wrong: I used "**/*.groovy" as filter in "Look on filesystem", "DSL Scripts" because I thought that it should include all .groovy files. Apparently the filter should only include files that define jobs. When I change the filter to "*.groovy" then the example works.

Is this the expected behavior of the filter? Perhaps it would be good for others who try to follow the examples to point to this behavior.

Kind regards,
Taco

Andrzej Rehmann

unread,
Dec 3, 2015, 9:29:02 AM12/3/15
to job-dsl-plugin
I've made simmilar mistake.
Also I had a problem that my main script was also in package and that did not work. I had to put my main script to root folder and remove "package com.example..." from that script

Victor thanks for this example. It blow my mind and it works ;)
What an inception.
Reply all
Reply to author
Forward
0 new messages