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
61
votes
2 answers

How to check if a directory containing a file exist?

I am using groovy to create a file like "../A/B/file.txt". To do this, I have created a service and pass the file path to be created as an argument. This service is then used by a Job. The Job will do the logic in creating the file in the specified…
chemilleX3
  • 1,176
  • 5
  • 17
  • 27
60
votes
2 answers

Groovy, how to iterate a list with an index

With all the shorthand ways of doing things in Groovy, there's got to be an easier way to iterate a list while having access to an iteration index. for(i in 0 .. list.size()-1) { println list.get(i) } Is there no implicit index in a basic for…
raffian
  • 31,267
  • 26
  • 103
  • 174
59
votes
4 answers

Using "$" in Groovy

I see { } are used for closures, and then I believe when a $ is put in front of braces, it is simply doing a variable substitution within a string. I can't find the documentation on how the $ works in the reference ... hard to search on it…
Ray
  • 5,885
  • 16
  • 61
  • 97
59
votes
6 answers

How to remove Duplicate Values from a list in groovy

I have a collection of ID list to be saved into the database if(!session.ids) session.ids = [] session.ids.add(params.id) and I found out that list has duplicates, like [1, 2, 4, 9, 7, 10, 8, 6, 6, 5] Then I wanted to remove all duplicates by…
Develop4Life
  • 7,581
  • 8
  • 58
  • 76
59
votes
4 answers

Java/Groovy - simple date reformatting

I'm new to Java/Groovy development and I have a simple string that I would like to reformat, however I get an 'Unparseable date' error when I attempt to run the following: import java.text.SimpleDateFormat import java.util.Date String oldDate Date…
DC Guy
  • 593
  • 1
  • 4
  • 4
58
votes
2 answers

Get key in groovy maps

def map = [name:"Gromit", likes:"cheese", id:1234] I would like to access map in such a way that I can get the key something like the output should be map.keys returns array of string. basically i just want to get the keys output: name likes id
kumar
  • 8,207
  • 20
  • 85
  • 176
58
votes
9 answers

Load Jenkins Pipeline Shared Library from same repository

TL;DR Is there a way to import code into the Jenkinsfile from the local repository (other than the load step)? Why? I've experienced that for complex builds the Jenkinsfile gets kind of bulky and not very maintainable. Now that the build job is…
schnatterer
  • 7,525
  • 7
  • 61
  • 80
58
votes
1 answer

Difference between @Delegate, @Mixin and Traits in Groovy?

Would someone explain when I would want to use Groovy Traits vs. Mixins (@Mixin) vs. Delegates (@Delegate)? Maybe some trade-offs and design concerns would help. They all seem to allow for reusing multiple "classes" of behavior. Thanks. :-) This SO…
Vahid Pazirandeh
  • 1,552
  • 3
  • 13
  • 29
58
votes
8 answers

Access to build environment variables from a groovy script in a Jenkins build step (Windows)

I'm using Scriptler plugin, so I can run a groovy script as a build step. My Jenkins slaves are running on windows in service mode. With scriptler, I don't need to use windows batch scripts. But I have trouble to get the environment variables in a…
Adam Ocsvari
  • 8,056
  • 2
  • 17
  • 30
58
votes
7 answers

Strip indent in Groovy multiline strings

Unfortunately stripIndent on a multiline string does not work. Side note: My IDE code style preferences allow only space indentation (tabs will be replaced by spaces). But I think this should have no matter. def s = """ This is …
Cengiz
  • 5,375
  • 6
  • 52
  • 77
58
votes
1 answer

calling .each{} on IntRange returns the range not each integer

I think I've got some funny expectations... I want to iterate the numbers from 1 to 10. As a while loop it goes like this: def countMe = 1 while (countMe<11) { println countMe countMe++ } I was expecting that the following would do this…
Mikey
  • 4,692
  • 10
  • 45
  • 73
57
votes
5 answers

how to parse json using groovy

I want to parse JSON data which is coming in like: { "212315952136472": { "id": "212315952136472", "name": "Ready", "picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/195762_212315952136472_4343686_s.jpg", "link":…
Nikhil Sharma
  • 2,221
  • 6
  • 25
  • 31
57
votes
1 answer

How do I convert a Groovy String collection to a Java String Array?

I'm trying to call a methond on a Java class from a Groovy class. The Java method has a String array as a parameter, and I have a collection of Strings in my Groovy class. How do I convert the Groovy collection to a Java String array? Java…
Kevin Williams
  • 2,588
  • 3
  • 24
  • 25
57
votes
11 answers

Is it possible to Git merge / push using Jenkins pipeline

I am trying to create a Jenkins workflow using a Jenkinsfile. All I want it to do is monitor the 'develop' branch for changes. When a change occurs, I want it to git tag and merge to master. I am using the GitSCM Step but the only thing that it…
Progger
  • 2,266
  • 4
  • 27
  • 51
57
votes
9 answers

BUG! exception in phase 'semantic analysis'

I have a 1.1.7 spring-boot app using Gradle 1.10 & jdk1.8. I use Groovy/Spock for testing It has two dependencies - jars build with Apache Maven 3.1.1 and jdk 1.8. I build the jars and them copy them into the /lib directory. Then I try and build…
sonoerin
  • 5,015
  • 23
  • 75
  • 132