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

标签 java spring spring-mvc spring-boot gradle

我已经通读了一些教程,

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项目是Gradle构建的普通MVC项目,是否有任何Gradle插件可以实现我的目标?还是实际上Spring-boot插件可以在非Spring-boot项目上做到这一点?

最佳答案

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

}

关于java - 将Spring Web项目打包到没有Spring Boot的jar(uber-jar)中(非spring-boot项目),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45874591/

相关文章:

java - 在Android上使用mac地址创建tcp连接

java - Java Map 的非支持键集

maven - 在 Spring Junit 测试用例中获取 jdbcTemplate null

json - 使用@JsonFormat 时的错误时间(+1 小时)

mysql - JDBC MySQL连接-读取通讯包错误

java - 我应该将 json 字符串解析为 json 对象还是直接操作字符串

java - 不能用命令行运行liquibase

java - 在一段时间后调用 API 时第一次获取连接重置异常

spring - 在@Transactional 中的事务期间提交

spring - 如何并发处理spring集成jms channel