Sunday, April 22, 2012

Creating Maven Web Project in Eclipse

Creating Maven Web Project in Eclipse

Introduction to Maven
Maven is a popular open source build tool for enterprise Java projects, designed to take much of the hard work out of the build process. Maven uses a declarative approach, where the project structure and contents are described, rather then the task-based approach used in Ant or in traditional make files, for example. This helps enforce company-wide development standards and reduces the time needed to write and maintain build scripts.
Here is a simple tutorial which you can go through to create Dynamic Web Project having Maven enabled in Eclipse. This project can be used as base project and can be easily converted to most kind of project like Struts based, Spring MVC based etc.

Required Tools

For this tutorial, I assume you have following setup in your machine.
1. JDK 1.5 or above (download)
2. Eclipse 3.2 or above (download)
3. Maven 2.0 or above (download)
4. M2Eclipse Plugin (download)

I assume that you have already installed and configure java and Eclipse.
1.install Maven
Maven is a Java tool, so you must have Java installed in order to proceed. More precisely, you need a Java Development Kit (JDK), the Java Runtime Environment (JRE) is not sufficient.
Additional optional installation steps are listed after the platform specific instructions.

Windows 2000/XP

  1. Unzip the distribution archive, i.e. apache-maven-3.0.4-bin.zip to the directory you wish to install Maven 3.0.4. These instructions assume you chose C:\Program Files\Apache Software Foundation. The subdirectory apache-maven-3.0.4 will be created from the archive.
  2. Add the M2_HOME environment variable by opening up the system properties (WinKey + Pause), selecting the "Advanced" tab, and the "Environment Variables" button, then adding the M2_HOME variable in the user variables with the value C:\Program Files\Apache Software Foundation\apache-maven-3.0.4. Be sure to omit any quotation marks around the path even if it contains spaces. Note: For Maven < 2.0.9, also be sure that the M2_HOME doesn't have a '\' as last character.
  3. In the same dialog, add the M2 environment variable in the user variables with the value %M2_HOME%\bin.
  4. Optional: In the same dialog, add the MAVEN_OPTS environment variable in the user variables to specify JVM properties, e.g. the value -Xms256m -Xmx512m. This environment variable can be used to supply extra options to Maven.
  5. In the same dialog, update/create the Path environment variable in the user variables and prepend the value %M2% to add Maven available in the command line.
  6. In the same dialog, make sure that JAVA_HOME exists in your user variables or in the system variables and it is set to the location of your JDK, e.g. C:\Program Files\Java\jdk1.5.0_02 and that %JAVA_HOME%\bin is in your Path environment variable.
  7. Open a new command prompt (Winkey + R then type cmd) and run mvn --version to verify that it is correctly installed.

Unix-based Operating Systems (Linux, Solaris and Mac OS X)

  1. Extract the distribution archive, i.e. apache-maven-3.0.4-bin.tar.gz to the directory you wish to install Maven 3.0.4. These instructions assume you chose /usr/local/apache-maven. The subdirectory apache-maven-3.0.4 will be created from the archive.
  2. In a command terminal, add the M2_HOME environment variable, e.g. export M2_HOME=/usr/local/apache-maven/apache-maven-3.0.4.
  3. Add the M2 environment variable, e.g. export M2=$M2_HOME/bin.
  4. Optional: Add the MAVEN_OPTS environment variable to specify JVM properties, e.g. export MAVEN_OPTS="-Xms256m -Xmx512m". This environment variable can be used to supply extra options to Maven.
  5. Add M2 environment variable to your path, e.g. export PATH=$M2:$PATH.
  6. Make sure that JAVA_HOME is set to the location of your JDK, e.g. export JAVA_HOME=/usr/java/jdk1.5.0_02 and that $JAVA_HOME/bin is in your PATH environment variable.
  7. Run mvn --version to verify that it is correctly installed.

2.install Maven in Eclipse
1. Open Eclipse IDE and go to Help-> Eclipse Marketplace..

2. Find : Maven integration for eclipse -> Install Maven integration for eclipse WTP

4. Select Plugin

5. Accept License Agreement  and Finish .

Security Warning for install

After completing restart Eclipse


3. Create Maven Web Application
To Create Maven Web Application you have to follow steps as below.
1.       File -> New -> Other
2.       Maven -> Maven Project

Click on Next Button.
3.       Select Directory where you want to create project, in this application I use default Workspace Location.



4.       Now it is the main important portion of creating application that is choose archetype of your application. You have to choose archetype according to your application architecture. For i.e. if you want to create spring web application than you have to select  ‘spring-mvc-webapp’ . here we are creating maven web application so that we are using ‘maven-archetype-webapp’ .
for ‘maven-archetype-webapp’ you have to type ‘maven-archetype-webapp’ in filter box.

Select ‘maven-archetype-webapp’ from the list and click on next.

5.       Now give Group Id, Artifact Id, version, package. For that you have to understand the meaning of each.

->  groupId This element indicates the unique identifier of the organization or group that created the project. The groupId is one of the key identifiers of a project and is typically based on the fully qualified domain name of your organization. For example org.apache.maven.plugins is the designated groupId for all Maven plug-ins.

->  artifactId This element indicates the unique base name of the primary artifact being generated by this project. The primary artifact for a project is typically a JAR file. Secondary artifacts like source bundles also use the artifactId as part of their final name. A typical artifact produced by Maven would have the form <artifactId>-<version>.<extension> (for example, myapp-1.0.jar).

->  version This element indicates the version of the artifact generated by the project. Maven goes a long way to help you with version management and you will often see the SNAPSHOT designator in a version, which indicates that a project is in a state of development. We will discuss the use of snapshots and how they work further on in this guide.

->  package This should be the package of project.


Here we use com.mavenexample.main as GroupId,  MavenWebApplication as Artifact Id, 0.0.1-SNAPSHOT as a version and com.mavenexample.main as package.
And click Finish create project.

6.      Now change java version.
Right click on project -> properties -> select Project Facets.

In this hear change java version to 1.6.



And click on Ok button to change version.

7.      Your project structure look like


4. Run on Tomcat server.

 To run project on tomcat server Right Click on project -> run -> Run on Server.

Select tomcat server from list of not exist than create new tomcat server.

Now open ‘http://localhost:8080/MavenWebApplication/’ url in browser.
  
Now you can start with actual work with maven integration.

2 comments:

  1. nice work...

    its really easy to understand..

    carry on dude..

    waiting for more informative blogs.

    ReplyDelete
  2. good blog

    hard work done on this

    keep it up...

    ReplyDelete