2017-08-25 7 views
0

パッケージの春春ブーツ(非春ブートプロジェクト)なしの瓶(ユーバー-jarファイル)へのWebプロジェクト

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を使用しようとしましたが、動作しません。

私は実際にそれが非春のブートプロジェクトで動作することができないので、私の質問は、非春のブートプロジェクトでは、uber-jarをパッケージ化することは可能ですか?

My Springプロジェクトは通常のMVCプロジェクトで、Gradleによって構築されていますが、私の目標を達成できるGradleプラグインはありますか?または、実際にSpringブートプラグインは、Spring以外のプロジェクトでもそれを実行できますか?

+0

であなたを助けるかもしれ手の込んだしてください? –

+0

@ ZakiAnwarHamdani私はスプリングブーツのウーファージャーのパッケージ機能が好きですが、私たちのウェブアプリケーションは現在、通常のスプリングMVCプロジェクトです。私はスプリングブーツに変換することはできません。また、tomcatやスタンドアロンサーバーを誰がインストールしないのかをテストするために、IT以外の人にjarファイルを渡す必要があります。彼らは埋め込み可能な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; 
} 

}