Using the Groovy Definition Type
Groovy is a programming language that runs on the Java platform. It needs the groovy runtime, which requires a JVM. Groovy is especially recommended for data processing.
Variable and Parameters
Variables that you require to hold intermediary values are defined in the same way as you would normally define variables in Groovy, using the standard syntax int myVar
or myVar = "initial value"
.
Process definition parameters are available directly in Groovy as Java variables, as shown in the examples below.
note
When a process reaches status Error, the Out values of parameters are not set. You can use a Post Running action to set them if required.
Get the sample XML music catalog from w3schools, parse the XML, print some information from it and store the number of CD's in the out parameter OutParameter1.
println "Getting Music Catalog"
catalog = "http://www.w3schools.com/xml/cd_catalog.xml".toURL().text
cds = new XmlSlurper().parseText(catalog)
cds.CD.each {
println it.ARTIST.text()+": " + it.TITLE.text()
}
// One is missing
println "Simon & Garfunkel: 'The 59th Street Bridge Song'"
//Store number of CD's found in OutParameter1, defined on the process
OutParameter1 = cds.CD.size()
Create an XML directory listing of a directory path specified in the Parameter1 process parameter
//Parameter1 is a String parameter defined on the process
def xml = new groovy.xml.MarkupBuilder()
File dir = new File(Parameter1)
xml.files(type:"current dir list")
{
dir.eachFile
{ f ->
file(f.getName())
}
}