How is it possible to import other files (e.a. with classdefinitions) into my dsl-file

7,259 views
Skip to first unread message

Christoph Moser

unread,
Apr 18, 2013, 3:51:59 AM4/18/13
to job-dsl...@googlegroups.com
I want to split my big dsl-file into some short one and want to move class definitions into separate files.
How is it possible to import files in my main-dsl-script and how do I access the imported classes?

Christoph Moser

unread,
Apr 18, 2013, 10:08:18 AM4/18/13
to job-dsl...@googlegroups.com
I supposed the path to the splittet sourcefiles I checked out from SVN is Jenkinslike ${workspace}/.
After some trial I discovered, that my suggestion was wrong - after I took the right path the code from "http://pleac.sourceforge.net/pleac_groovy/packagesetc.html" works well.

try {
    evaluate(new File('doesnotexist.groovy'))
} catch (Exception FileNotFoundException) {
    println 'File not found, skipping ...'
}

Justin Ryan

unread,
Apr 18, 2013, 11:22:48 AM4/18/13
to job-dsl-plugin
It's much easier than that, just make Groovy classes. E.g. 

1. Make a directory at the same level as the DSL called "utilities"
2. Make a file called MyUtilities.groovy in the utilities directory
3. Put the following contents in it:
  import javaposse.jobdsl.dsl.Job
  class MyUtilities {
    def addEnterpriseFeature(Job job) {
        job.with {
          description('Arbitrary feature')
       }
    }
  }
4. Then from the DSL, add something like this:
  import utilities.MyUtilities
  MyUtilities.addEnterpriseFeature(job)

There's some magic under the covers to lookup the Classes in the workspace, so that seed jobs can run from slaves. You can try to evaluate() for sure, but the general suggestion is to just use plain classes. And once we get @Grab working, you can put those classes into a .jar file and share it with many seed jobs.


--
You received this message because you are subscribed to the Google Groups "job-dsl-plugin" group.
To unsubscribe from this group and stop receiving emails from it, send an email to job-dsl-plugi...@googlegroups.com.
To post to this group, send email to job-dsl...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/job-dsl-plugin/-/ptiCoPv9oEIJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

Andrew Harmel-Law

unread,
Apr 18, 2013, 11:44:19 AM4/18/13
to job-dsl...@googlegroups.com
I'll document this on the wiki.

:D

Christoph Moser

unread,
Apr 18, 2013, 11:47:59 AM4/18/13
to job-dsl...@googlegroups.com
When trying this I got following error:
Compilation incomplete: expected to find the class utilities.MyUtilities in /utilities\MyUtilities.groovy, but the file contains the classes: MyUtilities
(I'm a bloody beginner )


2013/4/18 Andrew Harmel-Law <and...@harmel-law.com>
361.gif

Justin Ryan

unread,
Apr 19, 2013, 3:25:42 AM4/19/13
to job-dsl-plugin
I'm not exactly sure, I was writing that adhoc, so I could have missed something. I think you need to add a "package utilities" to the top. Try also making the class public.
361.gif

Christoph Moser

unread,
Apr 22, 2013, 11:54:52 AM4/22/13
to job-dsl...@googlegroups.com
thanks - it works now ... afterwards I'm wondering what was the problem >> it's really easy :-)

James Levinson

unread,
May 8, 2013, 6:26:36 PM5/8/13
to job-dsl...@googlegroups.com
This works well if your seed file is in the root of your job workspace, but I can't seem to get it working if it's down a couple folders, i.e $WORKSPACE/groovy/src.  I tried to pass a classpath with ENVInject, but I still get  unable to resolve class com.company.conf.Config with Config at groovy/src/com/company/conf/Config.groovy

CLASSPATH=$WORKSPACE/groovy/src
Look on Filesystem---->DSL Scripts-----> groovy/src/diff_artifacts.dsl.groovy

Can I pass in a classpath or should I re-arrange my project or something else, maybe?

Justin Ryan

unread,
May 8, 2013, 7:06:26 PM5/8/13
to job-dsl-plugin
You'll have to dig into the code and see how the GroovyScriptEngine is setup. I believe it does assume a few things, but I can't remember off the top of my head. Most likely it doesn't take CLASSPATH into account implicitly and I don't use it explicitly.

