简体   繁体   中英

Maven dependency javax.servlet must be included in POM for new build server

I have a new nexus server, and a new build server. I mvn package a project and get the following error when it gets to the testing phase:

org.mule.api.registry.RegistrationException: Failed to invoke lifecycle phase "initialise" on object: org.mule.config.bootstrap.SimpleRegistryBootstrap@191a01dd (org.mule.api.lifecycle.LifecycleException)

Caused by: java.lang.ClassNotFoundException: javax.servlet.http.HttpServletRequest
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 80 more
NOT FOUND

I created a barebone archetype pom as I was troubleshooting this. This included javax.servlet, so I figured that might be it, and it did seem to solve the problem.

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  <version>${servlet-api-release-version}</version>
  <scope>provided</scope>
</dependency>

Open ended question - why might this be?

When you are compiling classes which are importing something from javax.servlet package, you need to have that dependency on your class path.

For example when you have public class FooServlet extends HttpServlet , then the Java compiler needs to be able to get javax.servlet.http.HttpServlet to check whether the parent is not final , whether you are implementing all abstract methods, etc.


javax.servlet dependency then needs to be marked as provided in pom.xml . This is because all servlet containers already include that dependency. If you fail to exclude this library from you WAR, the HttpServlet class in your web application will be different from HttpServlet class used by the servlet container (ie FooServlet instanceof HttpServlet would be false as classes in Java are not only defined by their qualified name, but also by their class loader).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM