Before you begin
- Install Apache Maven 2.2.x
- Install Eclipse (preferably 3.5.x version or later)
- Install Maven plugin for Eclipse
- Configure Maven plugin by going to Windows -> Preferences -> Maven. Add and use your external maven installation instead of embedded version.
Define Maven Project
- To begin developing your web application, start by creating a Maven Project first.
- Go to File -> New -> Other
- Select Maven -> Maven Project from the dialog
Click Next on the next screen
- 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.
- Group Id and Artifact ID on the next screen.
- e. Click Finish to create your project.
- 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.
- Add directories for your java and unit test code.
- Add java folder under
src/main
- Create a new folder
test
undersrc
and then addjava
andresources
folders undertest
. All unit tests go under this folder.
- Add java folder under
- these new folders source folders by going to the menu
File -> New -> Other (or Ctrl + N)
and then selectingJava -> Source
Folder.
- Add directories for your java and unit test code.
- Modify
pom.xml
to provide additional support.- 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>
- 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>
- 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>
- 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>
- 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>
- The original
- Add new Maven Configuration to run
jetty:run
target
- 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.
- 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.