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