[英]How to add a spring-boot jar as a dependency in a new spring-boot project without changing the original pom
[英]Package Spring web project into jar (uber-jar) without spring boot (non spring-boot project)
我已經通讀了一些教程,
https://spring.io/guides/gs/spring-boot/
我也看到有人在這里問,但是它正在使用Maven,我曾嘗試使用Gradle,但沒有用。
我真的不能在非Spring引導項目中使用它,所以我的問題是,是否可以在非Spring引導項目中打包uber-jar?
我的Spring項目是Gradle構建的普通MVC項目,是否有任何Gradle插件可以實現我的目標? 還是實際上Spring-boot插件可以在非Spring-boot項目上做到這一點?
您可以使用embedded tomcat
來完成此工作。 也許本文將幫助您使用嵌入式Tomcat創建Java Web應用程序
這是我的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.