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

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

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’

Load script from groovy script

If you don’t mind the code in file2 being in a with block, you can do: new GroovyShell().parse( new File( ‘file1.groovy’ ) ).with { method() } Another possible method would be to change file1.groovy to: class File1 { def method() { println “test” } } And then in file2.groovy you can use mixin to add … Read more