groovy

Intercept all method calls for all metaclasses in groovy

我是研究僧i 提交于 2021-01-28 07:33:37
问题 I am trying to intercept all method calls inside Groovy scripts running in a Java environment. Specially I want to check the return type of all method calls, and if it is X I want to replace it with Y . I know you can intercept using invokeMethod on the MetaClass, but I can only do that to the script class that I compile. If the script in turn calls a method on class A then it will create a new MetaClass[A] that I can't intercept without earlier having manually fetched that MetaClass from the

IntelliJ type error when using Geb static content DSL with parameters

白昼怎懂夜的黑 提交于 2021-01-28 07:03:16
问题 Whenever I use a static-content defined closure that takes parameters, IntelliJ will complain that the types do not match (even if I specify the type inside the closure). For example, if I use this static content block: static content = { myModule { $('myModule').module(CustomModule) } namedModule { String name -> $(".$name").module(CustomModule) } } Both of the above items can be used successfully in my tests, but if I was to use 'namedModule' in one of my tests as follows: page.namedModule(

Jenkins Groovy: Update the labels in all nodes

别来无恙 提交于 2021-01-28 06:30:55
问题 I have a few hundred jenkins nodes and need to update the labels in all of them. It seems the easiest would be to run a groovy script in the Jenkins master script console that would add the extra label to each node's config. Question: How would one do this in groovy? 来源: https://stackoverflow.com/questions/56207343/jenkins-groovy-update-the-labels-in-all-nodes

Groovy (or Java) - Pass by reference into a wrapper object

偶尔善良 提交于 2021-01-28 06:21:01
问题 Is it possible in Java (or Groovy) to pass an object by reference into a wrapper object (ie: List or Map)? Example code (in Groovy): def object = null def map = [object: object] object = new Object() Unfortunately map.object remains null even though the object variable doesn't, so obviously the original creation of the map was done by value not by reference. Is it possible to create a List or Map via references so that when the object outside the List or Map changes the change is reflected

Error while executing another process through Groovy script

若如初见. 提交于 2021-01-28 05:30:23
问题 I'm invoking another process through Groovy script of Jenkins pipeline. Following is simple one script - pipeline { agent {label 'xxx.xxx.xx.xx} stages { stage('Test') { steps { script { sh 'pwd' def path = "pwd".execute().text } } } } } sh 'pwd' works perfectly fine. However "pwd".execute().text results into following error - java.io.IOException: CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessImpl.create(Native Method) at java.lang.ProcessImpl.<init>

Jenkins Pipeline - inserting variable in shell creates a new line

喜欢而已 提交于 2021-01-28 05:25:37
问题 I am using Choice param in my jenkins file to select environment as follows: pipeline { agent any parameters { choice( name: 'ENVIRONMENT_URL', choices: "https://beta1.xyz.com\nhttps://beta2.xyz.com\nhttps://beta3.xyz.com", description: 'interesting stuff' ) } in the Stage section, i have the following piece stage('execute tests') { steps { script { sh """URL=${ENVIRONMENT_URL} npm run e2e:tests""" sh 'echo -e "\nTest Run Completed.\n"' } } } However, when i run the pipeline job by selecting

Converting Integer to BigDecimal in Groovy

半世苍凉 提交于 2021-01-28 02:06:37
问题 Let's assume that we have a function in groovy that takes as a parameter BigDecimal : void func(BigDecimal bd) {...} And calling it again in other class on groovy var.func(0) This is working fine, but in java it will not compile at all. I know that there is a constructor in BigDecimal that will work for Integer , but what's the reason that this works in groovy and java complain about it? Is there something with converting def variables to something known in java? 回答1: Groovy uses argument

No closing tags while building xml file with MarkupBuilder

倾然丶 夕夏残阳落幕 提交于 2021-01-28 01:55:08
问题 I want to use Groovy in my Jenkins-Pipeline-Job to build a Statuslist of my Jenkins Slaves and the installed nodes. Therefore I have some lists which should be displayed in xml. While testing with IntelliJ i wrote the following code. import groovy.xml.MarkupBuilder def listNodeNames = [] listNodeNames << 'SLAVE1_NODE_1' listNodeNames << 'SLAVE1_NODE_2' listNodeNames << 'SLAVE2_NODE_1' listNodeNames << 'SLAVE2_NODE_2' def listComputerNames = [] listComputerNames << 'SLAVE1' listComputerNames <

How to persist Properties in jenkins pipeline?

南楼画角 提交于 2021-01-27 23:11:14
问题 In my jenkins pipeline I am working with properties stored in file. I can read properties from file and add new items to the map using this code, but I do not understand how to persist my changes. node('hozuki-best-girl') { def propertiesPath = "${env.hozuki_properties}" def props = readProperties file: propertiesPath props['versionCode'] = 100500 } What should I do in order to persist my changes? There is no writeProperties method here https://jenkins.io/doc/pipeline/steps/pipeline-utility

Unexpected behaviour when Spock datatable's header contains variable named like variable in the other feature method

允我心安 提交于 2021-01-27 20:40:29
问题 I have a problem with data-driven tests in Spock. When datatable header contains variable (in this case 'b') named like variable in the other feature method, the following exception is thrown: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '3' with class 'java.lang.Integer' to class 'java.util.UUID' Example specification class: class ExampleSpec extends Specification { def 'example feature with variable named like datatable header'() { given: UUID b = UUID