Thursday, November 6, 2014

Maven Basics

Powerful Feature of Maven is that it's ability to download the dependencies automatically.

Why Maven?

Common Problems we face in S/w Development is that

Each Project Type has it's own structure and there is a need to take care of it.

Requirement of Multiple Jars and other Dependencies. For each framework like Spring, hibernate etc we need to be aware of the Jars Required for the Corresponding Versions.

Build Process and Deployment

Apache group mainly developed Maven to build multiple projects together, publish projects information, deploy projects, share JARs across several projects and help in collaboration of teams.

How to SetUp Maven?

Dowlaod the latest version from http://maven.apache.org/ and unzip it.

Set the Environment Variables MAVEN_HOME / PATH / JAVA_HOME as below.

That's it. Once done open the cmd and type to check if everything has been set well.

How to setup a Project Structure via Cmd Prompt?

Run the cmd mvn archetype:generate from Cmd Prompt.

We get a list of predeined archetypes.

Archetype refer to the prototype how we want to structure our project. It requests for versions in some cases and other properties like groupId [similar to package name], artifactId [similar to project name],

Now if we look into the directory, we find the well defined Project structure with pom.xml carrying all the required details about the Project Structure, Dependencies, ArtiactId etc.,

artifactId

|_src

|_main

|_java

|_resources

|_test

|_pom.xml

Let's see how to run the Project. Check if have the sample hello worldwithin the java directory above.

Run the Command mvn comile. This downloads all the required jars and compile the class files to the artifactId->target directory

Now run the command mvn package. This builds not only jar but also run all test cases, so we don't have worry about running the Unit Tests manually.

Now roll the Java Main method with java -cp foo.jar full.package.name.ClassName

mvn archetype:generate -> This command contacted Maven Repository and fetched the list of Archetypes. Once we provide the required Archetype as Input, the project structure was created along with POM file as below.

Whenever we run a specific phase, other phases below run implicitly. For example if run package phase [resource gets prepared and compiled and finally packaged]

Note: Install command is Maven specific. It does not installs the war/ jar into Tomcat. It just instals into Maven Repositary [most often it is in our System]. Incase of any dependencies, Maven first looks into the Local Repository, it downloads if and only if it couldn't fetch from the Local.