Sunday, January 9, 2011

Apache Maven Tutorial - Basic setup (Eclipse)

In this post, I will show how to setup Apache Maven in Eclipse and to run a basic program by using Maven.

First of all, what is Maven? In my own words, Maven is a project management, dependency management and build tool. I will explain it further a little bit.
Lets say in your organization, you have various teams and there is a possibility that each team might use the code written by other teams. If the code written for various projects by all the teams follow a particular folder structure/setup, it becomes really easy to build those projects and setup continuous integration etc.
Lets say Team A writes a java application and bundles it as a jar file. Team B writes a JEE application and bundles it as a war/ear file. Team C wants to reuse some methods and classes in Team A's jar file.

Team A sets up its project structure using Maven's standard folder structure. Example would be putting all the java classes in src/main/java folder. It would put all of its properties files under src/main/resources etc. And then there is pom.xml, which is the file that manages the project. This xml is the heart of each project and explains what other jar files are needed for building this project, how to package the code (as a jar/war etc.) and some other details.
In short, Team A creates a Maven project following the standard directory structure and creates a pom.xml. After finishing coding the java classes, there are some commands that can be invoked which read the xml and generate the jar/war file and install them in a repository somewhere.

Now Team C, also creates its own project by following the standard directory structure. Now, instead of writing the Utility classes again, it just adds the Project A's jar file as a dependency in it's(Project C's) pom.xml. So, when the build commands are invoked, Maven download's Project A's jar file from the repository so that Project C can use them(using imports in java classes).

Please note that the above example is just one aspect of Maven.

Ok, enough talk. Lets start with an example.

Im going to use eclipse for this tutorial. You can download eclipse from http://www.eclipse.org/downloads/

Download the latest version of Maven from http://maven.apache.org/download.html. Extract to a folder like C:\Maven

In Eclipse, install the Maven plugin:
Go to Help -> Install New Software and then Click on Add button and enter the following URL for the update site
http://m2eclipse.sonatype.org/installing-m2eclipse.html
Follow the instructions on the screen and finish the plugin installation

After the plugin is installed, in Eclipse, click on File -> New -> Maven -> Maven Project. Click on Next. On this page, select the first checkbox (Create a simple project). Click Next.
Enter the groupId, ArtifactId, Name, Descpription etc. GroupId is something like your organization name(enter abc), ArtifactId is something like a project name(enter Sample). Click Next and click Finish. Voila. You now have your first Maven project setup. The plugin automatically creates a lot of default folder using the standard maven directory structure.
If you look at the Project Explorer for the 'Sample' project, you can see src/main/java, src/main/resources, some other folders and target, pom.xml.
You can open pom.xml and look at the xml file. It contains the details that we provided during the project creation.

Now, let us create a simple java class in this project. In Package explore, right click on src/main/java folder and click on New -> Class. Enter a package name as code and class name as . SampleClass. Check the checkbox to create a main method. Click on Finish.

We now have the SampleClass.java that has the following code.
package code;
public class SampleClass {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated
}
}

Now, lets say in this class we want to do some functionality. Like copying the contents of a directory to another directory. Instead of writing the code to do the copy the contents, we will use Maven's ability to download the jar files that has the code to do the copying. For example, we know that the FileUtils class of the commons-io utility package provides a lot of File Utility functions. So, all we need to do is go to our pom.xml and add the following lines.
 
   org.apache.commons
   commons-io
   1.3.2
  
Add these between <dependencies> tags.

So, the contents of sample/pom.xml should be something like:


 4.0.0
 abc
 sample
 0.0.1-SNAPSHOT
 sample
 Sample project

 
  
   org.apache.commons
   commons-io
   1.3.2
  
 



Click on Project -> Build in Eclipse. On the console, you will see that a lot of jar files are downloaded from some repositories. You can find what all jars are downloaded by going to the location .m2\repository somewhere in your C drive(this default location is specified in settings.xml in your initially downloaded Maven zip file)

That's it! Now, you have the commons-io jar at your disposal !

Go to SampleClass.java and type FileUtils and press Ctrl+Space. This will automatically import the class org.apache.commons.io.FileUtils. Now you can use the methods in the FileUtils class


Add the code so that the java class looks like below:
package code;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

public class SampleClass {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
File srcDir = new File("C:\\srcDir");
File destDir = new File("C:\\destDir");

FileUtils.copyDirectory(srcDir, destDir);
} catch (IOException e) {
e.printStackTrace();
}

}

}


Save, build and run the Java class using Run -> Run as Java application (You can change the srcDir, destDir paths in the java class to match your folders)

You have used Apache's FileUtils class just by adding the dependency in your pom.xml file.

If I get enough feedback, I will write more tutorials on how to use Maven to install/package your projects and how to use repositories and continuous integrations.

Hopefully, the above simple example will initiate some interest in using Maven for your projects.


References and useful links:

http://maven.apache.org/guides/getting-started/index.html

1 comment :

  1. Hi Kiran its good article to understand the basics of maven, can you give more examples about maven usage for web applications

    ReplyDelete