john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

maven eclipse servlet package war

PREREQUISITES: m2e eclipse maven plugin

File -> New -> Project -> Maven -> Maven Project
(probably not the default Workspace location if using an existing git repo?)

org.apache.maven.archetypes -> maven-archetype-webapp   1.0

GroupId:  com.company.utils
Artifact Id: AppProperties
Version: 0.1
Package: com.company.utils.AppProperties

Create the directories for maven standardized layout:

    mkdir /workspace/REPOSITORYNAME/AppProperties/src/main/java
    mkdir /workspace/REPOSITORYNAME/AppProperties/src/test/java

Eclipse -> Refresh the Project Explorer view to see the newly created directories

Right click on src/main/java -> Create a new package -> com.company.utils.apppropertiesui

Right click on src/main/java/com.company.utils.apppropertiesui -> Create a new Class -> AppProperties

package AppProperties;

import java.io.PrintWriter;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AppProperties extends HttpServlet
{
    private static final long serialVersionUID = 1L;

    public void doGet( HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException
    {
        PrintWriter out = response.getWriter();
        out.println( "Hi" );
    }

} // end class


The Eclipse Editor Errors are because Eclipse/Maven don't have the dependencies configured yet,
the "servlet-api" is required but often provided by your webapp server (i.e. tomcat) so can be
listed in the "provided" scope (preventing unnecessary duplication)

NOTE: compile is the default scope, Compile dependencies are available in all classpaths of a project.
Furthermore, those dependencies are propagated to dependent projects.


MODIFY the src\pom.xml

<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.oxygencloud.vm</groupId>
  <artifactId>apppropertiesui</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>apppropertiesui</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>apppropertiesui</finalName>
  </build>
</project>



MODIFY the src\main\webapp\WEB-INF\web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>AppProperties</display-name>
  <servlet>
    <description></description>
    <display-name>AppProperties</display-name>
    <servlet-name>AppProperties</servlet-name>
    <servlet-class>AppProperties</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>AppProperties</servlet-name>
    <url-pattern>/AppProperties</url-pattern>
  </servlet-mapping>
</web-app>


"Servlet Mapping" notes: When a request comes in the first successful match is used.
It's an explicit way to resolve requests and allows a "Filter" to correspond to multiple mappings.
A string containing only the / character indicates the "default" servlet of the application.

<servlet-mapping>
    <servlet-name>allrequests</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>alljsp</servlet-name>
    <url-pattern>*.jsp</url-pattern>
</servlet-mapping>


Run -> Run As -> Maven Clean
Run -> Run As -> Maven Package
Run -> Run As -> Maven Install (puts the artifact in the local maven repository)


NOW, to actually use this we need to integrate with tomcat (or just copy the war file...)

  • « HttpConnect simple
  • Recover buffalo raid drive mdadm gpt xfs »

Published

Sep 6, 2012

Category

java-servlet

~346 words

Tags

  • eclipse 22
  • java-servlet 61
  • maven 10
  • package 3
  • servlet 17
  • war 2