gradle-tomcat-plugin icon indicating copy to clipboard operation
gradle-tomcat-plugin copied to clipboard

Servlet annotated with @WebServlet not loaded when WEB-INF/web.xml exists

Open ghost opened this issue 12 years ago • 2 comments

Environment: JDK 1.7.0_25/Gradle 1.7/gradle-tomcat-plugin v0.9.9/Tomcat-7.0.42/Windows (both xp and 7)

Problem: My servlet is defined with @WebServlet annotation. I'm getting 404 error when WEB-INF/web.xml is there, but it works fine if I remove the file.

I don't have problem running the war (generated with 'gradle build') with the same version (7.0.42) of external Tomcat.

here is everything needed to reproduce the problem:

1> TestServlet.java

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;

@WebServlet("/hello")
public class TestServlet extends HttpServlet {

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

2> web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        metadata-complete="false" version="3.0">

    <display-name>test app</display-name>

</web-app>

3> build.gradle

apply plugin: 'java'
apply plugin: 'tomcat'

repositories {
    mavenCentral()
}

dependencies {
    def tomcatVersion = '7.0.42'
    tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
           "org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}"
    tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}") {
        exclude group: 'org.eclipse.jdt.core.compiler', module: 'ecj'
    }
    providedCompile('javax.servlet:javax.servlet-api:3.0.1')
}

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.gradle.api.plugins:gradle-tomcat-plugin:0.9.9'
    }
}

Thanks

ghost avatar Sep 23 '13 20:09 ghost

Just noticed TomcatRun.groovy has following method, which seems causing the above problem. I didn't try anything yet though, so I could be wrong.

private boolean isClassesJarScanningRequired() {
    isTomcat7x() && !existsWebXml()
}

ghost avatar Sep 24 '13 21:09 ghost

Just tried it. Simply removing '&& !existsWebXml()' solves my problem. Although it's not a generic solution but good enough for me. Anyway, I hope you would consider to fix it in the future release. And thanks for the plugin.

ghost avatar Sep 24 '13 21:09 ghost