java - 与 maven、jetty9、spring4 一起运行的 war 并且没有 web.xml

标签 java spring maven embedded-jetty

我想用 Jetty9 和 Spring4 创建一个可运行的 war ,并仍然将其保留为可部署的 war (有点像 Jenkins )

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

主类像这样启动 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取货SpringServletContainerInitializer作为 ServletContainerInitializer 的实现这反过来应该启动网络应用程序。

但是AnnotationConfiguration.getNonExcludedInitializers方法未找到任何初始化程序(ServiceLoader 返回空的可迭代对象)。

我在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

以下是我已经浏览过的一些文章/问题/来源:1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9

最佳答案

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

关于java - 与 maven、jetty9、spring4 一起运行的 war 并且没有 web.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28049350/

相关文章:

java - JDIC 的 64 位版本何时可用?

javascript - 在Java中,当我们在WebView中加载HTML时,如何使用鼠标选择一个HTML元素?

java - 无法初始化类 com.amazonaws.services.sqs.AmazonSQSClient

maven - 在 Maven 项目中包含非 Java 源代码

maven - io.fabric8:docker-maven-plugin:0.15.3:build失败

java - RecyclerView 不滚动 FirebaseUI

java - 当Redis缓存不可用时如何防止所有RedisConnectionFailureException

java - Spring MVC 和 Thymeleaf - 之间有什么区别 :value and th:field when you iterate multiple items?

Java Spring Boot - JPA : StackOverflowError with a @ManyToMany relation

java - Spring websocket - 来自集群的 sendToUser 无法从备份服务器工作