Create, configure maven project in eclipse - example tutorial

In this post, the followings are covered :
  • Download and Install maven in windows:
  • Add M2_REPO classpath variable in eclipse 
  • Generate Java/Web Project  with maven and import in eclipse
  • Add another dependency from web repository
  • Add custom or 3rd party library manually into repository:
  • Benefits of using MAVEN :

Download and Install maven in windows:
Download maven (apache-maven-X.X.X-bin.zip)  from http://maven.apache.org/download.html Extract the zip file to a folder say G:\maven3
Configure environment variables(The installation directories of JDK and Maven might differ in your case.)
  • JAVA_HOME  >> C:\Program Files (x86)\Java\jdk1.7.0\
  • MAVEN_HOME >> G:\maven3\
Add paths
  • %MAVEN_HOME%\bin ; %JAVA_HOME%\bin;
adding environment variables 
To Verify :
To verify it, in command prompt, type “mvn –version” to verify the installation detail.

Add M2_REPO classpath variable in eclipse :
adding m2_repo classpath variable in eclipse
  • Goto Window ->> Preferences ->> Java ->> Build Path ->> Classpath Variable in eclipse IDE.
  • Create new class path variable by : New ->>  Name M2_REPO, Path : C:\Users\GT\.m2\repository
  •  You may change the default folder( C:\Users\GT\.m2\repository ) to other folder. like, F:\REPO\ or F:\.m2\repository ... but make sure the path matches with <localRepository>.....</localRepository> in {M2_HOME}\conf\setting.xml file

Generate Java Project with maven:
Syntax for generating a java project with maven is :
mvn archetype:generate -DgroupId={packaging.path} -DartifactId={project-id}   -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
eg.
Create a project MavenTest with com.gt.mavenTest default package
F:\testt>mvn archetype:generate -DgroupId=com.gt.mavenTest-DartifactId=MavenTest -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

To convert  into eclipse project:
run following command.
F:\testt\MavenTest\mvn eclipse:eclipse 

For JavaEE web project:
use
-DarchetypeArtifactId=maven-archetype-webapp
instead of
-DarchetypeArtifactId=maven-archetype-quickstart
eg.
F:\testt>mvn archetype:generate -DgroupId=com.gt.mavenWebTest-DartifactId=MavenWebTest 
-DarchetypeArtifactId=maven-archetype-webapp  -DinteractiveMode=false

Converting  to eclipse java EE project
(extra parameter-->wtpversion = Web tools platform wtp version) 
Run following command.
F:\testt\MavenWebTest\mvn eclipse:eclipse -Dwtpversion=2.0

The converted Projects can be imported into eclipse.
MavenTest project (folder structure) after importing into eclipse

Add another dependency from web repository:
Lets suppose we need another library (jar) in our project, say "commons-logging-1.1.1.jar" from apache.
Adding dependency in pom.xml file
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>


Then run the following command
    F:\workspace\MavenTest\mvn clean install eclipse:clean eclipse:eclipse

the dependency is downloaded and added into eclipse project







Add custom or 3rd party library manually into repository:
Say "MyLibrary.jar" is your jar file, which is in folder F:\libs\MyLibrary.jar
Install custom library :
mvn install:install-file -Dfile=f:\libs\MyLibrary.jar -DgroupId=com.gt.mylib 
-DartifactId=MyLibrary -Dversion=1.0 -Dpackaging=jar 
To use in the eclipse project, Add this library in your pom.xml file
<dependency>
      <groupId>com.gt.mylib</groupId>
      <artifactId>MyLibrary</artifactId>
      <version>1.0</version>
</dependency>
And don't forget to run this command again
    F:\workspace\MavenTest\mvn clean install eclipse:clean eclipse:eclipse

Benefits of using MAVEN :
  • Central/ single repository of libraries, to be shared by all eclipse projects. The library can be shared among other developers
  • Useful in building, packaging, install, deploy etc...
  • ..........

No comments :

Post a Comment

Your Comment and Question will help to make this blog better...