Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

/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?

like image 727
David Melia Avatar asked Mar 29 '17 09:03

David Melia


People also ask

What is tomcat docBase?

The docBase attribute is a path to the WAR file or exploded deployment directory. It is relative to the webapps directory, although an absolute path can be used. The path attribute is the one we are most interested in, as it defines the context path of the application.

Can we use embedded tomcat in Spring Boot for production?

Yes. It's the same tomcat, but in library form. So you are responsible for configuration and other things that the standalone tomcat provides. Also you don't have to use tomcat.


2 Answers

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

like image 50
andy Avatar answered Nov 15 '22 17:11

andy


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

}
like image 43
Danylo Zatorsky Avatar answered Nov 15 '22 17:11

Danylo Zatorsky