Java – Deploying a Maven project with dependencies to Tomcat or Jboss running within Eclipse

eclipsejavajbossmaventomcat

I have several Maven projects in eclipse that depends on each other. Some of these have dependencies in the Spring libraries (Spring core, Spring MVC etc).

When i build the project using 'mvn clean install', maven generates a war file and places it in the ./target/ folder. I can then take this war file and deploy it on any running application server. I have tested the war file on both Tomcat 7 and Jboss 7.1.0 and it works fine.

I have a problem though if i try and run the project directly from eclipse. I would like to be able to debug the server code and the only way i know that this is possible is to run the project in an appserver running within eclipse.

I configured the Tomcat application Server on Eclipse. When i then select the Maven project and right click on it and select 'Run on Server', the project deploys on the running Tomcat instance but for some reason it produces the following error:

12-May-2012 15:48:22 org.apache.catalina.core.StandardContext listenerStart
SEVERE: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1701)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1546)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:525)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:507)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:124)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4715)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5273)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1568)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1558)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
    at java.lang.Thread.run(Thread.java:619)
12-May-2012 15:48:22 org.apache.catalina.core.StandardContext listenerStart
SEVERE: Skipped installing application listeners due to previous error(s)

I tried the same, i.e. run the project on a Jboss instance in Eclipse and it also complains that it cannot find several libraries. Most of the libraries it complains about are those referred to as dependencies in the Maven POM file.

I suspect that when i deploy the project to Tomcat or Jboss (Using Jboss tools), the dependencies are not being deployed hence the errors. Do i need any other configuration to instruct Eclipse to deploy all dependencies as well?

Note that if i deploy the war file manually it works. It just doesnt work if i deploy it using the 'Run on Server' option in eclipse.

Thanks

Best Answer

I assume that you have a WEB-INF folder in your project somewhere. Does this have a subfolder named lib that contains all the jars your project depends on?

If not, you should make sure that they are copied there. You can do this with the maven dependency plugin like this:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
    <execution>
        <phase>process-resources</phase>
        <goals>
            <goal>copy-dependencies</goal>
        </goals>
    </execution>
</executions>
<configuration>
    <outputDirectory>${project.basedir}/src/main/webapp/WEB-INF/lib</outputDirectory>
</configuration>

If the location of you WEB-INF-folder is not the same as above, remember to change it. You should also configure the clean plugin to empty this dir when you clean. Otherwise, if you remove dependencies, the jar-files will still be in the lib-folder.

Related Topic