mavenspring-mvcembedded-tomcat-8

ClassNotFoundException: ContextLoaderListener, when using Intellij with maven tomcat8 plugin and PostResources


I am using Intellij with the maven tomcat8 plugin. I added a PostResource to my context file:

    <Resources className="org.apache.catalina.webresources.StandardRoot">
     <PostResources base="{my_post_resources_path}}"
      className="org.apache.catalina.webresources.JarResourceSet"
      internalPath="/post-resources"
      webAppMount="/WEB-INF/classes/post-resources"/>
    </Resources>

and it is being picked up, no problem. When I do a mvn tomcat8:run from command line it works just fine. But when I run it from within Intellij, from within the Maven sidebar, by right-clicking on the tomcat8 plugin, then tomcat8:run, I get this error:

    SEVERE: Error configuring application listener of class 
    org.springframework.web.context.ContextLoaderListener
    java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    SEVERE: Error configuring application listener of class 
    org.springframework.web.context.request.RequestContextListener
    java.lang.ClassNotFoundException: org.springframework.web.context.request.RequestContextListener

I realize both of these classes are found within spring-web, which I have as a dependency in my pom:

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.3.26.RELEASE</version>
    </dependency>

I have confirmed that in the target/war/WEB-INF/lib directory of my project, the spring-web jar is there. I have seen many posts about getting this to work with Eclipse, by manually adding jars to the deployment. And I've seen you can do something similar in Intellij, in File -> Project structure -> Artifacts, but couldn't see where to add depenedencies, and I don't think that will work anyway. Someone said on one post to add spring-context-support as a dependency, which I did. Same result.

I've closed and reponed Intellij many times, run and re-run mvn clean install ... nothing will make tomcat start properly from within Intellij. Any ideas?

UPDATE: I have found that this is also happening when running from command-line, so it is not an Intellij thing. It seems to be related to the fact I am using web.xml instead of Java config. My understanding was that should work with web.xml.

In my web.xml I have defined:

    <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
      <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

If I comment them out, the error goes away. But of course then the app doesn't start because there is no ContextLoaderListener. :(


Solution

  • The problem was the configuration of the tomcat8-maven-plugin, I was missing the warSourceDirectory.

        <configuration>
          <port>8080</port>
          <path>/myapp</path>
          <addContextWarDependencies>true</addContextWarDependencies>
          <contextFile>${project.build.outputDirectory}/myapp.xml</contextFile>
          <warSourceDirectory>${project.build.directory}/${project.build.finalName}</warSourceDirectory>
        </configuration>