Setup development environment for web application using/with eclipse, maven and tomcat


This tutorial covers step by step guide to set up web application using eclipse WTP (Web Tool Platform), maven and tomcat. This is tested with followings.
  1. Java SE 6 Update 25
  2. Eclipse IDE for Java EE Developers and hence Web Tool Platform 3.0
  3. Maven 3.0.3
  4. Tomcat v 6.0.26
Please follow the below steps.
1.  Download Java and Install.

2.  Download Eclipse Helios IDE which has in built support for WTP.

3.  Download Maven 3.0.3 and follow the Installation Instructions section on the same page.

4.  Download the distribution archive for Tomcat 6.0 and unzip it to the directory you wish to install Tomcat 6.0 These instructions assume you chose C:\Program Files\Tomcat.

5. Open Eclipse and select the workspace (e.g. C:\Development). Go to Help -> Install New Software and install Maven plugin for Eclipse. Provide http://m2eclipse.sonatype.org/sites/m2e in the Location.

6. Click Windows -> Preferences -> Java -> Installed JREs -> Add -> Standard VM -> Directory. Select the directory where Java was installed in step 1 above.

7. Click File -> New -> Other -> Maven -> Maven Project.




8. Click Next and select an archetype as shown below.





9. Click Next and select archetype parameters as below and click Finish.



If following error or exception is encountered, then cause is that there is settings.xml under $USER_HOME/.m2 and that points to url which does not have maven-archetype-webapp archetype.


Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webapp:RELEASE from any of the configured repositories. Could not resolve artifact Failed to resolve version for org.apache.maven.archetypes:maven-archetype-webapp:pom:RELEASE: Could not find metadata org.apache.maven.archetypes:maven-archetype-webapp/maven-metadata.xml in local (C:\Users\vkarmani.OPERATIVE\.m2\repository) Failed to resolve version for org.apache.maven.archetypes:maven-archetype-webapp:pom:RELEASE: Could not find metadata org.apache.maven.archetypes:maven-archetype-webapp/maven-metadata.xml in local (C:\Users\vkarmani.OPERATIVE\.m2\repository)


To solve the above problem, remove settings.xml temporarily and allow $MAVEN_HOME/conf/settings.xml to fetch maven-archetype-webapp archetype from default url when Finish button is clicked. After you are done with this, put your settings.xml back under $USER_HOME/.m2


10. Configure pom.xml as below.


<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.mycompany</groupId>
 <artifactId>mywebapp</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <name>mywebapp Maven Webapp</name>
 <url>http://maven.apache.org</url>
 <build>
  <pluginManagement>
   <plugins>
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-eclipse-plugin</artifactId>
     <version>2.8</version>
     <configuration>
      <downloadSources>true</downloadSources>
      <downloadJavadocs>false</downloadJavadocs>
      <wtpversion>2.0</wtpversion>
      <wtpmanifest>true</wtpmanifest>
      <additionalBuildcommands>
       <buildCommand>
        <name>org.springframework.ide.eclipse.core.springbuilder</name>
       </buildCommand>
      </additionalBuildcommands>
      <additionalProjectnatures>
       <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
      </additionalProjectnatures>
     </configuration>
    </plugin>
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-compiler-plugin</artifactId>
     <version>2.3.2</version>
     <configuration>
      <compilerVersion>1.6</compilerVersion>
      <source>1.6</source>
      <target>1.6</target>
     </configuration>
    </plugin>
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-enforcer-plugin</artifactId>
     <version>1.0</version>
    </plugin>
   </plugins>
  </pluginManagement>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <executions>
     <execution>
      <id>enforce-versions</id>
      <goals>
       <goal>enforce</goal>
      </goals>
      <configuration>
       <rules>
        <requireMavenVersion>
         <version>[3.0,)</version>
        </requireMavenVersion>
        <requireJavaVersion>
         <version>[1.6,)</version>
        </requireJavaVersion>
       </rules>
      </configuration>
     </execution>
    </executions>
   </plugin>
  </plugins>
 </build>
 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>3.8.1</version>
   <scope>test</scope>
  </dependency>
 </dependencies>

</project>
      
            
                



11. Open command prompt and go to project folder. Run the following commands.
  • mvn clean
  • mvn install
  • mvn eclipse:eclipse
12. Click Window -> Show View -> Servers.

13. Right Click in the empty area of Servers View. Click New -> Server. Select Tomcate v6.0 server and click Next. Select mywebapp under Available section and click Add >. Click Finish.
If following error is encountered then open org.eclipse.wst.common.project.facet.core.xml file under mywebapp/.settings folder and change the version from 3.0 to 2.5 for jst.web facet because Tomcat 6.0 does not support WTP 3.0. i.e.
Tomcat version 6.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 Web modules

14. Make sure 'Build Automatically' option is checked under 'Project' menu in Eclipse. Double click on server. Under 'Publishing' section, check the option 'Automatically publish after a build event'.

15. Right click on Tomcat server and click start.If following warning is observed during server start up, then stop the server and double click on the server. Check 'Publish module contexts to separate XML files' under Server Options.
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:mywebapp' did not find a matching property.

16. Open http://localhost:8080/mywebapp and you will see 'Hello World!'.

17. For configuration changes in files and major changes to get reflected , always stop the server, right click on server and click on 'Clean' option and restart the server.

No comments:

Post a Comment