簡體   English   中英

使用Maven,Jetty9,spring4且沒有web.xml的可運行戰爭

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

我想使用Jetty9和Spring4進行一場可戰戰,並仍將其作為可部署戰(類似於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會通過war文件中的WEB-INF / lib和WEB-INF / classes文件夾進行掃描 ,以選擇ServletContainerInitializer的實現來獲取SpringServletContainerInitializer ,這反過來又會啟動Web應用程序。

但是, 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

這里有一些的,我已經通過看文章/問題/來源: 123456789

  1. org.eclipse.jetty.annotations.AnnotationConfiguration應該作為最后的配置添加,否則WEB-INF / lib目錄中的jar不會添加到類路徑中
  2. pom.xml中的插件不應位於pluginManagement塊中
  3. 刪除覆蓋彈簧網

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM