Thursday, June 28, 2012

Advantage of using Maven over ant.

What is Maven?

        It is a tool that can be used for build process and managing Java project. The way ant is used for build process of Java projects. But it does it very smartly and you can set-up your Java project very fast.

Advantage of using Maven over Ant.
  • With ant you have to do everything on your own, you have to write lot of code/configuration in build.xml. Where as maven is simple, easy and with minimal code you can start. 
  • The main important advantage you get is to manage your artifact ( could be .jar or .war ) by way of properly versioned artifactes. And easy way of managing dependencies (as in other libraries required by given project) of of a project. 
  • Another advantage it has which is more interesting for people who are using Eclipse as IDE. That is maven project can be directly imported into Eclipse with the help of few plugins for more details refer. And it will setup a required eclipse project by looking at your maven build script. You might say it is possible with ant also you just have to configure Ant builder in Eclipse and one can fire the build from eclipse but it simply calls ant build as it is the way it can be fired through command prompt. But you yourself have to configure and setup project by configuring all of its dependencies. There are ways to import ant build files but it fails to import some complicated ant projects. Where as in case of Maven it doesn't fire maven build as it is the way it will be fired through command prompt. When you import maven project into eclipse it sets up eclipse project on its own by configuring all of its dependencies. It also configures eclipse builders that are used to build given project, which will make it more easier for developers to work in properly configured eclipse project.
  • It has major and important feature to share your artifact with other people required in your company or even you can share with wide audience over Internet with the help of some openly available software tools.
  • It has life cycle phase in which your junits are invoked.
  • It enforces proper arrangement of your source code directories. 
  • I have seen in lot of the projects which uses ant as a build tool. Keep all their source files at one location. And generate multiple artifactes from the same source location by including and excluding some packages. This way of managing code makes it more difficult for a new person to understand the code arrangement and what goes in which artifact. This could be bad way of doing things, for which ant should not be blamed. But in contrary Maven don't allow you to generate multiple artifact by including few resources in one artifact and excluding those from others. That way you are forced to create another module and add dependency of that module where ever it is required. That makes your code more readable and segregated and looking at the dependency you can easily make out at what all places it is getting used.
refer this discussion for more detailed insights

look out for my next blog on Start with maven

Start with maven

Start with Maven

            To start with Maven lets create a sample application which will result into artifact packaged into .jar. Maven being build system for Java application, you need to have following software's installed on your machine and set few environment variables.
  • JDK may be latest version at least jdk 1.6.
  • Maven installation download latest version.
  • Set Maven's bin directory into path variable so that it is accessible from anywhere.
  • Set JAVA_HOME environment variable pointing to required JDK installation
 It is very easy to create simple jar project with the help of Maven archetype ( in short it is a Java project templating tool, which has different templates to create different kinds of projects. Which makes it easier for developer to setup the required project quickly with bare minimal configuration)

1:  mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart  

When you run above command it will run a wizard which will ask you few inputs following are the details for the same.

1:  Define value for property 'groupId': : com.sample  

A.  groupId: you need to provide meaningful group id to your module/artifact. Group Id is something which will help you categorize your artifacts. So when you have multiple modules you have clear segregation of those modules. It acts like namespace or package (most prominently similar to package concept inside java).

1:  Define value for property 'artifactId': : test  

B. artifactId: You need to provide some meaningful name to your module/project/artifact.

1:  Define value for property 'version': 1.0-SNAPSHOT: :  

C. version: You need to provide the version for given module/project. It shows default value as "1.0-SNAPSHOT". Will explain in detail in my next blog what's the meaning of SNAPSHOT. In short when you are developing your code and it is under development for long time. You should keep the version of your module in prefixed with "-SNAPSHOT".

1:  Define value for property 'package': com.sample: :  

D. package: This input is not exactly related to artifact. When I say exactly related it means its not part of the requirement to define a module/project in maven. This is being asked by this archetype is because this is quick start template. So it creates a sample project with sample "Hello World" app, to place the required Java class, it will ask you to provide some package name. As default value it will assume value of the groupId you have provided previously.

Once you enter all the values required by the wizard, it will show you entered values for required parameters and will ask you to confirm the same. Once you confirm the inputs. It will create a sample module/project with given input.

If you look into directory from where you run this quick start archetype, it must have created a directory with the same name as that of artifactId. And directory structure under that directory will be as follows

1:  test  
2:  |-- pom.xml  
3:  `-- src  
4:    |-- main  
5:    |  `-- java  
6:    |    `-- com  
7:    |      `-- sample  
8:    |        `--  
9:    |            
10:    `-- test  
11:      `-- java  
12:        `-- com  
13:          `-- sample  
14:            `--  

This quick start archetype creates a jar module. When I say jar module, packaging type of the module will be "jar".

To make a build from command prompt. Follow below commands.

1:  $ cd test  
2:  $ mvn clean package  

This will make a build, Maven creates target folder inside respective module folder, to generate build output, once you run above command to build the module. You will find a jar created inside test/target folder with the name "test-1.0-SNAPSHOT.jar". So final artifact will be generated as "<artifactId>-<version>.jar" for jar packaging type. One can override the default name with their own name by using <finalName> tag inside <build> tag inside your pom.xml file (refer pom reference).

To test the application which just compiled run following command

1:  $ java -cp target/test-1.0-SNAPSHOT.jar com.sample.App  

This should print out as

1:  Hello World!  

For more details refer getting started guide and maven in 5 mins.

To import the maven project inside eclipse to setup eclipse project refer