Saturday, April 25, 2015

Ant Cheat Sheet

This post list some of the commonly used ant commands and shortcuts.

Test if ant is installed
$ant -version
Ant is a command-line tool so it must be on the path to be used. If Ant is installed, above command will return version details otherwise it will say that ant command is not found (i.e. Ant is not installed).

It gives below response when I run it on my Mac:
Apache Ant(TM) version 1.9.2 compiled on July 8 2013

Running ant target(s)
$ant                                     //runs default target
$ant compile                       //runs compile target
$ant <target1> <target2>    //runs multiple targets

Generate verbose log during build
$ant -verbose        //runs default target in verbose mode
$ant <target> -verbose
$ant <target> -v   // v is shortcut for verbose
Ant produces verbose log when invoked with -verbose (or -v) parameter. This is useful to figure out what's going out during build and specially when build fails.

Generate debug log during build
$ant -debug                 //runs default target in debug mode
Debug prints more log information than verbose. This option prints lot more low-level details along with verbose details.
$ant -quiet                  //to see nothing but errors and final build status message
$ant -keep-going        //tells ant to try to recover from failure

List all targets
$ant -projecthelp        //or -p
$ant -p -verbose         // list sub-targets as well
Lists all the targets provided by build file. Ant list only public targets with optional description attribute. To see optional targets also along with main targets run above command with -verbose.

Pass a property dynamically

$ant execute -Dproperty=value
Passes a key value (property) pair to the ant build. This will override if the property already exist in the build or config file.

Get build sequence
Target dependency is transitive. So if the build file is quite huge then knowing the dependency becomes quite difficult. To get it, I often run the build in verbose mode and then grep for the given String.

$ant -v main | grep "Complete build sequence is"
Complete build sequence is [clean, compile, execute, main, ]

Help
$ant -help     //or -h
If you don't remember this cheat sheet, just ask ant to help. It will give all the options which ant supports. I have covered some of them here, but some other useful options like providing your own build file other than build.xml etc are not covered here.



    

Tuesday, April 14, 2015

Functional Interfaces in Java 8

This post compliments the previous post where I have discussed about Lambda Expression, link. To unleash the power of lambda, a good understanding of functional interface is must. Let's briefly cover it:

Functional Interface

Functional Interfaces are those Interfaces in Java which has only one abstract method. Interfaces having single method (like Runnable, Callable, Comparable etc) existed in Java even before version 8. It's just that this new term was coined in context of Lamda Expression.

Functional interfaces are pre-condition for Lambda expression. Lambda expression allows you to provide the implementation of the method inline as an anonymous implementation of the interface.   Before Java 8, programmers used to provide implementation of functional interfaces using Anonymous class (which let you declare and instantiate the class at same time). Below is mostly commonly seen way to implement a Runnable task. 

  Runnable task = new Runnable(){
    public void run(){
        //implementation...bla bla.
  }
};

If interface is NOT functional, implementing the interface or providing anonymous implementation as shown above are two options. But if there is just one abstract method in the interface, Java 8 provides a more succinct and less verbose style to provide implementation. Java also introduces an optional annotation to mark an interface as functional. 


@FunctionalInterface
public interface MyFunctionalIf {
public String doSomething(String);

}

Annotation is used to indicate that the interface is intended to be functional interface. It's not mandatory to use the annotation but definitely a good practice. This annotation will also prevent you to accidentally add another method and break the functional contract of having just a single method. One important point to keep in mind is that default and static methods don't break the functional contract. 
Above interface can be used by implementing it in a concrete class or by providing ad-hoc implementation using anonymous class as shown for Runnable. 

Implementing Functional Interface using Lambdas

Functions were not first class citizen (before Java 8) and you need to have a handle of object to call/use method. Now let's see how lambda changes the implementation of functional interfaces:

Runnable lambdaTask = () -> { //implementation  };
lambdaTask.run();  //using lambda

MyFunctionalIf myFunctionalIf = (String message) ->  "hello "+ message;
myFunctionalIf.doSomething("Sid");   //using lambda

                                                                                   
Now, look how cleaner the functional implementation of the interfaces look. It's very concise, flexible and re-usable.

Please note that in second case, the curly brace as well as return clause is missing (both can be removed if there is only one line in the method body. Even type in argument is optional, so you can ignore String in above lambda expression. Notice that the name of the method is missing in lambda so it is also called as anonymous method

Default Method

Now interfaces can also provide a default methods. Obviously, functional interfaces can also have any number of default methods. You just need to add default keyword in method definition.

List<> interface adds a sort method so that you can sort a list by calling sort method on its instance instead of calling sort method from Collections utility class.

default void sort(Comparator<? super E> c) {
     //implementation 
}

In-built Functional Interfaces Added In Java 8

Imagine creating your own multiple functional interfaces. But, Java helps programmers by providing some reusable functional interfaces out of the box. 

Java designers have created a separate package for same, java.util.funtion.

So do check this package before creating your own functional interfaces. Chances are high that you will find one matching your needs.

---
keep coding !!!