Month: September 2015

Differences between Ant and Maven

Posted on


The differences between Ant and Maven in this example?

Ant doesn’t have formal conventions like a common project directory structure, you have to tell Ant exactly where to find the source and where to put the output. Informal conventions have emerged over time, but they haven’t been codified into the product.

  • Ant is procedural, you have to tell Ant exactly what to do and when to do it. You had to tell it to compile, then copy, then compress.
  • Ant doesn’t have a lifecycle, you had to define goals and goal dependencies. You had to attach a sequence of tasks to each goal manually.
  • Ant is mainly a build tool.

Where Maven…

  • Maven has conventions, it already knew where your source code was because you followed the convention. It put the bytecode in target/classes, and it produced a JAR file in target.
  • Maven is declarative. All you had to do was create a pom.xml file and put your source in the default directory. Maven took care of the rest.
  • Maven has a lifecycle, which you invoked when you executed mvn install. This command told Maven to execute a series of sequence steps until it reached the lifecycle. As a side-effect of this journey through the lifecycle, Maven executed a number of default plugin goals which did things like compile and create a JAR.
  • Maven is a project and dependencies management tool which build project as well.
  • Maven support modules base project integratio.

Default life cycle of Maven :


  • validate – validate the project is correct and all necessary information is available
  • compile – compile the source code of the project
  • test – test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package – take the compiled code and package it in its distributable format, such as a JAR.
  • integration-test – process and deploy the package if necessary into an environment where integration tests can be run
  • verify – run any checks to verify the package is valid and meets quality criteria
  • install – install the package into the local repository, for use as a dependency in other projects locally
  • deploy – done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

How to exclude jars generated by maven war plugin ?

Posted on Updated on

There are various way to exclude jar files generated by Maven pom file.

1. Using <packagingExcludes> tags we can exclude files.


2. We can excluded jar base on their artifactid or groupid.



Hidden Commands of Maven

mvn tree

view the dependency hierarchy of the project currently being built.

mvn dependency:analyze

performs byte code analysis to determine missing or unused dependencies. This goal is meant to be launched from the command line. It will fork the build and execute test-compile so there are class files to analyze. If you want to bind analyze in your pom, use the dependency:analyze-only mojo instead.

How to sort Map by key and value in Java.

Posted on Updated on


I believe, you have the knowledge of Map data structure  nevertheless sorting over Map’s Keys and Values is also most tricky and frequently asked interview question today. Since we know that Map does not guarantee any sorting order but we can do it by some additional programming logic ( it’s not recommended to sort over large dataset because feeding the data into Map and sort it again adding extra cost )

Although construction of Map is used to array of Entry Object containing Key and Value  object and a KeySet() represents a group of unique key data structure but not sorted and similarly values() represents a group of Value elements but also not sorted way.

Below, given a program that demonstrated how to sort Map’s Element by Key or Values.

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class MapSorts {
    Map<String,String> map = new HashMap<String,String>();
    //SortedMap<String,String> map = new TreeMap(new MyCompatator());
    public static void main(String args[]){
    	MapSorts p = new MapSorts();
        //; // default sorting 
        //; // custom sorting by value p.customKeySort(; // custom sorting by key
        Iterator it =;
            String key =;
            System.out.println(key+" >> ";

    public  Map<key,Val> DefaultKeySort(Map<key,Val> map){
        List keys = new ArrayList(map.keySet());
        Map sortedMap = new LinkedHashMap();
        for(Object key: keys){
            sortedMap.put(key, map.get(key));
        return sortedMap;
    public  Map<key,Val> customValueSort(Map<key,Val> map){
        List<Entry<key, Val>> Val = new ArrayList<Entry<key, Val>>(map.entrySet());
        Collections.sort(Val,new Comparator<Entry<key, Val>>() {

            public int compare(Entry<key, Val> o1, Entry<key, Val> o2) {
                return o1.getValue().compareTo(o2.getKey());
        Map sortedMap = new LinkedHashMap();
        for(Map.Entry<key, Val> entity: Val){
            sortedMap.put(entity.getKey(), entity.getValue());
        return sortedMap;
    public  Map<key,Val> customKeySort(Map<key,Val> map){
        List list = new ArrayList(map.keySet());
        Collections.sort(list,new Comparator() {

            public int compare(key o1, key o2) {
                return o1.compareTo(o2);
        Map sortedMap = new LinkedHashMap();
        for(key k: list){
            sortedMap.put(k, map.get(k));
        return sortedMap;

I hope it will be helpful.