Maven Basic


 Maven can  do  many things, but in a nutshell Maven is an attempt to apply patterns to a project's build infrastructure in order to promote comprehension and productivity by providing a clear path in the use of best practices.It is specially used in many project to minimize the coupling and increase the cohesiveness of the module.it is the enhancement of build tool like ant wherein we have to specify the jar file name as well as the location of jar file  which resides in our local system.Due to increase  Maven is essentially a project management and comprehension tool and as such provides a way to help with managing:

It is a build tool which is used for building all the process which are carried out during the deployment of a software project.
Following are the list  of process which are happening during the building process.

  • Setting jar file on class-path for compilation
  • Creating deployment directory structure for web application or enterprise application
  • Pasting required jar file in lib folder.
  • creating war or jar or pom file
  •  Starting a server.
 Apart from this some more operation we can achieve are
  • Documentation
  • Reporting   (creating report)
  • Dependencies  (providing dependency between  multiple module )
  • SCMs(connecting version control system like svn)
  • Releases

Procedure to install and configure maven in windows

Prerequisite s/w
  1. JDK 1.6
  2. Maven 2.2.1
  3. Windows 7/xp

setting the classpath environment variable

Go th      computer->right click->properties->advanced system settings->Environment variable->System properties.

Here u have to create one maven  variable.

variable name- M2_HOME
value-location of maven home directory like D:\software\java software\apache-maven-2.2.1.

Linking the maven  home directory to jdk
create path environment variable and give the maven   home directory bin location to path variable as a value.
if u have existing path variable then just add the the maven directory bin folder   location to path variable separated by semicolon between multiple  path location value.

like
variable name-PATH
        value-     D:\software\java software\apache-maven-2.2.1\bin


checking for maven installation and configuration.

open command prompt. type  mvn -version and a screen will come for successful configuration and integration with jdk with version like......

                                           Repository

Repository in maven is a store place for all jar file which are required for deployment of a application module.
maven has a by default repository called m2 folder under which  a folder named repository is created which contains all jar file.
 There are there type of repository in maven

  1. Local Repository
  2. Center Repository
  3. Remote Repository


The searching procedure for finding a particular jar file is from local to central to remote.In other-words the jar file will be first searched in local repository  and if not found then central  and if not found then it ll search that file in remote repository and will download the jar file. 

Local Repository
  The default repository of maven is m2_home directory i:e m2 folder.
If want to change the local reposiroy location the we have to change the location in settings.xml file which resides in conf folder of maven home directory.
<settings>
  <!-- local Repository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ~/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
 
<localRepository>D:/maven_repo</localRepository>
 
 

Central repository

It is the maven own site where exactly all the jar file has been stored.
it is one url which has been provided by Apache software foundation to
 download all the required jar file and placing those into local repository
 if those are not present in local.

The central repository url is "http://repo1.maven.org/maven2/"
we can access this by "http://search.maven.org/"



 This repository is by default repository for every pom file.




Remote Repository

 

 Every maven jar files libraries are store in Maven central repository, some times, we need to add some remote repositories to download the libraries from another location instead of the default central repository.

 

  1. Search in Maven local repository, if not found, go step 2, else exit.
  2. Search in Maven central repository, if not found, go step 3, else exit.
  3. Search in java.net Maven remote repository, if not found, prompt error message, else exit.
To access remote repository we have add one tag in pom.xml  like......

    <repositories>
 <repository>
     <id>java.net</id>
     <url>https://maven.java.net/content/repositories/public/</url>
 </repository>
    </repositories>
  
we can have multiple remote repository in a single pom.xml and are placed under  repositories tag like...

  <repositories>
 <repository>
     <id>java.net</id>
     <url>https://maven.java.net/content/repositories/public/</url>
 </repository> 
        <repository>
 <id>JBoss repository</id>
 <url>http://repository.jboss.org/nexus/content/groups/public/</url>
      </repository> 
 </repositories>
 
and a particular jar file will be searched from top to down manner.in other words 
whenever a particular jar file will be searched,it will be first searched in first
repository under the repositories tag, if  not found then only it will go to 
next and so on..
 

Description of maven pom file