It probably wouldn't be too hard to have it be relative to the .dsl file, is that what you're looking for?

I'm currently working on packaging our "convention" into a .jar so I can use @Grab, though I'm not sure it'll work yet.


--
You received this message because you are subscribed to the Google Groups "job-dsl-plugin" group.
To unsubscribe from this group and stop receiving emails from it, send an email to job-dsl-plugi...@googlegroups.com.
To post to this group, send email to job-dsl...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/job-dsl-plugin/-/8eGwWkELpDgJ.

James Levinson

unread,
May 8, 2013, 8:06:32 PM5/8/13
to job-dsl...@googlegroups.com
Hmm, you must mean http://groovy.codehaus.org/api/groovy/util/GroovyScriptEngine.html  

Yes, relative would be fine. I found this ancient thread but I'm not sure if it ever got implemented.

As for @Grab, clueless.

Justin Ryan

unread,
Jun 19, 2013, 11:15:30 PM6/19/13
to job-dsl-plugin
(Digging through old threads...)

James, to help explain @Grab (now that it's in the job-dsl official), I added some docs: https://github.com/jenkinsci/job-dsl-plugin/wiki/Job-DSL-Commands#grab. This might be the best way to distribute a bunch of scripts to be used across multiple projects, i.e. cleaner than doing some directory magic to get the classpath right. I'm lacking any docs on how to make a jar, I'll leave that up to the reader.


To view this discussion on the web visit https://groups.google.com/d/msg/job-dsl-plugin/-/EmKGKrOC2yoJ.

Patrick Pötz

unread,
Jul 26, 2016, 5:34:36 AM7/26/16
to job-dsl-plugin
Hi guys,

Are there any updates on this issue?
We are currently working with v1.39
I tried to make some configure blocks reusable and put them into a Utility Class.
But this class can only be resolved if it's located in the same package as the calling script.
It seems Jenkins isn't able to figure out what a "import" command is.
Following error message:
"ERROR: startup failed:
[11:12:53] workspace://ProductCore%2Fbugfix-branches-seed/src/jobs/groovy/build/productBuild.groovy: 3: unable to resolve class util
[11:12:53]  @ line 3, column 1.
[11:12:53]    import static util.JobDSLUtility"

Would appreciate any recommendations.
Best regards,
Patrick

Azul Inho

unread,
Jul 26, 2016, 9:23:49 AM7/26/16
to job-dsl-plugin
This works for me,


my templates.dsl.groovy:


class TemplatesDSL {

    static COMMON_ENVIRONMENT_VARIABLES = [

       'ARTIFACTORY_CREDENTIALS': 'jenkins:XXXXXXX',
       'ARTIFACTORY_LOCAL_NUGET_URL': 'http://artifactory.com:8083/artifactory/api/nuget/local_nuget',
       'ARTIFACTORY_URL': 'http://artifactory.com:8083/artifactory/api/nuget',
       'MSBUILD_EXE': 'C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\msbuild.exe',
       'NUGET_EXE': '$WORKSPACE\\_Build\\.nuget\\nuget.exe',
       'XUNIT_CONSOLE_EXE': '$WORKSPACE\\xunit.runner.console.2.1.0\\tools\\xunit.console.exe'
    ]

    static generateJob(def dslFactory,
                  project,
                  repo,
                  branch_simple_name,
                  full_branch_name,
                  job_name,
                  build_environment_variables,
                  nupkg_environment_variables,
                  properties_filename,
                  merge_to_branch) {


        def BITBUCKET_CREDENTIALS = 'jen...@bitbucket.com'
        def GIT_MERGE_TO_BRANCH = "${merge_to_branch}"
        def GIT_REMOTE_URL = "https://bitbucket.com/scm/${project}/${repo}.git"
        def JENKINS_SLAVE_LABEL = 'POOL1'
        def NUGET_ASSEMBLY_BRANCHES = [ 'develop_disabled', 'master_disabled' ]
        def NUGET_SYMBOLS_BRANCHES = []
        def JENKINS_WORKSPACE = "${project}.${repo}"


        dslFactory.folder("${project}")
        dslFactory.folder("${project}/${repo}")
        dslFactory.folder("${project}/${repo}/${branch_simple_name}")

        dslFactory.job("${project}/${repo}/${branch_simple_name}/build") {
            description("This job is responsible for building the ${repo} repository")
            checkoutRetryCount(3)
            label(JENKINS_SLAVE_LABEL)
            concurrentBuild(false)
            customWorkspace(JENKINS_WORKSPACE)

            scm {
                git {
                    branch("*/${full_branch_name}")
                    remote {
                        url(GIT_REMOTE_URL)
                        credentials(BITBUCKET_CREDENTIALS)
                    }
                    extensions {
                        cleanAfterCheckout()
                        mergeOptions {
                            remote('origin')
                            branch(GIT_MERGE_TO_BRANCH)
                        }
                        cloneOptions {
                            shallow(true)
                        }

                    }
                }
            }

            triggers {
                scm('') {
                  ignorePostCommitHooks(false)
                }
            }

            steps {
              environmentVariables {
                  for (item in COMMON_ENVIRONMENT_VARIABLES) {
                      env(item.key, item.value)
                  }

                  for (item in build_environment_variables) {
                      env(item.key, item.value)
                  }
                }

              batchFile('jenkins\\pre-build.bat')

 
                  for (item in nupkg_environment_variables) {
                      env(item.key, item.value)
                  }
              }

              batchFile('jenkins\\build.bat')

 
            }

            wrappers {
                colorizeOutput()
                timestamps()
                timeout {
                    absolute(15)
                    failBuild()
                }
            }

            publishers {
                archiveXUnit {
                    xUnitDotNET {
                        pattern('*.xml')
                        deleteOutputFiles(true)
                        failIfNotNew(true)
                        skipNoTestFiles(true)
                        stopProcessingIfError(true)
                    }
                }
                wsCleanup {
                    deleteDirectories(false)
                        cleanWhenFailure(false)
                        cleanWhenSuccess(true)
                        cleanWhenUnstable(false)
                        cleanWhenNotBuilt(false)
                        cleanWhenAborted(false)
                        failBuildWhenCleanupFails(false)
                }
            }


        }
    }
}

