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
108
votes
5 answers

Get a list of all the files in a directory (recursive)

I'm trying to get (not print, that's easy) the list of files in a directory and its sub directories. I've tried: def folder = "C:\\DevEnv\\Projects\\Generic"; def baseDir = new File(folder); files = baseDir.listFiles(); I only get the directories.…
Yossale
  • 14,165
  • 22
  • 82
  • 109
108
votes
5 answers

How to access java-classes in the default-package?

I'm working now together with others in a grails project. I have to write some Java-classes. But I need access to an searchable object created with groovy. It seems, that this object has to be placed in the default-package. My question is: Is there…
Mnementh
  • 50,487
  • 48
  • 148
  • 202
107
votes
8 answers

Hibernate: Automatically creating/updating the db tables based on entity classes

I have the following entity class (in Groovy): import javax.persistence.Entity import javax.persistence.Id import javax.persistence.GeneratedValue import javax.persistence.GenerationType @Entity public class ServerNode { @Id …
Jason Miesionczek
  • 14,268
  • 17
  • 76
  • 108
107
votes
3 answers

String concatenation with Groovy

What is the best (idiomatic) way to concatenate Strings in Groovy? Option 1: calculateAccountNumber(bank, branch, checkDigit, account) { bank + branch + checkDigit + account } Option 2: calculateAccountNumber(bank, branch, checkDigit, account)…
Arturo Herrero
  • 12,772
  • 11
  • 42
  • 73
105
votes
1 answer

Groovy map method of collections

Is there a map method in Groovy? I want to do something like I do with the following Scala snippet: scala> val l = List(1, 2, 3) l: List[Int] = List(1, 2, 3) scala> l.map(_ + 1) res0: List[Int] = List(2, 3, 4)
deamon
  • 89,107
  • 111
  • 320
  • 448
105
votes
8 answers

How to define and call custom methods in build.gradle?

As part of my project, I need to read files from a directory and do some operations all these in build script. For each file, the operation is the same(reading some SQL queries and execute it). I think its a repetitive task and better to write…
Tomin
  • 1,898
  • 3
  • 18
  • 23
103
votes
1 answer

Clojure 1.2.1/1.3/1.4 'proxy generated in Grails 2.0.0 runtime fails. 1.2.0 is fine

I'm working on extending the Grails Clojure plugin in Grails 2.0.0 (and 2.1.0-SNAPSHOT) and I wanted to update it to Clojure 1.3.0 and add clojure.tools.logging. Clojure throws an exception during compilation of a proxy of a ByteArrayOutputStream…
John Courtland
  • 1,055
  • 1
  • 7
  • 10
103
votes
4 answers

What are "Groovy" and "Grails" and what kinds of applications are built using them?

Nowadays I hear a lot about "Groovy on Grails" and I want to know more about it: What is Groovy? What is Grails? What kind of applications are built using Groovy on Grails?
Rachel
  • 100,387
  • 116
  • 269
  • 365
98
votes
10 answers

How to access parameters in a Parameterized Build?

How do you access parameters set in the "This build is parameterized" section of a "Workflow" Jenkins job? TEST CASE Create a WORKFLOW job. Enable "This build is parameterized". Add a STRING PARAMETER foo with default value bar text. Add the code…
Vizionz
  • 1,137
  • 1
  • 7
  • 11
97
votes
8 answers

IntelliJ IDEA and Gradle - Cannot be applied to '(groovy.lang.Closure)'

I have a Gradle file which, whenever I load open it in IntelliJ IDEA 14.1.5, shows IDE errors for the entire file. Namely all the errors seem to be either: java.lang.String errors or groovy.lang.Closure errors I've tried clearing the file's…
Seb Charrot
  • 2,816
  • 1
  • 22
  • 24
96
votes
2 answers

Does Groovy have method to merge 2 maps?

First map is default options [a: true, b: false]. Second map - options passed by user [a:false]. Does Groovy has maps merge method to obtain [a: false, b:false]? It's not problem to implement it in Groovy. I'm asking about method out of the box
fedor.belov
  • 22,343
  • 26
  • 89
  • 134
94
votes
4 answers

How to create methods in Jenkins Declarative pipeline?

In Jenkins scripted pipeline we are able to create methods and can call them. Is it possible also in the Jenkins declarative pipeline? And how?
93
votes
8 answers

Run bash command on jenkins pipeline

Inside a groovy script (for a jenkins pipeline): How can I run a bash command instead of a sh command? I have tried the following: Call "#!/bin/bash" inside the sh call: stage('Setting the variables values') { steps { sh ''' …
Yago Azedias
  • 4,480
  • 3
  • 17
  • 31
93
votes
2 answers

Understanding the groovy syntax in a gradle task definition

I am new to Gradle and Groovy and trying to understand what is happening at the level of groovy when a gradle task is defined. task hello { println "configuring task hello" doLast { println "hello there" } } From reading the…
ams
  • 60,316
  • 68
  • 200
  • 288
90
votes
4 answers

Jenkins pipeline if else not working

I am creating a sample jenkins pipeline, here is the code. pipeline { agent any stages { stage('test') { steps { sh 'echo hello' } } stage('test1') { …
Shahzeb Khan
  • 3,582
  • 8
  • 45
  • 79