To use maven as a build tool in our project we have a xml file named "pom.xml" stands for project object model in which we have to place the required jar file information as a dependency.
The necessary tag for a pom are listed below...
<artifactid>
<groupId>
<package>
<url>
<dependencies>
<dependency>
<plugins>
 and so on...
Every pom file will generate a jar file or war file and a parent pom file which may act as a dependency to another pom file.In otherwords the dependency between multiple module we can achieve my using maven parent tag i:e <parent> which contain artifactid ,groupid and version of the parent jar file or pom file.Using this parent tag we can have a modular dependency(Inheritance) between our different module to achieve performance in our project.A simple parent tag is like..


<parent>
    <groupId/>
    <artifactId/>
    <version/>
    <relativePath/>
  </parent>
Here we can give the give the parent url also... 
 
The common maven tag for all dependency is dependencies tag where we can declare 
multiple jar as a dependency.it is the root tag for all dependency tag.it is look
 like..
<dependencies>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1</version> 
    </dependency>
<dependency>
            <groupId>javax.xml</groupId>
            <artifactId>jaxrpc-api</artifactId>
            <version>1.1</version>
        </dependency>
       <dependency>
            <groupId>asm</groupId>
            <artifactId>asm</artifactId>
            <version>3.3</version>
        </dependency> 

  </dependencies>
For better performance we can declare all the dependancy under dependancy management tag. ex..
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
            <!-- type and scope omitted since they use the default values -->
        </dependency>
    </dependencies>
</dependencyManagement>
 
artifactId- It describes the jar file name 
groupId-It describes the jar file package
version-It describes the version to which the jar file belong.
 
Apart from this the  dependency tag contain another optional tag called scope ,
 in which we can use a particular jar either at compile time or runtime. i:e
<scope>
</scope> 

It contain there values which are

  1. compile

  2. runtime

  3. provided

    The default is provided.
If its value is compile then it will used during compilation of .java file by
compiler only and can to be visible at run time and if its value is runtime then
it will be used during execution by jvm and if its  value is provided then it
will used during compilation and will be at runtime also.
it means at run time automatically it will be provided.
 
 

  Build Tag  


It is the child tag of root tag called pom and it contains information which are required to build a web application .
It contain a tag called final name whose value represent the web application war file name.
Apart from that it contain one sub element called pluginmanagement tag under which various inbuilt plugin can be declared.
 Ex..
<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>MyPlugins</groupId>
                <artifactId>hiPlugin</artifactId>
                <version>1.0</version>
                <configuration>
                    <greetings>welcome</greetings>
                </configuration>
            </plugin>
        </plugins>
        </pluginManagement>
    </build>
  
It is optional tag of build tag and is used to to good performance in our application.In other words without pluginManagement tag  we can declare plugins tag also.

                     Plugin tag 

It is the sub tag of plugins tag  and is used to declare different type inbuilt plugins is  to be placed to the pom file to achieve some inbuilt functionality from different tools like JaxB,wsdl2java and so on.
In other-words we  use these plugin to automate some extra functionality.

ex..
<plugin>
      <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
 


Here we are using to get the java compiler plugin which tells to maven that we
are using jdk 1.6 version compiler.
 
Every plugin are declared under one sub root tag called plugins(<plugins>)
A plugins  tag can have multiple plugin tag as a sub elements.

<plugins>    
            <!-- It's advised not to chickin the generated classes. By default they are generated in target/generated-sources with xmlns namespace provided
            in xsd. If you need the sources to be generated uncomment the below.
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <packagename>xxx.schema</packagename>
                    <schemaDirectory>${basedir}/src/main/resources</schemaDirectory>
                    <outputdirectory>${basedir}/src/main/java/xxx/generated</outputdirectory>
                </configuration>
            </plugin>
            -->
         
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <configuration>
                    <extension>true</extension>
                    <args>
                        <arg>-Xfluent-api</arg>
                    </args>
                    <plugins>
                        <plugin>
                            <groupId>net.java.dev.jaxb2-commons</groupId>
                            <artifactId>jaxb-fluent-api</artifactId>
                            <version>2.1.8</version>
                        </plugin>
                    </plugins>
                    <bindingDirectory>src/main/resources/binding</bindingDirectory>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
 
 

 

 

2 comments: