groovy

Groovy dynamic property per object

这一生的挚爱 提交于 2020-01-11 05:27:36
问题 Using Groovy 1.8. I'm trying to create a dynamic class definition that will cache properties per object. I did use propertyMissing without adding the property to the object just fine. I just think caching the properties would be more efficient. Right? Note that each instance must have its own different properties. The code below works fine: class C {} def c = new C() c.metaClass.prop = "a C property" println c.prop def x = new C() x.prop will output: a C property groovy.lang

How to get environment variable in Jenkins Groovy script console?

雨燕双飞 提交于 2020-01-11 04:36:08
问题 In Jenkins configuration (http://JenkinsURL/configure) within "Global properties" I defined some "Environment variables". How can I access them in the Groovy Script console (http://JenkinsURL/script)? I've tried to find appropriate solution (for example the solutions mentioned in: Access to build environment variables from a groovy script in a Jenkins build step (Windows)) but it seems that none of them work for me. I've tried for example: System.getenv("myVar") and manager.build

Why don't I get a NullPointerException in Groovy in this case?

自作多情 提交于 2020-01-11 04:27:05
问题 I have this test code: def test = null test.each { } Why don't I get any exception? 回答1: The implementation of each tries to call the iterator method of it's target in a null-safe fashion . If each is called on a null object, or an object without an iterator method, nothing happens. I haven't seen the source code, but it could look something like this§ Object each(Closure closure) { if (this?.respondsTo("iterator")) { def iterator = this.iterator() while (iterator.hasNext() { def item =

Groovy script to get the request xml

我只是一个虾纸丫 提交于 2020-01-11 04:12:08
问题 I can get the response xml using Groovy script. I need to get the request XML since I need to add 'assertion script' to my soap ui testing. I am using the following code to get the response xml def response = new XmlHolder(messageExchange.responseContentAsXml) But I am not sure how to get the request xml of SOAPUI. Can anyone please help me in getting the request xml of SOPAUI? 回答1: To get the request content as a string you can use testRunner.testCase.testSteps["Name of your teststeup"]

How to submit Jenkins job via REST API?

痴心易碎 提交于 2020-01-11 04:09:05
问题 The following 'Execute system Groovy script' Build Task updates the build's description to add a button that will submit another Jenkins job which is parameterized: import hudson.model.Cause import hudson.model.Job import jenkins.model.Jenkins final JOB_NAME = 'my-job' final jenkins = Jenkins.instance final job = jenkins.getItemByFullName(JOB_NAME, Job.class) final currentBuild = Thread.currentThread().executable final buildNumber = currentBuild.getNumber() job.builds .findAll { build ->

getting well formed output from ant - sshexec in groovy script

二次信任 提交于 2020-01-11 03:41:14
问题 my problem is, that the output from the ant task alwas has some [ssh-exec] infotext at the beginning. can i suppress / disable that? my code so far: def ant = new AntBuilder() // .... variable definition ... ant.sshexec(host: host, port: port, trust: true, username: username, password: password, command: 'ls') >>> output: [sshexec] Connecting to foomachine.local:22 [sshexec] cmd : ls [sshexec] oldarchive.gz [sshexec] newarchive.gz [sshexec] empty-db.sh [sshexec] testfile.py i just want to

Installing HTTPBuilder for Groovy

让人想犯罪 __ 提交于 2020-01-11 01:29:06
问题 Apologies for the newbie question, but how do you install HTTPBuilder for Groovy? I've added the http-builder-0.7.jar, http-builder-0.7-source.jar, and http-builder-0.7-javadoc.jar to GROOVY_HOME/lib. Is there anything else I need to do? The HTTPBuilder website isn't clear. Code run from GroovyConsole: import groovy.grape.Grape Grape.grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' ) def http = new groovyx.net.http.HTTPBuilder('http://www.codehaus

Installing HTTPBuilder for Groovy

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-11 01:29:02
问题 Apologies for the newbie question, but how do you install HTTPBuilder for Groovy? I've added the http-builder-0.7.jar, http-builder-0.7-source.jar, and http-builder-0.7-javadoc.jar to GROOVY_HOME/lib. Is there anything else I need to do? The HTTPBuilder website isn't clear. Code run from GroovyConsole: import groovy.grape.Grape Grape.grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' ) def http = new groovyx.net.http.HTTPBuilder('http://www.codehaus

【Gradle】Groovy的语法详解(下篇)

不羁岁月 提交于 2020-01-11 00:14:58
上文介绍了Groovy的一般省略规则,以及字符串、列表、Map的使用方式,然而在Groovy中,闭包是其中的一大杀器,可以对代码的结构和逻辑带来的优化显而易见。本博文主要对Groovy闭包的使用、文件IO的使用进行讲解。 本文实例源码github地址 : https://github.com/yngzMiao/yngzmiao-blogs/tree/master/2020Q1/20200110 。 闭包 闭包,是一个代码块,或可以理解成一个匿名函数。在外部方法调用时,可以将其作为方法的实参传递给方法的形参,并在方法内部回调此匿名函数,且回调此匿名函数时可以传递实参给到匿名函数的内部去接收,并执行此匿名函数 。 同时,此代码块或匿名函数也可以赋值给一个变量,使其具有自执行的能力,且最后一行的执行语句作为匿名函数的返回。 闭包基础 可能乍一读并不太能够理解这段话的含义,可以先看几个例子: def b1 = { println "hello world" } b1 ( ) //此处也可以写成b1.call() 运行后,输出结果为: hello world 可以看出,所谓闭包,其实就是一段代码块(用大括号包括起来了)。它可以被赋值给一个闭包对象,如果查看此时b1的类型,其实是 Closure 。 既然存在闭包对象,那么很显然,也可以将其作为方法的实参传递给某个方法。例如: def b1 =

Resolve Application label for each build type

泪湿孤枕 提交于 2020-01-10 19:58:43
问题 I want to resolve manifestPlaceholders dependency for each build types and flavors. For example, I have productFlavors { dev { manifestPlaceholders = ['applicationLabel': 'DevFlavor'] } prod { manifestPlaceholders = ['applicationLabel': 'ProdFlavor'] } ..... buildTypes { debug { def old_name = manifestPlaceholders.get('applicationLabel'); // every time is null // def old_name = productFlavors.dev.manifestPlaceholders.get('applicationLabel'); // ITS OK, but not dynamic manifestPlaceholders =