0

I want to create a runnable war with Jetty9 and Spring4 and still keep it as deployable war (sort of like Jenkins)

The war file is built with Maven so that maven-war-plugin moves main class(WebAppRunner) to the top of file tree, overlays all the jetty-* jars, javax* jars and spring-web.jar inside war (to be accessible to main class) and sets main class in META-INF/MANIFEST.MF.

The main class starts Jetty like this:

ProtectionDomain domain = WebAppRunner.class.getProtectionDomain();
URL location = domain.getCodeSource().getLocation();

WebAppContext context = new WebAppContext();
context.setContextPath( "/" );
context.setWar( location.toExternalForm() );
context.setParentLoaderPriority( true );
context.setConfigurations( new Configuration[] { 
  new AnnotationConfiguration(),
  new WebInfConfiguration(), 
  new WebXmlConfiguration(),
  new MetaInfConfiguration(),
  new PlusConfiguration(), 
  new JettyWebXmlConfiguration() 
} );

Server server = new Server( 8080 );
server.dumpStdErr();
server.setHandler( context );
try {
  server.start();
  server.join();
} catch ( Exception e ) {
  LOG.warn( e );
} 

Jetty itself starts without problems and during startup WebAppContext scans through WEB-INF/lib and WEB-INF/classes folders inside the war file to pickup SpringServletContainerInitializer as implementation of ServletContainerInitializer which in turn should start the web application.

However AnnotationConfiguration.getNonExcludedInitializers method doesn't find any initializers (ServiceLoader returns empty iterable).

I created a small demo project on github to demonstrate this (it overrides AnnotationConfiguration with MyAnnotationConfiguration only to add log entries). You can build with:

mvn clean compile war:exploded antrun:run war:war

and run with:

java -jar target/myapp.war

or to get more logging:

java -Dorg.eclipse.jetty.LEVEL=DEBUG -jar target/myapp.war

Here are few of articles/issues/sources that I've already looked through: 1, 2, 3, 4, 5, 6, 7, 8, 9

Community
  • 1
  • 1
Ragnar
  • 1,387
  • 4
  • 20
  • 29
  • Did you have a look at [Spring Boot](http://projects.spring.io/spring-boot/)? It makes it easy to create a stand-alone runnable application that has an embedded Jetty or Tomcat, and you can also package your app as a WAR that can be deployed the traditional way. – Jesper Jan 20 '15 at 15:47
  • I did and I'm considering it as a last resort. – Ragnar Jan 20 '15 at 16:13

1 Answers1

1
  1. org.eclipse.jetty.annotations.AnnotationConfiguration should be added as last configuration, otherwise jar's in the WEB-INF/lib dir are not added to the classpath
  2. in pom.xml plugins should not be in pluginManagement block
  3. remove overlay spring-web
Madis Pärn
  • 106
  • 5
  • Sweet :) Its working! I updated the project on [github](https://github.com/ragnarruutel/java8-jetty9-spring4-noxml) for anyone looking for reference. – Ragnar Mar 15 '15 at 14:27