you're reading...


Starting web development with Maven and Eclipse

Before you begin

  1. Install Apache Maven 2.2.x
  2. Install Eclipse (preferably 3.5.x version or later)
  3. Install Maven plugin for Eclipse
  4. Configure Maven plugin by going to Windows -> Preferences -> Maven. Add and use your external maven installation instead of embedded version.

Define Maven Project

  1. To begin developing your web application, start by creating a Maven Project first.
    1. Go to File -> New -> Other
    2. Select Maven -> Maven Project from the dialog

      Click Next on the next screen
    3. Type webapp in the filter field on the next screen to narrow down the list of available Maven archetypes. Select maven-archetype-webapp to create your project.
    4. Group Id and Artifact ID on the next screen.
    5. e. Click Finish to create your project.
  2. The above step creates a basic starter project for developing web based applications using J2EE platform. This is a good start, but the generated project needs many changes to turn it in to a real project that can be used for developing the final application.
    1. Add directories for your java and unit test code.
      • Add java folder under src/main
      • Create a new folder test under src and then add java and resources folders under test. All unit tests go under this folder.
    2. these new folders source folders by going to the menu File -> New -> Other (or Ctrl + N) and then selecting Java -> Source Folder.
  3. Modify pom.xml to provide additional support.
    1. The original pom.xml looks like this
      <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/maven-v4_0_0.xsd">
      	<name>webapp Maven Webapp</name>
    2. Add plugins for compiler and creating war.
    3. Add properties section to define versions of used components.
      	<!-- Shared version number properties -->
    4. Add Jetty support
    5. Modify dependency section
  4. Add new Maven Configuration to run jetty:run target
  5. Run the maven configuration created in the previous step. Please note that when executed first time, maven download all required dependencies from the central repository. This may take a while (may be a little coffee break may not be a bad idea at this time). The following lines in the Eclipse console indicate that Jetty was started successfully.
    [INFO] [jetty:run {execution: default-cli}]
    [INFO] Configuring Jetty for project: Sample web application using Eclipse and Maven
    [INFO] Webapp source directory = C:\_work\workspace-prototypes\webapp\src\main\webapp
    [INFO] web.xml file = C:\_work\workspace-prototypes\webapp\src\main\webapp\WEB-INF\web.xml
    [INFO] Classes = C:\_work\workspace-prototypes\webapp\target\classes
    [INFO] Added extra scan target:C:\_work\workspace-prototypes\webapp\src\main\webapp\WEB-INF\web.xml
    2010-03-11 12:21:27.593::INFO:  Logging to STDERR via org.mortbay.log.StdErrLog
    2010-03-11 12:21:27.671::INFO:  jetty-6.0.x
    [INFO] Context path = /
    [INFO] Tmp directory = C:\_work\workspace-prototypes\webapp\target\work
    [INFO] Web defaults =  jetty default
    [INFO] Webapp directory = C:\_work\workspace-prototypes\webapp\src\main\webapp
    [INFO] Starting jetty null ...
    [INFO] Classpath = [file:/C:/_work/workspace-prototypes/webapp/target/classes/, file:/C:/Documents%20and%20Settings/<your dir>/.m2/repository/log4j/log4j/1.2.14/log4j-1.2.14.jar]
    2010-03-11 12:21:27.984::INFO:  Started SelectChannelConnector @
    [INFO] Started Jetty Server
    [INFO] Starting scanner at interval of 3 seconds.
  6. Point your browser to http://localhost:8080 to access your application.


That’s it folks. You are ready to start developing your web application now.


No comments yet.

Post a Comment