my jenkins.dsl file:



// slurp some methods from a common template.
// this template job needs to be executed once to allow the seed job for this
// particular project to complete correctly.

File sourceFile = new File("/var/lib/jenkins/jobs/PROJ/jobs/PROJ.jenkins.dsl.helpers/jobs/master/jobs/build/workspace/TemplatesDSL.groovy")
Class TemplatesDSL = new GroovyClassLoader(getClass().getClassLoader()).parseClass(sourceFile)

def BUILD_ENVIRONMENT_VARIABLES = [
    'CONFIGURATION': 'release'
]

def NUPKG_ENVIRONMENT_VARIABLES = [
    'MY_NUPKG': 'asdfk.nupkg',
]


TemplatesDSL.generateJob(
    this,
    "${_injected_var_PROJECT}",
    "${_injected_var_REPO}",
    "${_injected_var_BRANCH_SIMPLE_NAME}",
    "${_injected_var_BRANCH}",
    "build",
    BUILD_ENVIRONMENT_VARIABLES,
    NUPKG_ENVIRONMENT_VARIABLES,
    "build.properties",
    "develop"
)

Patrick Pötz

unread,
Jul 26, 2016, 9:55:25 AM7/26/16
to job-dsl-plugin
@Azul Inho
Thx, you helped me a lot.

Solved it now your way:

String sourceFile = readFileFromWorkspace("path/from/workspace/to/your/UtilityClass.groovy")
Class UtilityClass = new GroovyClassLoader(getClass().getClassLoader()).parseClass(sourceFile)

Best regards,
Patrick

Felix Becker

unread,
Sep 12, 2016, 9:48:18 AM9/12/16
to job-dsl-plugin
https://github.com/jenkinsci/job-dsl-plugin/wiki/Real-World-Examples

Please remove it from the examples. It doesn't work as described and it costs people much time to find the bug.

Taras Fog

unread,
Nov 22, 2016, 9:51:27 AM11/22/16
to job-dsl-plugin
Hi Felix.

Actually, it works. Though with some little tweaks. 

Please see thread as follow:

Hopefully, it will save you from wasting time as saved to me.

Regards,
Taras.
Reply all
Reply to author
Forward
0 new messages