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
74
votes
6 answers

How do you get the path of the running script in groovy?

I'm writing a groovy script that I want to be controlled via a properties file stored in the same folder. However, I want to be able to call this script from anywhere. When I run the script it always looks for the properties file based on where it…
Dan Woodward
  • 2,559
  • 1
  • 16
  • 19
73
votes
5 answers

How to limit Jenkins concurrent multibranch pipeline builds?

I am looking at limiting the number of concurrent builds to a specific number in Jenkins, leveraging the multibranch pipeline workflow but haven't found any good way to do this in the docs or google. Some docs say this can be accomplished using…
jmreicha
  • 3,155
  • 7
  • 32
  • 39
72
votes
4 answers

Global constants in Groovy

It is often desired to declare constants at the top of a script that can be referenced anywhere else in the script. In Groovy, it seems that if you declare a constant using final then it isnot accessible in child scopes. What is the solution for…
justGroovy
  • 721
  • 1
  • 5
  • 3
72
votes
2 answers

Array of strings in groovy

In ruby, there is a indiom to create a array of strings like this: names = %w( lucas Fred Mary ) Is there something like that in groovy?
Lucas
  • 3,059
  • 5
  • 33
  • 48
72
votes
21 answers

Is Grails (now) worth it?

I know this is a duplicate, however, the Grails world has moved on considerably since that question was asked more than a year ago, as has the IDE support in Eclipse, so please don't just blindly close it. I thought the answer was yes and have…
Simon
  • 78,655
  • 25
  • 88
  • 118
72
votes
7 answers

Spock - Testing Exceptions with Data Tables

How can exceptions be tested in a nice way (e.g. data tables) with Spock? Example: Having a method validateUser that can throw exceptions with different messages or no exception if the user is valid. The specification class itself: class User {…
René Scheibe
  • 1,970
  • 2
  • 14
  • 20
72
votes
3 answers

How to write a conditional collect in groovy?

Imagine I have this structure: class Foo { String bar } Now imagine I have several instance of Foo whose bar value is baz_1, baz_2, and zab_3. I want to write a collect statement that only collects the bar values which contain the text baz. I…
ubiquibacon
  • 10,451
  • 28
  • 109
  • 179
70
votes
7 answers

How to pass boolean parameter value in pipeline to downstream jobs?

I'm using Jenkins v2.1 with the integrated delivery pipeline feature (https://jenkins.io/solutions/pipeline/) to orchestrate two existing builds (build and deploy). In my parameterized build I have 3 user parameters setup, which also needs to be…
Bram Gerritsen
  • 7,178
  • 4
  • 35
  • 45
70
votes
1 answer

How do I add an element to a list in Groovy?

Let's say I've got a list, like this... def myList = ["first", 2, "third", 4.0]; How do I add (push) an element to the end of it? I come from a PHP background, and there I would just do something like $myList[] = "fifth";. What's the equivalent of…
soapergem
  • 9,263
  • 18
  • 96
  • 152
70
votes
6 answers

Best pattern for simulating "continue" in Groovy closure

It seems that Groovy does not support break and continue from within a closure. What is the best way to simulate this? revs.eachLine { line -> if (line ==~ /-{28}/) { // continue to next line... } }
talanb
  • 994
  • 1
  • 8
  • 13
69
votes
8 answers

Remove null items from a list in Groovy

What is the best way to remove null items from a list in Groovy? ex: [null, 30, null] want to return: [30]
RyanLynch
  • 2,987
  • 3
  • 35
  • 48
67
votes
8 answers

How is the performance of Groovy compared with Java?

What the performance of Groovy compared with Java?
user607498
  • 789
  • 1
  • 5
  • 4
67
votes
7 answers

Elegant way for do ... while in groovy

How to do code something like this in groovy? do { x.doIt() } while (!x.isFinished()) Because there is no do ... while syntax in groovy. No 'do ... while()' syntax as yet. Due to ambiguity, we've not yet added support for do .. while to…
MariuszS
  • 30,646
  • 12
  • 114
  • 155
67
votes
2 answers

How do I find out Groovy runtime version from a running app?

I need to be able to find out the version of Groovy runtime via a groovy script. How do I do it? The examples I found on the net show a way, but apparently it has been deprecated and removed in the latest version.
Alex Khvatov
  • 1,415
  • 3
  • 14
  • 23
66
votes
7 answers

Jenkinsfile Declarative Pipeline defining dynamic env vars

I'm new to Jenkins pipeline; I'm defining a declarative syntax pipeline and I don't know if I can solve my problem, because I didn't find a solution. In this example, I need to pass a variable to ansible plugin (in old version I use an ENV_VAR or…
DEL
  • 663
  • 1
  • 6
  • 6