3

I'm trying to deploy a war file with an embedded Jetty. You can clone a minimal working example here: https://github.com/guilty/jetty-test

When you build the project and try to run the war-project with a jetty-runner (which can be downloaded here: http://mvnrepository.com/artifact/org.eclipse.jetty/jetty-runner/9.2.10.v20150310), everything works fine.

As you can see, I have a jar-project, which has a dependency to war-project (with classes classifier). It tries to start an embedded Jetty server and deploy the war-project, but problem is, it reads the application-context.xml twice. As I understand, it finds it twice in the classpath: once in a built *.war, and once in a dependency I made with classes classifier.

The same happens if you use jetty-runner and call Runner.main(new String[] { "path-to.war" }), though as I said before, it works fine when called from command line.

So my question, I guess, is how to restrict classpath to Jetty, if that's at all possible.

As I mentioned, this is a minimal working example, not a real-world application. If I can be of any assistance -- please let me know.

Edit: I've been asked to put relevant code here, because links may rot.

Since I won't post all the code, I'm going to describe the situation. I have a *.war project, which is a standard web application powered by spring. It has a class, which throws an exception if more than one instance of it is created by spring (which should never happen, since it's scope is singleton).

Another project references the war-project with a classes classifier (i.e. gets everything that lays in src/main/java and src/main/resources) and tries to deploy the war-file produced by war-project. Here is the relevant deployment code:

Server server = new Server(8080);
WebAppContext webAppContext = new WebAppContext(args[0], "/");

server.setHandler(webAppContext);

server.start();

This code runs, but the server fails to start, because a second instance of the bean I wrote about before gets created and it throws an exception.

web.xml of the war-project:

<?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_2_5.xsd"
         version="2.5">

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:application-context.xml</param-value>
    </context-param>
</web-app>

application-context.xml of the war-project contains only one bean, with default (singleton) scope and nothing else.

Here's the relevant pom.xml part of jar-project:

    <dependency>
        <groupId>com.github.guilty.jettyTest</groupId>
        <artifactId>web-project</artifactId>
        <version>1.0-SNAPSHOT</version>
        <classifier>classes</classifier>
    </dependency>
Paulius K.
  • 813
  • 1
  • 7
  • 13
  • please post the relevant code here. links can rot, leaving the question and answer useless at a later date – Martin Serrano Mar 23 '15 at 18:18
  • @MartinSerrano: the code is really simple and I didn't want to put all of the code in the question, so I only put the most critical parts (which too, are dead simple). I hope it's clearer now. – Paulius K. Mar 23 '15 at 18:37

0 Answers0