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

Named parameters

I have method def test(String a, String b) { } and I would like to call this with a dynamic parameter map. I always though that test(['1','2']); //valid call and also test([a:'1',b:'2']); //=> does not work will work. but it doesn't. So I…
rdmueller
  • 10,742
  • 10
  • 69
  • 126
65
votes
3 answers

Groovy different results on using equals() and == on a GStringImpl

According to the Groovy docs, the == is just a "clever" equals() as it also takes care of avoiding NullPointerException: Java’s == is actually Groovy’s is() method, and Groovy’s == is a clever equals()! [...] But to do the usual equals()…
Anuj Arora
  • 3,017
  • 3
  • 29
  • 43
65
votes
7 answers

How to access Grails configuration in Grails 2.0?

I have obtained the latest Grails 2.0 milestone, and I am seeing a deprecation warning for the ConfigurationHolder class: org.codehaus.groovy.grails.commons.ConfigurationHolder The deprecation message simply says "Use dependency injection instead"…
DigitalZebra
  • 39,494
  • 39
  • 114
  • 146
65
votes
3 answers

how to accept multiple parameters from returning function in groovy

I want to return multiple values from a function written in groovy and receive them , but i am getting an error class org.codehaus.groovy.ast.expr.ListExpression, with its value '[a, b]', is a bad expression as the left hand side of an…
Nikhil Sharma
  • 2,221
  • 6
  • 25
  • 31
65
votes
2 answers

Jenkins how to create pipeline manual step

Prior Jenkins2 I was using Build Pipeline Plugin to build and manually deploy application to server. Old configuration: That works great, but I want to use new Jenkins pipeline, generated from groovy script (Jenkinsfile), to create manual step. So…
Zigac
  • 1,551
  • 1
  • 16
  • 27
64
votes
9 answers

Groovy not in collection

The idiomatic way to check if a list contains something in Groovy is to use in. if ('b' in ['a', 'b', 'c']) But how do you nicely check if something is not in a collection? if (!('g' in ['a', 'b', 'c'])) Using logical not seems messy and the ! is…
Bob Herrmann
  • 9,458
  • 10
  • 38
  • 45
64
votes
1 answer

How to define and use function inside Jenkins Pipeline config?

I'm trying to create a task with a function inside: def doCopyMibArtefactsHere(projectName) { step ([ $class: 'CopyArtifact', projectName: $projectName, filter: '**/**.mib', fingerprintArtifacts: true, …
Dr.eel
  • 1,837
  • 3
  • 18
  • 28
64
votes
5 answers

groovy: safely find a key in a map and return its value

I want to find a specific key in a given map. If the key is found, I then want to get the value of that key from the map. This is what I managed so far: def mymap = [name:"Gromit", likes:"cheese", id:1234] def x = mymap.find{ it.key == "likes"…
nonbeing
  • 6,907
  • 6
  • 36
  • 46
63
votes
16 answers

Generate A Weighted Random Number

I'm trying to devise a (good) way to choose a random number from a range of possible numbers where each number in the range is given a weight. To put it simply: given the range of numbers (0,1,2) choose a number where 0 has an 80% probability of…
Todd Sharp
  • 3,207
  • 2
  • 19
  • 27
63
votes
4 answers

Gradle: getting the root project directory path when starting with a custom build file

The structure of my Gradle project is the following: Project ├── app └── build.gradle ├── foo └── bar.txt · · · └── build.gradle Normally to get the absolute path of the foo folder I can just simply do new File('foo').getAbsolutePath() in…
Roberto Leinardi
  • 10,641
  • 6
  • 65
  • 69
63
votes
3 answers

How do I use String interpolation in a Groovy multiline string?

In Groovy, I have a multiline String, defined with ''', in which I need to use interpolation in order to substitute some other variables. For all my efforts, I can't get it to work -- I assume I need to escape something, which I'm missing. Here's…
gsaslis
  • 3,066
  • 2
  • 26
  • 32
61
votes
5 answers

Try-catch block in Jenkins pipeline script

I'm trying to use the following code to execute builds, and in the end, execute post build actions when builds were successful. Still, I get a MultipleCompilationErrorsException, saying that my try block is Not a valid section definition. Please…
lenkovi
  • 1,708
  • 2
  • 11
  • 16
61
votes
5 answers

Can I define multiple agent labels in a declarative Jenkins Pipeline?

I'm using declarative Jenkins pipelines to run some of my build pipelines and was wondering if it is possible to define multiple agent labels. I have a number of build agents hooked up to my Jenkins and would like for this specific pipeline to be…
FrontSide
  • 1,128
  • 2
  • 10
  • 12
61
votes
2 answers

How to set variables in a multi-line shell script within Jenkins Groovy?

Suppose I have a Groovy script in Jenkins that contains a multi-line shell script. How can I set and use a variable within that script? The normal way produces an error: sh """ foo='bar' echo $foo """ Caught:…
Fo.
  • 3,752
  • 7
  • 28
  • 44
61
votes
3 answers

Groovy, "try-with-resources" construction alternative

I'm a new to Groovy. I used to use 'try-with-resources' construction in my Java code during work with I/O streams. Could you please advise, is there any analogue of such construction in Groovy?
XZen
  • 225
  • 5
  • 27
  • 49