简体   繁体   English

将Spring Web项目打包到没有Spring Boot的jar(uber-jar)中(非spring-boot项目)

[英]Package Spring web project into jar (uber-jar) without spring boot (non spring-boot project)

I have read through some tutorials, 我已经通读了一些教程,

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

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

I also saw people asking here , but it is using Maven, I tried to use Gradle, but it doesn't work. 我也看到有人在这里问,但是它正在使用Maven,我曾尝试使用Gradle,但没有用。

I can't really get it works in non Spring-boot project, so my question is, is it possible to package uber-jar in non Spring boot project? 我真的不能在非Spring引导项目中使用它,所以我的问题是,是否可以在非Spring引导项目中打包uber-jar?

My Spring project is normal MVC project, built by Gradle, is there any Gradle plugin can fulfill my objective? 我的Spring项目是Gradle构建的普通MVC项目,是否有任何Gradle插件可以实现我的目标? Or actually the Spring-boot plugin can do it on non Spring-boot project? 还是实际上Spring-boot插件可以在非Spring-boot项目上做到这一点?

You can use a embedded tomcat to do that works. 您可以使用embedded tomcat来完成此工作。 May be this article will help you Create a Java Web Application Using Embedded Tomcat 也许本文将帮助您使用嵌入式Tomcat创建Java Web应用程序
And here is my TomcatBootstrap code 这是我的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;
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM