2017-08-25 89 views
0

我已經經歷了一些教程閱讀,包Spring的web項目到罐子(尤伯杯JAR)無彈簧啓動(非彈簧啓動項目)

https://spring.io/guides/gs/spring-boot/

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-tool-plugins-gradle-plugin

我也看到有人問here,但它使用Maven,我試圖使用Gradle,但它不起作用。

我真的不能在非Spring引導項目中工作,所以我的問題是,是否可以在非Spring引導項目中打包uber-jar?

我的Spring項目是正常的MVC項目,由Gradle構建,是否有任何Gradle插件可以實現我的目標?或者實際上,Spring啓動插件可以在非Spring啓動項目上執行它?

+0

請詳細說明一下你爲什麼要這麼做? –

+0

@ZakiAnwarHamdani我喜歡spring-boot超級jar包的功能,但我們的網絡應用程序現在是普通的spring-mvc項目,我不允許將它轉換爲spring-boot。我需要將jar文件傳遞給非IT人員,以測試誰不安裝tomcat或任何獨立服務器。如果他們只是用可嵌入的Java容器向可執行jar發出命令,那就太好了。 – GMsoF

回答

0

你可以使用embedded tomcat來做到這一點。可能是本文將幫助你Create a Java Web Application Using Embedded Tomcat
這裏是我的TomcatBootstrap代碼

public class TomcatBootstrap { 
private static final Logger LOG = LoggerFactory.getLogger(TomcatBootstrap.class); 
public static void main(String[] args) throws Exception{ 
    System.setProperty("tomcat.util.scan.StandardJarScanFilter.jarsToSkip", "*.jar"); 
    int port =Integer.parseInt(System.getProperty("server.port", "8080")); 
    String contextPath = System.getProperty("server.contextPath", ""); 
    String docBase = System.getProperty("server.docBase", getDefaultDocBase()); 
    LOG.info("server port : {}, context path : {},doc base : {}",port, contextPath, docBase); 
    Tomcat tomcat = createTomcat(port,contextPath, docBase); 
    tomcat.start(); 
    Runtime.getRuntime().addShutdownHook(new Thread() { 
     @Override 
     public void run(){ 
      try { 
       tomcat.stop(); 
      } catch (LifecycleException e) { 
       LOG.error("stoptomcat error.", e); 
      } 
     } 
    }); 
    tomcat.getServer().await(); 
} 

private static String getDefaultDocBase() { 
    File classpathDir = new File(Thread.currentThread().getContextClassLoader().getResource(".").getFile()); 
    File projectDir =classpathDir.getParentFile().getParentFile(); 
    return new File(projectDir,"src/main/webapp").getPath(); 
} 
private static Tomcat createTomcat(int port,String contextPath, String docBase) throws Exception{ 
    String tmpdir = System.getProperty("java.io.tmpdir"); 
    Tomcat tomcat = new Tomcat(); 
    tomcat.setBaseDir(tmpdir); 
    tomcat.getHost().setAppBase(tmpdir); 
    tomcat.getHost().setAutoDeploy(false); 
    tomcat.getHost().setDeployOnStartup(false); 
    tomcat.getEngine().setBackgroundProcessorDelay(-1); 
    tomcat.setConnector(newNioConnector()); 
    tomcat.getConnector().setPort(port); 
    tomcat.getService().addConnector(tomcat.getConnector()); 
    Context context =tomcat.addWebapp(contextPath, docBase); 
    StandardServer server =(StandardServer) tomcat.getServer(); 
    //APR library loader. Documentation at /docs/apr.html 
    server.addLifecycleListener(new AprLifecycleListener()); 
    //Prevent memory leaks due to use of particularjava/javax APIs 
    server.addLifecycleListener(new JreMemoryLeakPreventionListener()); 
    return tomcat; 
} 

private static Connector newNioConnector() { 
    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); 
    Http11NioProtocol protocol =(Http11NioProtocol) connector.getProtocolHandler(); 
    return connector; 
} 

}