Questions tagged [groovy]

Groovy is an object-oriented programming language for the Java platform. It is a dynamic language with features similar to those of Python, Ruby, Perl and Smalltalk. It can be used as a scripting language for the Java platform.

Apache Groovy is a powerful, optionally typed and dynamic language, with static-typing and static compilation capabilities, for the platform aimed at improving developer productivity thanks to a concise, familiar and easy to learn syntax. It integrates smoothly with any program, and immediately delivers to your application powerful features, including scripting capabilities, Domain-Specific Language authoring, runtime and compile-time meta-programming and functional programming.

Groovy is an object-oriented, agile and dynamic language for the Java Virtual Machine. It builds upon the strengths of but has additional power features inspired by languages like , and . It makes modern programming features available to developers with almost zero learning curve. It can be used as a scripting language for the Platform. From Wikipedia

Flat learning curve:

Concise, readable and expressive syntax, easy to learn for developers

Powerful features:

Closures, builders, runtime & compile-time meta-programming, functional programming, type inference, and static compilation

Smooth Java integration:

Seamlessly and transparently integrates and interoperates with and any third-party libraries

Domain-Specific Languages:

Flexible & malleable syntax, advanced integration & customization mechanisms, to integrate readable business rules in your applications

Vibrant and rich ecosystem:

Web development, reactive applications, concurrency / asynchronous / parallelism library, test frameworks, build tools, code analysis, GUI building

Scripting and testing glue:

Great for writing concise and maintainable tests, and for all your build and automation tasks

Hello World

println 'Hello World'

Tools

Online Resources

Groovy compared to other languages

On January 19, 2015 Pivotal announced end of their sponsorship to Groovy and Grails. Groovy was then submitted to become a project at the Apache Software Foundation. On November 18, 2015 the Groovy project graduated from Apache Project Incubation becoming an official Apache project.

29887 questions
192
votes
5 answers

Groovy / grails how to determine a data type?

What is the best way to determine the data type in groovy? I'd like to format the output differently if it's a date, etc.
Jack BeNimble
  • 35,733
  • 41
  • 130
  • 213
187
votes
6 answers

Can I find out the return value before returning while debugging in Intellij?

With: Object method(){ ... return /* some complex expression */ } Is there a way I can see what value will be returned while debugging? Like somehow set a breakpoint that would be hit right after the return, but before execution goes to the…
Kyle
  • 21,377
  • 37
  • 113
  • 200
171
votes
4 answers

How can I determine if a String is non-null and not only whitespace in Groovy?

Groovy adds the isAllWhitespace() method to Strings, which is great, but there doesn't seem to be a good way of determining if a String has something other than just white space in it. The best I've been able to come up with is: myString &&…
cdeszaq
  • 30,869
  • 25
  • 117
  • 173
163
votes
8 answers

Can you break from a Groovy "each" closure?

Is it possible to break from a Groovy .each{Closure}, or should I be using a classic loop instead?
tinny
  • 4,232
  • 7
  • 28
  • 38
161
votes
8 answers

How do I create and access the global variables in Groovy?

I need to store a value in a variable in one method and then I need to use that value from that variable in another method or closure. How can I share this value?
srini
  • 6,719
  • 8
  • 32
  • 36
155
votes
8 answers

How to check if element in groovy array/hash/collection/list?

How do I figure out if an array contains an element? I thought there might be something like [1, 2, 3].includes(1) which would evaluate as true.
banderson623
  • 2,708
  • 2
  • 19
  • 13
155
votes
3 answers

What's wrong with Groovy multi-line String?

Groovy scripts raises an error: def a = "test" + "test" + "test" Error: No signature of method: java.lang.String.positive() is applicable for argument types: () values: [] While this script works fine: def a = new String( "test" + "test" …
yegor256
  • 102,010
  • 123
  • 446
  • 597
155
votes
5 answers

Conditional step/stage in Jenkins pipeline

How do you run a build step/stage only if building a specific branch? For example, run a deployment step only if the branch is called deployment, leaving everything else the same.
Devonte
  • 3,319
  • 5
  • 20
  • 15
154
votes
20 answers

How to list all `env` properties within jenkins pipeline job?

Given a jenkins build pipeline, jenkins injects a variable env into the node{}. Variable env holds environment variables and values. I want to print all env properties within the jenkins pipeline. However, I do no not know all env properties ahead…
JamesThomasMoon
  • 6,169
  • 7
  • 37
  • 63
150
votes
5 answers

Could not find method leftShift() for arguments after updating studio 3.4

After updating studio 3.4 and Gradle version to 5.1.1 I got the error on my task as Could not find method leftShift() My task: task incrementBetaVersion << { println("Incrementing Beta Version Number...") …
Bhuvanesh BS
  • 13,474
  • 12
  • 40
  • 66
149
votes
3 answers

Difference between Groovy Binary and Source release?

I have been seeing the words binary and source release in many websites download sections. What do they actually mean? For example, I have seen this in Groovy download page. My question is how they differ? Both tend to install Groovy, but what's…
Ant's
  • 13,545
  • 27
  • 98
  • 148
140
votes
7 answers

Cannot define variable in pipeline stage

I'm trying to create a declarative Jenkins pipeline script but having issues with simple variable declaration. Here is my script: pipeline { agent none stages { stage("first") { def foo = "foo" // fails with "WorkflowScript:…
Malcolm Crum
  • 4,345
  • 4
  • 30
  • 49
139
votes
1 answer

What is the effect of @NonCPS in a Jenkins pipeline script

I have a pipeline script in Jenkins. I used to get this exception: org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method groovy.json.JsonSlurperClassic parseText java.lang.String I looked the…
bsky
  • 19,326
  • 49
  • 155
  • 270
138
votes
10 answers

How to get just the parent directory name of a specific file

How to get ddd from the path name where the test.java resides. File file = new File("C:/aaa/bbb/ccc/ddd/test.java");
minil
  • 6,895
  • 16
  • 48
  • 55
138
votes
3 answers

Checking if a collection is null or empty in Groovy

I need to perform a null or empty check on a collection; I think that !members?.empty is incorrect. Is there a groovier way to write the following? if (members && !members.empty) { // Some Work }
Jay Bose
  • 1,511
  • 2
  • 11
  • 14