//
you're reading...

Development

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">
      	<modelVersion>4.0.0</modelVersion>
      	<groupId>com.example</groupId>
      	<artifactId>webapp</artifactId>
      	<packaging>war</packaging>
      	<version>0.0.1-SNAPSHOT</version>
      	<name>webapp Maven Webapp</name>
      	<url>http://maven.apache.org</url>
      	<dependencies>
      		<dependency>
      			<groupId>junit</groupId>
      			<artifactId>junit</artifactId>
      			<version>3.8.1</version>
      			<scope>test</scope>
      		</dependency>
      	</dependencies>
      	<build>
      		<finalName>webapp</finalName>
      	</build>
      </project>
      
    2. Add plugins for compiler and creating war.
      	<build>
              <plugins>
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-compiler-plugin</artifactId>
                      <version>2.0.2</version>
                      <configuration>
                          <source>1.6</source>
                          <target>1.6</target>
                      </configuration>
                  </plugin>
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-war-plugin</artifactId>
                      <version>2.1-beta-1</version>
                      <configuration>
                          <failOnMissingWebXml>false</failOnMissingWebXml>
                      </configuration>
                  </plugin>
              </plugins>
      	
      		<finalName>webapp</finalName>
      	</build>
      
    3. Add properties section to define versions of used components.
      	<!-- Shared version number properties -->
      	<properties>		  
                     <junit.version>4.4</junit.version>
      		<log4j.version>1.2.14</log4j.version>
      	</properties>
      </project>
      
    4. Add Jetty support
                  <plugin>
                      <groupId>org.mortbay.jetty</groupId>
                      <artifactId>maven-jetty-plugin</artifactId>
                      <version>6.0.0</version>
                      <configuration>
                          <contextPath>/</contextPath>
                          <scanIntervalSeconds>3</scanIntervalSeconds>
                          <scanTargets>
                              <scanTarget>
                                 src/main/webapp/WEB-INF/web.xml
                              </scanTarget>
                          </scanTargets>
                      </configuration>
                  </plugin>
      
    5. Modify dependency section
      	<dependencies>
      		<dependency>
      			<groupId>log4j</groupId>
      			<artifactId>log4j</artifactId>
      			<version>${log4j.version}</version>
      			<scope>runtime</scope>
      	   </dependency>
         		
      		<dependency>
      			<groupId>junit</groupId>
      			<artifactId>junit</artifactId>
      			<version>${junit.version}</version>
      			<scope>test</scope>
      		</dependency>
      	</dependencies>
      
  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 @ 0.0.0.0:8080
    [INFO] Started Jetty Server
    [INFO] Starting scanner at interval of 3 seconds.
    
  6. Point your browser to http://localhost:8080 to access your application.

Conclusion

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

Discussion

No comments yet.

Post a Comment


*

Categories