'/tmp/tomcat-docbase is always created with a Spring Boot JAR (but not a WAR)

From STS I'm creating a standard Spring Boot 1.5.2 'Web' project. If you run this application you get two directories created - the normal 'base' directory and a '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 get only the 'base' directory

. . .   4096 Mar 29 10:06 tomcat.3131223012454570991.8080

It's easy to override the default base directory using

 server.tomcat.basedir=.

however this has no effect on tomcat-docbase. It is possible to override tomcat-docbase programmatically but seems like a hack.

Does anyone think this is a bug?



Solution 1:[1]

Solution: create a folder name as public under your project, in the same folder with your JAR.

Reason: from the springboot code, there is no configuration for docbase folder. but you can create a common root folder in you project folder name as public, static or src/main/webapp, then the springboot will never create temp tomcat-docbase 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;
}

Link: DocumentRoot.java

Solution 2:[2]

For Spring Boot 2.x+

@Component
public class EmbeddedServletContainerConfig implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {

    @Override
    public void customize(TomcatServletWebServerFactory factory) {
        factory.setDocumentRoot(new File("/your/path/here"));
    }

}

Solution 3:[3]

I found a programmatically way of setting the docbase folder with spring and the embedded tomcat server. You must override the TomcatEmbeddedServletContainerFactory implementation like the following:

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;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dario Seidl
Solution 2 Danylo Zatorsky
Solution 3 Jose Da Silva