0
votes

I created a simple webapp via maven 3.1.1. I just want to develop a simple servlet. After doing a clean install I've expected the class-files of the servlet in the target/WEB-INF/classes-Directory. But there were just the copied source files.

What did I do wrong?

I haven't found any conclusion yet :(

---------- System information

  • maven version: Apache Maven 3.1.1
  • java version: 1.7.0_40 OS
  • Windows 8.1

---------- 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>de.mypage</groupId>
<artifactId>ThirdServlet</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>ThirdServlet 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>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>servlet-api</artifactId>
        <version>6.0.37</version>
    </dependency>
</dependencies>
<build>
    <finalName>ThirdServlet</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <inherited>true</inherited>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
    </plugins>
</build>

---------- File-Structure

  • src/main/resources/de/mypage/servlets/MyServlet.java
  • src/main/resources/webapp/index.jsp
  • src/main/resources/webapp/WEB-INF/web.xml

---------- My Servlet

some imports

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

/**
 * Default constructor. 
 */
public MyServlet() {
    super();
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter pw = response.getWriter();

    pw.println("<html>");
    pw.println("<body>");
    pw.println("<h2>");
    pw.println("If you can read this the servlet is running...");
    pw.println("</h2>");
    pw.println("</body>");
    pw.println("</html>");

    pw.close();
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
}

}

2
I forgot to post the webserver: It's apache tomcat 7Buddy Hu

2 Answers

2
votes

Your Java code should be in src/main/java and not in src/main/resources. Java code is compiled and resources are just copied over. These are not packages but Maven defined folders.

After you fix this issue, you may need to package the war and/or run it using the Maven tomcat plugin. The goal `install' means deploying to the local Maven repository.

In Eclipse: In Eclipse

On Filesystem: On file system

0
votes

I found a working solution for me. I just added a new path to the existing build-path in eclipse.

  1. Right Click on my project
  2. Choose Build Path > Configure Build Path
  3. Choose the "Source"-Tab
  4. Add Folder
  5. In the dialog I had to choose the location of my servlet files, in my case src/main/resources/de/mypage/servlets
  6. After that I needed to edit the output folder to src/main/webapp/WEB-INF/classes

I think that's not the perfect way - especially from the maven point of view. But it's working and I'll take a deeper look into the maven architecture...