我想用 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
最佳答案
- org.eclipse.jetty.annotations.AnnotationConfiguration 应作为最后一个配置添加,否则 WEB-INF/lib 目录中的 jar 不会添加到类路径
- 在 pom.xml 中,插件不应位于pluginManagement block 中
- 删除覆盖 spring-web
关于java - 与 maven、jetty9、spring4 一起运行的 war 并且没有 web.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28049350/