groovy

groovy syntax for executing unix command with process output

六月ゝ 毕业季﹏ 提交于 2020-07-22 21:36:01
问题 I'm trying to execute a shell command in groovy. The command is diff <(ls dir1) <(ls dir2) . From the shell, that works fine, but when I do "diff <(ls dir1) <(ls dir2)".execute() in groovy, I get a shell error saying diff: extra operand . I can't seem to figure out the correct syntax for this in groovy. Can someone point out what I'm doing wrong? 回答1: If you can live with calling this via bash like you do on command line, then the syntax to call by shell is: def p = ["/bin/bash", "-c", "diff

NoClassDefFoundError: groovy/lang/binding

折月煮酒 提交于 2020-07-18 22:52:10
问题 I'm trying to evaluate Groovy script inside a Java app by using GroovyShell. Problem: My program compiles ok, but gives me a NoClassDefFoundError at run-time. TestClass.java: import groovy.lang.Binding; import groovy.lang.GroovyShell; class TestClass { static Binding binding; static GroovyShell shell; public static void main(String[] args) { System.out.println("Hello, world."); binding = new Binding(); shell = new GroovyShell(binding); Object value = shell.evaluate("5 ** 5"); } } Then I

Groovy: validate JSON string

断了今生、忘了曾经 提交于 2020-07-18 04:09:26
问题 I need to check that a string is valid JSON in Groovy. My first thought was just to send it through new JsonSlurper().parseText(myString) and, if there was no exception, assume it was correct. However, I discovered that Groovy will happily accept trailing commas with JsonSlurper , but JSON doesn't allow trailing commas. Is there a simple way to validate JSON in Groovy that adhere's to the official JSON spec? 回答1: JsonSlurper class uses JsonParser interface implementations (with

Catching typos in scripting languages

…衆ロ難τιáo~ 提交于 2020-07-17 03:19:32
问题 If your scripting language of choice doesn't have something like Perl's strict mode, how are you catching typos? Are you unit testing everything? Every constructor, every method? Is this the only way to go about it? 回答1: Really-thorough unit tests are the most important technique (yes, I do always aim for 100% coverage), as they also catch many other typos (e.g. where I write + and meant - ), off-by-one issues, etc. Integration and load tests exercising every feature are the second line of

How to mock an ItemReader in a Spring Batch application using Spock and Groovy

孤街浪徒 提交于 2020-07-10 04:50:11
问题 I'm trying to write the tests for a Spring Batch application, specifically on the interaction from the following reader when it gets records from a database implementing a simple RowMapper : @Component @StepScope public class RecordItemReader extends JdbcCursorItemReader<FooDto> { @Autowired public RecordItemReader(DataSource dataSource) { this.setDataSource(dataSource); this.setSql(AN_SQL_QUERY); this.setRowMapper(new RecordItemMapper()); } } Here is the step definition from the Batch

How to mock an ItemReader in a Spring Batch application using Spock and Groovy

安稳与你 提交于 2020-07-10 04:48:44
问题 I'm trying to write the tests for a Spring Batch application, specifically on the interaction from the following reader when it gets records from a database implementing a simple RowMapper : @Component @StepScope public class RecordItemReader extends JdbcCursorItemReader<FooDto> { @Autowired public RecordItemReader(DataSource dataSource) { this.setDataSource(dataSource); this.setSql(AN_SQL_QUERY); this.setRowMapper(new RecordItemMapper()); } } Here is the step definition from the Batch

What does [:] mean in groovy?

霸气de小男生 提交于 2020-07-03 01:45:41
问题 While reading some groovy code of another developer I encountered the following definition: def foo=[:] What does it mean? 回答1: [:] is shorthand notation for creating a Map. You can also add keys and values to it: def foo = [bar: 'baz'] 回答2: [:] creates an empty Map. The colon is there to distinguish it from [] , which creates an empty List. This groovy code: def foo = [:] is roughly equivalent to this java code: Object foo = new java.util.LinkedHashMap(); 回答3: Quoting the doc: Notice that [:

ext and code block's meaning in the gradle file

匆匆过客 提交于 2020-07-02 06:17:53
问题 ext { springVersion = "3.1.0.RELEASE" emailNotification = "build@master.org" } Above code is the snippet of build.gradle I understand that call ext method with { } closure parameter. it's right? So I think gradle is accessing springVersion and emailNotification. I'm gonna verify my assumption with below code def ext(data) { println data.springVersion } ext { springVersion = "3.1.0.RELEASE" emailNotification = "build@master.org" } but run that code below Error occured. groovy.lang

ext and code block's meaning in the gradle file

这一生的挚爱 提交于 2020-07-02 06:16:42
问题 ext { springVersion = "3.1.0.RELEASE" emailNotification = "build@master.org" } Above code is the snippet of build.gradle I understand that call ext method with { } closure parameter. it's right? So I think gradle is accessing springVersion and emailNotification. I'm gonna verify my assumption with below code def ext(data) { println data.springVersion } ext { springVersion = "3.1.0.RELEASE" emailNotification = "build@master.org" } but run that code below Error occured. groovy.lang

ext and code block's meaning in the gradle file

纵然是瞬间 提交于 2020-07-02 06:16:27
问题 ext { springVersion = "3.1.0.RELEASE" emailNotification = "build@master.org" } Above code is the snippet of build.gradle I understand that call ext method with { } closure parameter. it's right? So I think gradle is accessing springVersion and emailNotification. I'm gonna verify my assumption with below code def ext(data) { println data.springVersion } ext { springVersion = "3.1.0.RELEASE" emailNotification = "build@master.org" } but run that code below Error occured. groovy.lang