Saturday, 16 April 2016

Maven Guide


Contents



 Prerequisites

Following are the certified and recommended versions for Maven Projects:
Apache Maven
3.3.9 or higher
Eclipse
Luna
JDK
1.7
Tomcat
apache-tomcat-7.0.62
Windows
7


1         Introduction to Maven

Maven primary goal is to provide developer
·         A comprehensive model for projects which is reusable, maintainable, and easier to comprehend.
·         Plugins or tools that interact with this declarative model.
Maven project structure and contents are declared in an xml file, pom.xml referred as Project Object Model (POM), which is the fundamental unit of the entire Maven system.

  2         Installing Maven

To install Apache Maven on Windows, you just need to download the Maven’s zip file, and unzip it to the directory you wish to install, and configure the Windows environment variables.

     2.1       JDK and JAVA_HOME

Make sure JDK is installed, and “JAVA_HOME” variable is added as Windows environment variable.



2.2       ADD M2, M2_HOME and PATH

Assume you unzip the maven folder to location: D:\apache-maven-3.3.9-bin.tar\apache-maven-3.3.9-bin\apache-maven-3.3.9.
Add both M2_HOME and MAVEN_HOME variables in the Windows environment, and point it to your Maven folder. Update PATH variable.
M2=%M2_HOME%\bin
M2_HOME=Path to apache maven folder
In Path variable append at last %M2%;




2.3       Update Proxy Setting


Go to $M2_HOME/conf/Setting.xml
You find:
<!-- proxies
   | This is a list of proxies which can be used on this machine to connect to the network.
   | Unless otherwise specified (by system property or command-line switch), the first proxy
   | specification in this list marked as active will be used.
   |-->
<proxies>
<!-- proxy
     | Specification for one proxy, to be used in connecting to the network.
     |
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username>proxyuser</</username>
<password>proxypass</password>
<host>proxy.host.net</host>
<port>80</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
    -->
</proxies>

Un-comment the proxy options and fill in your proxy server detail.

2.4       Verification

To verify that maven is installed correctly to your machine. Go to command prompt, enter the command mvn Or mvn –version. If you see the similar message, means the Apache Maven is installed successfully on your machine.

3         Importing existing Maven project in eclipse

3.1       Importing Project


File > Import > Maven > Existing maven project >Next>Root directory >Finish.

3.2       Setting proxy for eclipse

Go to Windows > Preferences >Proxy > Active provider: Manual > Check HTTP >EDIT
Provide your proxy settings. Apply and then OK.

 

4         Running the Maven project

4.1       Running Maven project from command prompt:

Open command prompt. Go to your project directory.
To clean maven repository, use command: mvn clean
To install maven repository, use command:mvn install
To update maven repository, use command: mvn clean install
To execute Java file, use command:
mvnexec:java -Dexec.mainClass="com.persistent.ITGClient.HttpClient" 
After install command all the jars mentioned in pom.xml of project get downloaded in your local maven “./m2” repository that is located in user folder.






4.2       Running Maven project from eclipse

Right click on your project > Run as
You get all the option related to your project like:
  •  Maven clean
  •   Maven install
  •    Maven build
  •    Run on server etc.

4.3       Issue while running maven command from eclipse

If you face issue like apache 2.6 in pom.xml or so while running maven command from eclipse but all these command are running properly from command prompt.
Go to Window > preferences > Maven > User Setting > Point Setting.xml of your Apache Maven.

5         Creating a new maven project in eclipse

5.1       Maven based web application


·         File > New > Maven > Maven Project
·         Do not select any check box. Provide the default workspace location as depicted in fig 6.1.1
·         Click Next
·         Choose the artifact ORG.APACHE.MAVEN.ARCHETYPES : MAVEN-ARCHETYPE-WEBAPP : RELEASE
As depicted in fig 6.1.2
·         Click Next
·         Provide GroupId, Artifact id version and package detail as shown in fig 6.1.3
GroupId: groupId will identify your project uniquely across all projects, so we need to enforce a naming schema. It has to follow the package name rules, what means that has to be at least as a domain name you control, and you can create as many subgroups as you want.
Artifact ID:artifactId is the name of the jar without version. If you created it then you can choose whatever name you want with lowercase letters and no strange symbols. If it's a third party jar you have to take the name of the jar as it's distributed.
Version:version if you distribute it then you can choose any typical version with numbers and dots (1.0, 1.1, 1.0.1, ...). Don't use dates as they are usually associated with SNAPSHOT (nightly) builds. If it's a third party artifact, you have to use their version number whatever it is, and as strange as it can look.
eg. 2.0, 2.0.1, 1.3.1
·         Click Finish






5.2       Convert existing web based application into Maven based web application

Maven provides sensible default behavior for projects. When a Maven project is created, Maven creates default project structure. Developer is only required to place files accordingly and he/she need not to define any configuration in pom.xml.
Source code: projectName/src/main/java
Resource file: projectName/src/main/resources
Test files: projectName/src/main/test
WEBContect: projectName/src/main/webapp

5.3       POM.xml

5.3.1. Adding jars dependencies supported by maven repository.

<dependencies>
        <dependency>
                     <groupId>org.json</groupId>
                     <artifactId>json</artifactId>
                     <version>20140107</version>
                     <scope>compile</scope>
       </dependency>

       <dependency>

               <groupId>org.apache.commons</groupId>

              <artifactId>commons-math3</artifactId>

              <version>3.6</version>

              <scope>compile</scope>
     </dependency>
</dependencies>
These jars get automatically downloaded and added to your project.

5.3.2. Adding jars dependencies which are not supported by maven public repository

       For e.g. Oracle or Microsoft specific jars. You have to provide them externally and mentioned that       dependencies in pom.ml file.

      These are the jar you have to ship them with the application.
    
     There are two possible cases where you need to ship the jar with application:
1.       Jar is not supported by maven public repository.
2.       You created a custom jar and want to use them for your project.

<dependencies>

         <dependency>
              <groupId>com.microsoft.sqlserver</groupId>
              <artifactId>sqljdbc4</artifactId>
              <version>4.0</version>
              <scope>provided</scope>
        </dependency>
         
         <dependency>
               <groupId>com.oracle.oim</groupId>
               <artifactId>wlfullclient</artifactId>
               <version>11g-R2-PS3</version>
               <scope>provided</scope>
        </dependency>

</dependencies>

To add provided dependency in pom.xml execute cmd:

mvninstall:install-file -Dfile=sqljdbc4.jar -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc4 -Dversion=4.0 -Dpackaging=jar

In –Dfile=Mention the complete path from where to load jar as these are provided jar you are supposed to provide these jar to maven repository.

Either run this command from command prompt to add jar or add these command to batch property file.

If these commands are added to batch file.

While running your project add provided jar to tomcat/web app /lib folder.


                                               ************************************

1 comment:

  1. If you face issue : mvn is not recognized as an internal or external command

    Resolution: CHECK YOUR PATH, M2 and M2_HOME enviorment variable..Restart your machine

    ReplyDelete

Wait, notify and notifyAll method in Object Class

We usually get  surprise  when these methods are present in  Object  class however they only used in  Threading  Concepts.   Wonder WHY?...