groovyshell

Groovy Shell script object not executed entirely

我与影子孤独终老i 提交于 2020-01-05 07:50:07
问题 we are creating a groovy shell object and passing the bindings to the shell then the parsing the groovy code using the shell and initializing a Script object as below GroovyShell shell = new GroovyShell(binding); Script script = shell.parse(//groovy code ); then we are storing the script object in a Concurrent hashmap and running the script using script.run() fetching the script from this hashmap , But the groovy code in the script does not executes completely say 1 in 100 runs . we had

How do I redirect a process' stdout, stderr and stdin to and from files in Groovy, like in the Bash shell?

旧巷老猫 提交于 2019-12-24 05:00:40
问题 I'm porting a Bash shell script to Groovy. Most constructs can easily be converted (e.g. mkdir "$foo" to foo.mkdir() . However, I'm stumped on this: #!/bin/bash sleep 60 > /tmp/output.log 2>&1 < /dev/null When running it, let's inspect the file descriptors of sleep : $ ls -l /proc/$(pgrep sleep)/fd total 0 lr-x------ 1 user user 64 Feb 25 13:40 0 -> /dev/null l-wx------ 1 user user 64 Feb 25 13:40 1 -> /tmp/output.log l-wx------ 1 user user 64 Feb 25 13:40 2 -> /tmp/output.log Running a

evaluating a groovy string expression at runtime

自古美人都是妖i 提交于 2019-12-23 22:50:58
问题 If I have code such as (which doesn’t work): def value = element.getAttribute("value") Binding binding = new Binding(); binding.setVariable("valueExpression", value); def interpolatedValue = new GroovyShell(binding).evaluate("return valueExpression") println ("interpolated Value = $interpolatedValue") and the value from the xml attribute is “The time is ${new Date()}” how do I get Groovy to evaluate this expression at runtime? Using the above code I get “The time is ${(new Date()}” instead of

How to use @SourceURI annotation to retrieve the full path of the script file in Groovy 2.3?

安稳与你 提交于 2019-12-23 14:39:52
问题 I need to retrieve at runtime the full path of the script file in Groovy 2.3. Actually I have the same problem than the one described here: get path to groovy source file at runtime. My script can be executed from GroovyShell or not. My script is located at : C:\users\myname\documents\scripts\myscript.groovy => I simpy want to retrieve this path at runtime. If I use the solution that was accepted: println new File(".").absolutePath what I get is actually : C:\groovy-2.3.7\. which is the

Use groovy category in groovy shell

不羁的心 提交于 2019-12-23 12:23:25
问题 I am working under some DSL using Groovy categories and I would like to find a way to use my DSL with groovy shell without explicitly writing use(MyCategory){ myObject.doSomething() } for each command. For example, suppose I have the following toy category: class MyCategory { static Integer plus(Integer integer, String string){ return integer + Integer.valueOf(string) } } Then, I can use this category in groovysh in the following way: groovy> use(MyCategory){ 2 + '3' } //gives 5 So, is there

How to add imports to groovysh on startup?

不羁岁月 提交于 2019-12-22 06:34:03
问题 I'm working on a project where I'd like users to experiment with Java classes on Groovysh. I'd like to make it convenient for them and want to import certain packages by default, when groovysh starts up so that users would not have to re-type the same imports every time they start the shell. Does anyone know how to accomplish this? Thanks in advance, igor 回答1: You can add the imports to $HOME/.groovy/groovysh.rc 回答2: From http://groovy.codehaus.org/Groovy+Shell: This script, if it exists, is

Stopping the execution of a Groovy script

坚强是说给别人听的谎言 提交于 2019-12-19 04:19:12
问题 I am embeding Groovy runtime in my code and I would like to have the ability to interrupt it. I don't have control of the scripts that are going to run. I read about groovy.transform.ThreadInterrupt to handle thread interruptions but for some reason this code below isn't working as intended. It's actually waiting 10000 ms instead of the 1000 where it should get interrupted. Any ideas? Thank you. import groovy.lang.Binding; import groovy.lang.GroovyShell; import groovy.transform

Replace the string in file using groovy

谁说我不能喝 提交于 2019-12-14 02:43:59
问题 I have got a file with name "silent.txt". This file is having a line as follows bop4InstallDir = myProps.cordys_install_dir + "/" + instanceName I want to replace the above text with bop4InstallDir = "/abc/xyz/pqr" Using groovy script how do I accomplish this? Please help. 回答1: Not very elegant, but this should work. def file = new File("silent.txt") file.text = file.text.replace('bop4InstallDir = myProps.cordys_install_dir + "/" + instanceName', 'bop4InstallDir = "/abc/xyz/pqr"') 回答2: Is

Parsing classes with GroovyShell

*爱你&永不变心* 提交于 2019-12-13 03:52:09
问题 I have a groovy script that need to run a method inside a class inside an external groovy script. I know how to run a method within an external groovy script: new GroovyShell().parse( new File( 'foo.groovy' ) ).with { method() } But what if the method is inside a class? I tried this but it gave me an error. new GroovyShell().parse( new File( 'foo.groovy' ) ).with { theclass.method() } 回答1: You can use Java reflection to create new instance of a Class that is located in another script: File

error seeing when parsing the json response using groovy

自作多情 提交于 2019-12-11 15:36:52
问题 import groovy.json.JsonSlurper def testStepName = 'Adding_Users' def jsonSlurper = new JsonSlurper() def response = context.expand('$(testStepName#Response)') def usersInfomration = jsonSlurper.parseText(response) String userName = userInformation.name log.info UserName 来源: https://stackoverflow.com/questions/58326431/error-seeing-when-parsing-the-json-response-using-groovy