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>