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

What is the branch name variable for Jenkins multibranch pipelines?

I need to know which branch is being built in my Jenkins multibranch pipeline in order for it to run steps correctly. We are using a gitflow pattern with dev, release, and master branches that all are used to create artifacts. The dev branch auto…
Christian Rigdon
  • 883
  • 1
  • 6
  • 6
86
votes
2 answers

Groovy XmlSlurper vs XmlParser

I searched for a while on this topic and found some results too, which I am mentioning at the end of post. Can someone help me precisely answer these three questions for the cases listed below them? For which use-cases using XmlSluper makes more…
kdabir
  • 9,623
  • 3
  • 43
  • 45
86
votes
10 answers

Copy entire directory contents to another directory?

Method to copy entire directory contents to another directory in java or groovy?
Sanal MS
  • 2,424
  • 4
  • 24
  • 31
85
votes
6 answers

How do you load a groovy file and execute it

I have a jenkinsfile dropped into the root of my project and would like to pull in a groovy file for my pipeline and execute it. The only way that I've been able to get this to work is to create a separate project and use the fileLoader.fromGit…
user301693
  • 2,377
  • 7
  • 22
  • 24
85
votes
3 answers

Groovy - Convert object to JSON string

I'm pretty used to Grails converters, where you can convert any object to a JSON representation just like this (http://grails.org/Converters+Reference) return foo as JSON But in plain groovy, I cannot find an easy way to do this…
Wavyx
  • 1,715
  • 2
  • 14
  • 23
84
votes
6 answers

How to capture arguments passed to a Groovy script?

I am just starting out with Groovy. I couldn't find any examples anywhere of how to handle arguments to a Groovy script and so I hacked this method myself. There must be a better way of doing this? If so, I am looking for this better way, since…
djangofan
  • 28,471
  • 61
  • 196
  • 289
84
votes
2 answers

How to print a Groovy variable in Jenkins?

I have the following code within a Jenkins pipeline: stage ('Question') { try { timeout(time: 1, unit: 'MINUTES') { userInput = input message: 'Choose server to publish to:', ok: '', parameters: [ [$class:…
Itai Ganot
  • 5,873
  • 18
  • 56
  • 99
83
votes
9 answers

Show a Jenkins pipeline stage as failed without failing the whole job

Here's the code I'm playing with node { stage 'build' echo 'build' stage 'tests' echo 'tests' stage 'end-to-end-tests' def e2e = build job:'end-to-end-tests', propagate: false result = e2e.result if…
techgnosis
  • 1,879
  • 2
  • 17
  • 19
83
votes
15 answers

Found shared references to a collection org.hibernate.HibernateException

I got this error message: error: Found shared references to a collection: Person.relatedPersons When I tried to execute…
nightingale2k1
  • 10,095
  • 15
  • 70
  • 96
83
votes
1 answer

How to encode URL in Groovy?

Is there a kind of URLEncode in Groovy? I can't find any String → String URL encoding utility. Example: dehydrogenase (NADP+) → dehydrogenase%20(NADP%2b) (+ instead of %20 would also be acceptable, as some implementations do that)
Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
82
votes
5 answers

Using variables in Gradle build script

I am using Gradle in my project. I have a task for doing some extra configuration with my war. I need to build a string to use in my task like, lets say I have: task extraStuff{ doStuff 'org.springframework:spring-web:3.0.6.RELEASE@war' } This…
huzeyfe
  • 3,554
  • 6
  • 39
  • 49
80
votes
5 answers

How to read a properties files and use the values in project Gradle script?

I am working on a Gradle script where I need to read the local.properties file and use the values in the properties file in build.gradle. I am doing it in the below manner. I ran the below script and it is now throwing an error, but it is also not…
unknown
  • 1,815
  • 3
  • 26
  • 51
80
votes
12 answers

Converting File to MultiPartFile

Is there any way to convert a File object to MultiPartFile? So that I can send that object to methods that accept the objects of MultiPartFile interface? File myFile = new File("/path/to/the/file.txt") MultiPartFile ....? def (MultiPartFile file)…
birdy
  • 9,286
  • 24
  • 107
  • 171
80
votes
5 answers

Gradle Single vs Double Quotes

I'm new to gradle and am currently just trying to follow the tutorials and quite a few times I've seen single and double quotes intermixed. I just wanted to know if there was a difference of when one set should be used over the other. One example of…
Dan W
  • 5,718
  • 4
  • 33
  • 44
78
votes
3 answers

How do I implement a retry option for failed stages in Jenkins pipelines?

I have a Jenkinsfile with multiple stages and one of them is in fact another job (the deploy one) which can fail in some cases. I know that I can made prompts using Jenkinsfile but I don't really know how to implement a retry mechanism for this…
sorin
  • 161,544
  • 178
  • 535
  • 806