I checked this question ,但答案(虽然很有趣)基本上是说“不要使用 WAR 包装”,这不是这里的一个选项。
我有一个 Spring Boot 重新打包的 WAR,它使用嵌入式 Tomcat(Spring Boot 1.5.3.RELEASE,嵌入式 Tomcat 8.5.14)。重新打包的应用程序使用 mvn spring-boot:run
命令运行良好,但是当我尝试使用 `java -jar target\mywar.war' 运行它时,我得到了这个:
java.io.FileNotFoundException: file:\C:\work\spring-boot\target\spring-boot-mywar.war!\WEB-INF\classes!\mywar.war (The filename, directory name, or volume label syntax is incorrect)
这是在 Spring Boot 尝试加载“包装”war 的上下文时引起的:
Context context = tomcat.addWebapp("/mywar", Thread.currentThread().getContextClassLoader().getResource("mywar.war").getPath());
实际错误发生在嵌入式 Tomcat 类中:
private URL getWebappConfigFileFromJar(File docBase, String contextName) {
URL result = null;
try (JarFile jar = new JarFile(docBase)) {
JarEntry entry = jar.getJarEntry(Constants.ApplicationContextXml);
if (entry != null) {
result = UriUtil.buildJarUrl(docBase, Constants.ApplicationContextXml);
}
} catch (IOException e) {
Logger.getLogger(getLoggerName(getHost(), contextName)).log(Level.WARNING,
"Unable to determine web application context.xml " + docBase, e);
}
return result;
}
new JarFile(docBase)
操作抛出 FileNotFoundException
。
当使用 Maven spring-boot:run
目标时,这工作正常,所以我觉得基本结构是合理的——我认为有一些类路径问题或其他东西阻止它工作。
有没有人建议在使用WAR打包时在命令行复制spring-boot:run
的环境?
最佳答案
对于 spring boot 应用程序,您可以使用 mvn spring-boot:run
运行,使用 java -jar thePackage.war
或将 war 包放入 tomcat webapps 中。每种方式都应该能够运行您的应用。
所以我认为你的项目有问题。确保你的 pom 文件中有 spring-boot-maven-plugin
。然后当你使用 mvn package
时,你应该会看到一些关于重新打包的日志。如果您解包 war 文件,您应该会看到一些目录,例如:“META-INFO”和“BOOT-INF”。
关于java - Spring boot WAR 从 maven 运行,而不是从命令行运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43945556/