Runnable war 有 maven、jetty9、spring4 而没有 web.xml

Runnable war with maven, jetty9, spring4 and no web.xml

我想用 Jetty9 和 Spring4 创建一个 运行nable war 并且仍然保持它是可部署的 war(有点像 Jenkins

war 文件是用 Maven 构建的,因此 maven-war-plugin 将 main class(WebAppRunner) 移动到文件树的顶部,覆盖所有 jetty-* jars、javax* jars 和 spring-web.jar inside war(以便 main class 可以访问)并在 META-INF/MANIFEST 中设置 main class。中频

主要 class 像这样启动 Jetty:

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 本身启动没有问题,并且在启动 WebAppContext scans through WEB-INF/lib and WEB-INF/classes folders inside the war file to pickup SpringServletContainerInitializer as implementation of ServletContainerInitializer 期间应该启动 Web 应用程序。

但是 AnnotationConfiguration.getNonExcludedInitializers 方法没有找到任何初始化器(ServiceLoader returns 空可迭代)。

我在 github 上创建了一个小型演示项目来演示这一点(它用 MyAnnotationConfiguration 覆盖 AnnotationConfiguration 只是为了添加日志条目)。您可以使用:

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

和运行与:

java -jar target/myapp.war

或获取更多日志记录:

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

以下是我已经浏览过的 articles/issues/sources 中的一些内容:1, 2, 3, 4, 5, 6, 7, 8, 9

  1. org.eclipse.jetty.annotations.AnnotationConfiguration 应该添加为最后一个配置,否则 WEB-INF/lib 目录中的 jar 不会添加到类路径
  2. 在 pom.xml 中的插件不应在 pluginManagement 块中
  3. 删除叠加层 spring-web