Friday, November 18, 2011

Maven basics

Birth of maven

Before ANT came into place,developers used to compile java classes through command line  and generate the corresponding jar/war.It was error prone and lengthy process.


Then ANT came into picture.Developers used to write build script and use ANT to generate the required artifacts.However developer still need to write or customize the build script.Every project had a different structure ,roughly same build process with files spread over directories spread over source code.Different targets were created for cleaning the build directories,compiling the source code,running the test script,creating the deployment artifacts like war,ear,jar ,....
Developer used to worry about the dependent libraries and other stuff.



Then MAVEN was born.It standardized the project structure for most types of projects.Automated common activities like clean,compile,package and others.Relieved developers of fixing the dependencies of direct or indirect libraries.It removed inconsistent setup across developers and made process simple and transparent.

What all maven do?


Maven is a build and project management tool.

It is a build tool because of following reasons.These are:-
i) It has normalized and standardized the project structure

ii) Dependencies of third party libraries are handled gracefully.Developer need not have to download the jars and push it in the class path for building.Developer defines that this project is dependent on these tools/libraries in the pom.xml and maven does the magic of downloading  the associated jars,building the class path and managing the versions for different dependencies.

iii)We can generate the required artifacts from the project like war,jar,...Since maven has standardized the project structure.It knows which files needs to be compiled,how properties and other configuration files needs to packaged and generate the project artifact.Developer has to just fire the required goal.

iv)Lets suppose you are working in distributed environment where changes made in one module affects other module.So using mvn package will deploy the application in local /shared repository  from where others can define it as a dependency and latest changed jar file will be available to others.

v) Using various plugins we can do lots of other activities eg.plugin for jetty helps you run the war file and others.

It is a project management tool because of following reasons.These are:-
i) We can integrate integration tool like cruise control and other such tools with maven.So when any code is committed,cruise control process will run.

ii) We can integrate Bugzilla with maven.

iii)We can generate java docs,unit test reports,code coverage,code analysis using mvn site.The project site generation goal creates a professional web site of your project, including links to syntax highlighted source code, code metrics, javadoc, unit test results, and more.

Foundation blocks of maven

Maven consists of two repositories.These are :-


i) Archetype repository
Archetype repository contains information about different project types.When we invoke mvn archetype:generate.It list down the projects that we want to create.
Depending upon the selection maven creates the standardized directory structure containing folders for configuration files,source code,test cases and other required setup.

ii) Dependency repository
Most of the project depends upon third party libraries and these libraries depends upon other libraries.Different versions have different version dependencies.Maven helps in dependency management.By defining dependency of the project on third party library and corresponding version ,maven will automatically download the mentioned dependency and all other libraries which this version of dependency require.

These two are foundation blocks of the maven.

Archetype,ArtifactId,GroupId and Version

Lets take the example of creating a web based  application in java.
A web based application  needs WEB-INF,web.xml ,source folder ,test cases folder.

So what needs to be done and how maven assists us in making such application.Lets go through step by step .
1. After installing maven ,go to the directory where you want project to be setup.
Lets say we created a folder named 'DummyWebApp' under C: drive.

2. Go to C:\DummyWebApp.

3.Type mvn  archetype:generate

4.It will show you the list of project types present in maven archetype repository.So archetype is nothing but a standardized template of a project with some defaults.

5.For web based application we will enter 1 when prompt for 'Chose a number or apply filter' comes.

6.Then it  will prompt for 'Choose version:' i.e which version of archetype template you want to choose.It would have shown you the different versions available for the archetype you have selected.Go ahead with the default one.

7.Then it will prompt for ' Define a value for property <groupId>'.groupId refers to the structure in which you want to package your project in local repository or remote repository.
Eg. 
Lets suppose your code jar file is required by other dependent projects.So if you construct your jar file as ABC.jar and define groupId as com.kunaal.deploy.Then if you invoke mvn install.It will put ABC.jar inside com\kunaal\deploy folder of your local repository.

8.Then it will prompt for 'Define a value for property <artifactId>'.ArtifactId refers to name of your packaged file i.e name of the jar or war file which your project will create.
Eg.
If we give artifactId as 'Kunaal' so when we do mvn package and project is web based application and packaging strategy is 'war'.It will create Kunaal.war.

9.Then it will prompt for 'Define a value for property <version>'.Define a version

10.Then it will prompt for 'Define a value for property <package>'.i.e how you want the package structure.Normally groupId and package property is kept same.
Now you are done.It will create a directory structure like this 
DummyWebApp
                      -------> pom.xml
                      -------->src
                                           --->main
                                                         -----> java
                                                         -----> test
                                                        ------> webapp
                                                                              ------> WEB-INF ---->web.xml
                                                                              ------> index.jsp
                                                         ------> resources

So you can see by simply following the instructions.We can create a standardized project with defaults .



What is dependency and how to inform maven about it?

Every project need some third party libraries.These libraries acts as a dependency in maven.Some dependencies are required for running test cases, some are required for compiling,some are standardized service level interfaces whose implementation is provided by the server like servlet api and others.So in order to define these dependencies in pom.xml .We add a dependency tag .

Lets take an example.
Our application is dependent upon log4j and Junit.We need log4j for logging.So instead of downloading the same and publishing it in classpath.We will add dependency for the same in pom.xml.

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.16</version>
</dependency>
 
So in our repository there would be folder named <groupId>\<artifactId>\<version>
Inside this there would be pom file for the same and corresponding library file.In this case there would be a folder like ...\.m2\log4j\log4j\1.2.16\log4j.jar

Similarly for JUnit,dependency would be like this.
<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.7</version>
      <scope>test</scope>
</dependency>

There is one difference here.Scope of junit dependency is 'test'.By default scope of dependency is 'compile'.Following scopes are available for dependencies.These are:-

i)Compile-This is the default scope.It means that these libraries are available at the compile time.These libraries are available in classpath of the project.

ii)Test-This means that these libraries are available during compilation and execution of test cases.

iii)Provided-This means that these libraries are provided by the container or the jdk.eg servlet

iv)Runtime-This means that these libraries are available at the runtime.

Common goals of maven
Following are the common goals of the maven.These are :-
i) validate - This checks whether project structure is proper or not and all necessary information is available or not.

ii) compile - This compiles the source code

iii)test - This runs the test cases

iv)package - This takes the compiled code and distribute it in the suitable format like jar,war

v)install -This installs the package in the local repository so that other local projects can use it.

vi) deploy- This copies the package in the remote repository.Normally done during release phase of the project.

When we invoke any goal through maven,all previous goals in the hierarchy will also run.
eg. We invoke mvn install
It will run validate,compile,test,package and install.


Settings.xml

Maven execution settings are defined in settings.xml.It contains information about the local repository,whether maven should interact with user for their input or not,whether it should work in offline mode or not.

It also states the information regarding repositories,servers and other profile related information.This file is available at following two places.These are:-
  • The Maven install: $M2_HOME/conf/settings.xml
  • A user's install: ${user.home}/.m2/settings.xml
 First one is called Global setting.Second one is called user settings.If both are present then these gets merged with user settings taking the major role.




No comments:

Post a Comment