Wednesday, September 14, 2011

Maven for Web based Projects

Maven is a software tool for project management and builds automation.

Maven serves a similar purpose to the Apache Ant tool, but it is based on different concepts and works in a profoundly different manner. Maven is hosted by the Apache Software Foundation, where it was formerly part of the Jakarta Project.

Maven uses a construct known as a Project Object Model (POM) to describe the software project being built, its dependencies on other external modules and components, and the build order. It comes with pre-defined targets for performing certain well-defined tasks such as compilation of code and its packaging.

Maven dynamically downloads Java libraries and Maven plug-ins from one or more repositories. Maven provides built-in support for retrieving files from the Maven 2 Central Repository and other Maven repositories, and can upload artefacts to specific repositories after a successful build. A local cache of downloaded artefacts acts as the primary means of synchronizing the output of projects on a local system.

To Use Maven for a web based project application should follow Maven Recommended directory structure and it should have a pom file which contains project properties and dependency details

To convert your simple Web based project to a Maven project using Eclipse follow following steps

  1. Configure Maven installation at your machine
  2. In Document and Settings Users folder create a folder with name .m2 and in this folder create a file with name settings.xml
    1. C:\Documents and Settings\38569Ad\.m2\settings.xml
  3. This file contains configuration details and server URL related to Maven repository
  4. Create New Dynamic Web Project
  5. Give you project a name, for example I named it SpringMvnPro
  6. Make a new folder with name src
  7. Inside src create two separate folder with name main and test
  8. Main folder is used to store project source and resources
  9. Test folder is used to store test cases and test resources
  10. Inside main create new folders with name java and resources
  11. If you test cases for your project then inside test create new folders with name java and resources
  12. Java folder is used to store src files
  13. resources folder is used to store web.xml file and if you are using spring then its used to store spring related xml files
  14. Next very important step is to create pom.xml file. In POM file we mention important dependency and plug-in details.
  15. Store pom.xml at root of your project folder
  16. For example in our project we are using Spring so we need to add spring dependencies
                        <dependency>
                                     <groupId>org.springframework</groupId>
                                     <artifactId>spring-core</artifactId>
                                     <version>${springframework}</version>
                        </dependency>

Add Spring MVC dependencies
                        <dependency>
                                     <groupId>org.springframework</groupId>
                                     <artifactId>spring-web</artifactId>
                                     <version>${springframework}</version>
                        </dependency>
                        <dependency>
                                     <groupId>org.springframework</groupId>
                                     <artifactId>spring-webmvc</artifactId>
                                     <version>${springframework}</version>
                        </dependency>

  1. Right Click on Project Properties and Java Build path. Source folder should be SpringMvnPro/src/main/java and ourput folder should be SpringMvnPro/target/classes
  2. Righlt Click on Project à Select Maven à and enable dependency Management
  3. To make war file right click on pom.xml, select run as, Click on Run Configuration. Click on Maven Build. Create new Configuration. Select Project and In Goals add clean package. Run Configuration
  4. This will generate war file with name SpringMvnPro.war
Maven Repository

Application Structure

Content of pom.xml file

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
  <groupId>SpringMvnPro</groupId>
  <artifactId>SpringMvnPro</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>SpringMvnPro</name>
  <url>http://maven.apache.org</url>
  <properties>
            <springframework>3.0.5.RELEASE</springframework>
  </properties>
  <dependencies>
                  <dependency>
              <groupId>javax.inject</groupId>
              <artifactId>javax.inject</artifactId>
              <version>1</version>
            </dependency>                
            <dependency>
                   <groupId>org.springframework</groupId>
                   <artifactId>spring-core</artifactId>
                   <version>${springframework}</version>
            </dependency>
            <dependency>
                   <groupId>org.springframework</groupId>
                   <artifactId>spring-web</artifactId>
                   <version>${springframework}</version>
            </dependency>
            <dependency>
                   <groupId>org.springframework</groupId>
                   <artifactId>spring-webmvc</artifactId>
                   <version>${springframework}</version>
            </dependency>
            <dependency>
                   <groupId>org.springframework</groupId>
                   <artifactId>spring-context</artifactId>
                   <version>${springframework}</version>
            </dependency>
</dependencies>
      <build>
            <finalName>SpringMvnPro</finalName>
            <defaultGoal>package</defaultGoal>       
            <testSourceDirectory>src/test/java</testSourceDirectory>
            <testResources>
                  <testResource>
                        <directory>src/test/resources</directory>
                  </testResource>
            </testResources>
            <plugins>
                  <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>2.1</version>
                        <configuration>
                              <source>1.6</source>
                              <target>1.6</target>
                        </configuration>
                  </plugin>
                  <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-site-plugin</artifactId>
                        <version>2.3</version>
                        <configuration>
                              <locales>en</locales>
                        </configuration>
                  </plugin>
                  </plugins>
      </build>
</project

3 comments:

  1. Oh gosh, I disagree, but, nonetheless, it's an interesting experience, RRS subscribed!

    ReplyDelete
  2. Nice tutorial.

    First time I got maven based web application example. I have seen many examples on maven for stand alone application but for web based application this tutorial really very useful.
    Thanks for sharing your experience with us.

    Thanks,
    Binod Suman
    Bangalore, India

    ReplyDelete