Skip to content

Commit

Permalink
Added a switch to the command line runner to put the script's directo…
Browse files Browse the repository at this point in the history
…ry on the classpath, see

[FIXES JENKINS-42299]
  • Loading branch information
daspilker committed Apr 30, 2017
1 parent f8b5191 commit b75d2f0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
3 changes: 3 additions & 0 deletions docs/Home.md
Expand Up @@ -37,6 +37,9 @@ Browse the Jenkins issue tracker to see any [open issues](https://issues.jenkins
* Enhanced support for the [Parameterized Remote Trigger
Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Remote+Trigger+Plugin)
([JENKINS-43025](https://issues.jenkins-ci.org/browse/JENKINS-43025))
* Added a switch to the command line runner to put the script's directory on the classpath, see
[User-Power-Moves](User-Power-Moves#run-a-dsl-script-locally)
([JENKINS-42299](https://issues.jenkins-ci.org/browse/JENKINS-42299))
* Support for the older versions of the [Parameterized Remote Trigger
Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Remote+Trigger+Plugin) is deprecated, see
[Migration](Migration#migrating-to-162)
Expand Down
6 changes: 6 additions & 0 deletions docs/User-Power-Moves.md
Expand Up @@ -13,6 +13,12 @@ If you already have the source code checked out then you can ignore step 1.

What's going on here is that there's a static main method that can run the DSL, you just have to give it a filename. It'll output all the jobs' XML to the current directory. Likewise, if you use "using" (the templates-like feature) it'll look in the current directory for a file with the name of the job appended with ".xml" at the end of it.

By default the current directory is added to the classpath to be able to import classes. When using sub-directories for
scripts, the classpath differs compared to running in Jenkins where the DSL script's directory is added to the
classpath. Add the `-j` command line option to use the same behavior as when running in Jenkins:

java -jar $DSL_JAR -j sample.dsl.groovy

# Generate a Job config.xml without having to fire up Jenkins
1. Add some job dsl content to a file, say job.dsl
1. Run the gradle command: ./gradlew run -Pargs=job.dsl
Expand Down
14 changes: 11 additions & 3 deletions job-dsl-core/src/main/groovy/javaposse/jobdsl/Run.groovy
Expand Up @@ -17,7 +17,14 @@ class Run {

@SuppressWarnings('NoDef')
static void main(String[] args) throws Exception {
if (args.length == 0) {
boolean scriptClasspath
String[] files = args
if (files.length > 0 && files[0] == '-j') {
files = files[1..-1]
scriptClasspath = true
}

if (files.length == 0) {
LOG.severe('Script name is required')
return
}
Expand All @@ -31,10 +38,11 @@ class Run {
jm.parameters.put(key.toString(), value.toString())
}

args.each { String scriptName ->
files.each { String scriptName ->
File scriptFile = new File(scriptName)
String scriptBody = scriptFile.getText('UTF-8')
ScriptRequest request = new ScriptRequest(scriptBody, cwdURL, false, scriptFile.absolutePath)
URL urlRoot = scriptClasspath ? scriptFile.absoluteFile.parentFile.toURI().toURL() : cwdURL
ScriptRequest request = new ScriptRequest(scriptBody, urlRoot, false, scriptFile.absolutePath)
GeneratedItems generatedItems = new DslScriptLoader(jm).runScripts([request])

for (GeneratedJob job : generatedItems.jobs) {
Expand Down

0 comments on commit b75d2f0

Please sign in to comment.