groovy
Difference between Groovy Binary and Source release?
A source release will be compiled on your own machine while a binary release must match your operating system. source releases are more common on linux systems because linux systems can dramatically vary in cpu, installed library versions, kernelversions and nearly every linux system has a compiler installed. binary releases are common on ms-windows systems. … Read more
Recommended way to stop a Gradle build
I usually throw the relevant exception from the org.gradle.api package, for example InvalidUserDataException for when someone has entered something invalid, or GradleScriptException for more general errors. If you want to stop the current task or action, and move on to the next, you can also throw a StopActionException
Groovy different results on using equals() and == on a GStringImpl
Nice question, the surprising thing about the code above is that println “${‘test’}”.equals(‘test’) returns false. The other line of code returns the expected result, so let’s forget about that. Summary “${‘test’}”.equals(‘test’) The object that equals is called on is of type GStringImpl whereas ‘test’ is of type String, so they are not considered equal. But … Read more
Reading file from Workspace in Jenkins with Groovy script
I realize this question was about creating a plugin, but since the new Jenkins 2 Pipeline builds use Groovy, I found myself here while trying to figure out how to read a file from a workspace in a Pipeline build. So maybe I can help someone like me out in the future. Turns out it’s … Read more
groovy execute with parameters containing spaces
The trick was to use a list: println([‘ls’, ‘/tmp/folder with spaces’].execute().text)
How can I use the Jenkins Copy Artifacts Plugin from within the pipelines (jenkinsfile)?
With a declarative Jenkinsfile, you can use following pipeline: pipeline { agent any stages { stage (‘push artifact’) { steps { sh ‘mkdir archive’ sh ‘echo test > archive/test.txt’ zip zipFile: ‘test.zip’, archive: false, dir: ‘archive’ archiveArtifacts artifacts: ‘test.zip’, fingerprint: true } } stage(‘pull artifact’) { steps { copyArtifacts filter: ‘test.zip’, fingerprintArtifacts: true, projectName: env.JOB_NAME, … Read more
Converting a string to int in Groovy
Use the toInteger() method to convert a String to an Integer, e.g. int value = “99”.toInteger() An alternative, which avoids using a deprecated method (see below) is int value = “66” as Integer If you need to check whether the String can be converted before performing the conversion, use String number = “66” if (number.isInteger()) … Read more
Groovy: what’s the purpose of “def” in “def x = 0”?
It’s syntactic sugar for basic scripts. Omitting the “def” keyword puts the variable in the bindings for the current script and groovy treats it (mostly) like a globally scoped variable: x = 1 assert x == 1 assert this.binding.getVariable(“x”) == 1 Using the def keyword instead does not put the variable in the scripts bindings: … Read more
Get values from properties file using Groovy
It looks to me you complicate things too much. Here’s a simple example that should do the job: For given test.properties file: a=1 b=2 This code runs fine: Properties properties = new Properties() File propertiesFile = new File(‘test.properties’) propertiesFile.withInputStream { properties.load(it) } def runtimeString = ‘a’ assert properties.”$runtimeString” == ‘1’ assert properties.b == ‘2’