我正在从 STS 创建一个标准的 Spring Boot 1.5.2“Web”项目。如果你运行这个应用程序,你会创建两个目录——普通的“base”目录和一个“tomcat-docbase”目录
. . . 4096 Mar 29 10:00 tomcat.2743776473678691880.8080
. . . 4096 Mar 29 10:00 tomcat-docbase.76291847886629412.8080
如果我将此项目更改为 WAR 项目,我只会获得“基本”目录
. . . 4096 Mar 29 10:06 tomcat.3131223012454570991.8080
使用
很容易覆盖默认的基本目录 server.tomcat.basedir=.
但是这对 tomcat-docbase 没有影响。以编程方式覆盖 tomcat-docbase 是可能的,但看起来像是 hack。
有人认为这是一个错误吗?
最佳答案
解决方法: 在您的项目下创建一个名为 public 的文件夹,与您的 JAR 在同一文件夹中。
原因: 从springboot代码来看,没有配置docbase文件夹。 但是你可以在你的项目文件夹中创建一个公共(public)根文件夹,名称为public、static或src/main/webapp,那么springboot永远不会再次为您创建临时 tomcat-docbase 文件夹。
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;
}
关于java -/tmp/tomcat-docbase 始终使用 Spring Boot JAR(但不是 WAR)创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43089611/