/ tmp / tomcat-docbase is always built with Spring Boot JAR (but not WAR)
From STS I am creating a standard Spring Boot 1.5.2 'Web' project. If you run this application, you will create two created directories - the normal "base" directory and the "tomcat-docbase" directory
. . . 4096 Mar 29 10:00 tomcat.2743776473678691880.8080
. . . 4096 Mar 29 10:00 tomcat-docbase.76291847886629412.8080
If I change this project to a WAR project, I only get the "base" directory
. . . 4096 Mar 29 10:06 tomcat.3131223012454570991.8080
Easily override the default base directory using
server.tomcat.basedir=.
however, this does not affect tomcat-docbase. It is possible to override tomcat-docbase programmatically, but seems like a hack.
Does anyone think this is a bug?
thank
source to share
Solution: create a folder name as public in your project, in the same folder with your JAR.
Reason: There is no config for docbase folder from springboot code. but you can create a shared root folder in your project folder name as public, static or src / main / webapp, then Springboot will never create the tomcat-docbase temp folder for you again.
private static final String[] COMMON_DOC_ROOTS = { "src/main/webapp", "public", "static" }
...
public final File getValidDirectory() {
File file = this.directory;
file = (file != null ? file : getWarFileDocumentRoot());
file = (file != null ? file : getExplodedWarFileDocumentRoot());
file = (file != null ? file : getCommonDocumentRoot());
if (file == null && this.logger.isDebugEnabled()) {
logNoDocumentRoots();
}
else if (this.logger.isDebugEnabled()) {
this.logger.debug("Document root: " + file);
}
return file;
}
...
private File getCommonDocumentRoot() {
for (String commonDocRoot : COMMON_DOC_ROOTS) {
File root = new File(commonDocRoot);
if (root.exists() && root.isDirectory()) {
return root.getAbsoluteFile();
}
}
return null;
}
Ref: DocumentRoot.java
source to share
I found a programmatic way to install docbase folder with spring and embedded tomcat server. You must override the implementation TomcatEmbeddedServletContainerFactory
as shown below:
public @Bean EmbeddedServletContainerFactory embeddedServletContainerFactory() {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory("/app", 8080) {
@Override
protected void configureContext(Context context, ServletContextInitializer[] initializers) {
context.setDocBase("/path/to/your/docbase");
super.configureContext(context, initializers);
}
};
return factory;
}
source to share