Showing posts with label Groovy. Show all posts
Showing posts with label Groovy. Show all posts

Sunday, November 09, 2014

Gradle : How to modify tasks post task graph creation

Gradle allows you to script your build file and add hooks in to the build lifecycle so you can maximize flexibility without complicating build setup and tasks.
One of the useful ways to manipulate all tasks once they are loaded by gradle into task execution graph is to use the whenReady lifecycle hook which only gets called when gradle  the complete tasks execution graph.This allows you to dynamically modify task properties, add new properties,etc. This comes helpful when you need to manipulate some tasks based on the user arguments passed in the project or some other project properties.

Below is the snippet that you need to add to make this work:
1:  gradle.taskGraph.whenReady {  
2:   tasks.each {  
3:    it.doFirst {   
4:     println "Calling ${it.name}"  
5:    }  
6:   }  
7:  }  

Output:
 sgokak ~Todo $ gradle build  
 :compileJava  
 Calling compileJava  
 :processResources UP-TO-DATE  
 :classes  
 Calling classes  
 :war  
 Calling war  
 :assemble  
 Calling assemble  
 :compileTestJava UP-TO-DATE  
 :processTestResources UP-TO-DATE  
 :testClasses  
 Calling testClasses  
 :test UP-TO-DATE  
 :check  
 Calling check  
 :build  
 Calling build  
   
 BUILD SUCCESSFUL  
   

As you can see from the above output, the doFirst Closure  is only executed when the task is actually executed and not for any UP-TO-DATE tasks.

Read more...

Tuesday, May 19, 2009

More Groovier collections

Ranges are a beautiful ways of mangaing feinite sequence with a defined start and end.


a=1..5
println a // prints [1,2,3,4,5]
println a.size() // prints 5
println a.from // prints 1
println a.to //prints 5

x='a'..'f'
println x // prints [a,b,c,d,e,f]


You can also use ranges in subscript way to get part of a list.

a=[1,2,3,4,5,6]
x = 2
println a[0..2] // printrs [1,2,3]


You can also loop over each element of a range simple as follows

a=[1,2,3,4,5,6]
a.each{
println it
}

this prints all elements on new line.

Ranges can work with any objects whose class implments the Commprable interface having next and previous methods.

Another interesting thing that happens with all collections in any programming langugae is looping over it.Groovy has various options to this in many elegant ways.

Classical for .
This is usual for loop found in C,C++,JAVA,etc.this makes you feel home

for(int i=0;i<5;i++)
println i // you know what it prints


Iterable for.
This for works with any class that implments iterable like ranges,lists,maps,etc.
For on ranges

for(i in 0..5)
println i


For on lists

for(i in [1,2,3,4,5])
println i


For on maps

m=[a:"ant",b:"bat",c:"cat"]
for(e in m)
println e.key + "=" + e.value


For on strings

for(i in "Groovy")
println i // prints ewach character on newline


Another flexible and intuitive nature added to collections in groovy is that all Empty collections are evaluated to false in all boolean checks.
Following all prints true .(trick question : try removing ! :)

println ![]
println !''
println ![:]


Feel free to share your Groovy tips and tricks in the comments section

Read more...

Sunday, April 19, 2009

Groovier collections

I have been groovying almost over a year now.There are many remarkable things that groovy do in a very simple and intuitive manner .If you haven't start using it, you are already late.Today , i am going to discuss how groovy takes using collection to another level.Currently, i use it for routine scripts and code generation.
Java Collections framework is one of the most used SDK API (esp. in enterprise applications).This Api allows you do handle ,manipulate and represent the Collection of any Java objects.This greatly reduces programming effort where as boosting programming performance and fostering reuse.
Groovy adds to this many useful operators and tweaks to make working with collection api more entertaining experience.
Following is the ways you create a List in groovy:


L=[0,1,2,3]
println L.class //prints class java.util.ArrayList

This showing that ArrayList is used by groovy behind the scenes to create a List representation.

To work with LinkedList do this

L = new LinkedList([0,1,2,3]);
println L.class


To access, any element from the List , you can use array-like notation familiar to most of Java developers.Other ways to retrieve the elements are get and getAt methods expecting the index (-ve index allowed).

println L[2] //prints 2
println L[-3] //prints 1
println L.get(0) //prints 0
println L.getAt(1) //prints 1

The most frequent operation done on a list is to add elements to it.This can be acheived using +=,<< or using add method.To merge 2 lists use, addAll or + operator.

L = [0,1,2,3]
L += 4
L << 5
L.add(6)
println L //prints [0,1,2,3,4,5,6]
L.addAll(["a","b"])
println L // prints [0,1,2,3,4,5,6,a,b]
K = L + [99,100]
println K // prints [0,1,2,3,4,5,6,a,b.99.100]


Removing an element is equally easy.Use -,-= or call minus.remove() method of list is too supported.

L = [0,1,2,3]
L -= [3]
println L //prints [0,1,2]
L = L.minus(2)
println L //prints [0,1]
L.remove(0)
println L //prints [1]


Reversing the content of the list can be done as

L = [0,1,2,3]
println L.reverse() //prints [3,2,1,0]


Using map is easier than ever, the content left to the : is the key and to the right is the value.

M= ['a':'A','b':'B','c':'C']
println M // prints [a:A,b:B,c:C]
println M.getClass() //prints java.util.LinkedHashMap.We cannot use .class as . is reserved to access the key-values
println new HashMap(['a':'A','b':'B','c':'C']) // prints [a:A,b:B,c:C]


Retreiving value can be done numerous ways.

M= ['a':'A','b':'B','c':'C']
println M.a // prints A
println M['a'] // prints A
println M.get('c') // prints C
println M.getAt('b') // prints B
println M.getAt('x') // prints null as no key is matched.


Adding new key-value pairs too has various alternative.

M= ['a':'A','b':'B','c':'C']
M['d'] = 'D'
println M // prints ['a':'A','b':'B','c':'C','d':'D']
M.put('e','E')
println M // prints ['a':'A','b':'B','c':'C','d':'D','e':'E']
M.putAt('f','F')
println M // prints ['a':'A','b':'B','c':'C','d':'D','e':'E','f':'F']

Remove a pair with remove method paasing the Key.

M= ['a':'A','b':'B','c':'C']
M.remove('b') = 'D'
println M // prints ['a':'A','c':'C']


i'll cover more groovy stuff in upcoming posts.
till then...happy Groovying ;)

P.S : Syntax high lighting help from here.

Read more...

Sociofluid