【问题标题】:Package Spring web project into jar (uber-jar) without spring boot (non spring-boot project)将 Spring web 项目打包成 jar(uber-jar),无需 spring boot(非 spring-boot 项目)
【发布时间】:2018-02-03 02:27:02
【问题描述】:

我已经阅读了一些教程,

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-boot 项目中工作,所以我的问题是,是否可以在非 Spring-boot 项目中打包 uber-jar?

我的 Spring 项目是普通的 MVC 项目,由 Gradle 构建,是否有任何 Gradle 插件可以实现我的目标?或者实际上 Spring-boot 插件可以在非 Spring-boot 项目上做到这一点?

【问题讨论】:

  • 请详细说明您为什么要这样做?
  • @ZakiAnwarHamdani 我喜欢 spring-boot uber-jar 包功能,但是我们现在的 web 应用程序是普通的 spring-mvc 项目,我不允许将其转换为 spring-boot。而且我需要将 jar 文件传递​​给非 IT 人员来测试谁没有安装 tomcat 或任何独立服务器。如果他们可以使用可嵌入的 java 容器向可执行 jar 发出命令,那就太好了。

标签: java spring spring-mvc spring-boot gradle


【解决方案1】:

您可以使用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;
}

}

【讨论】:

    猜你喜欢
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    • 2017-11-17
    • 2015-06-10
    相关资源
    最近更新 更多