使用 maven 为 spring-boot web 应用程序生成工作 war 文件

generating a working war file with maven for a spring-boot web application

我们有一个 maven 管理的 spring-boot 应用程序,我们现在需要使用 WAR 文件在 tomcat 中部署它。在开发过程中,我们使用 maven 使用以下命令启动嵌入式 tomcat:

mvn -D"-classpath %classpath package.path.App" -D"exec.executable=java" process-classes org.codehaus.mojo:exec-maven-plugin:1.2.1:exec

我可以通过 运行 mvn war:war 构建一个 war 文件,但是如果我尝试部署生成的 war,则会产生错误:

SEVERE: ContainerBase.addChild: start: 
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext
...
Caused by: java.lang.IllegalStateException: No SpringApplication sources have been defined. Either override the configure method or add an @Configuration annotation

我尝试将 mainClass 指令添加到 maven-war-plugin 的配置中,但无济于事。

我按照 dunni 的建议成功构建了一个 war。

基本上,我需要在扩展 SpringBootServletInitializer 的 class 上添加一个 @SpringBootApplication 注释,并覆盖它的 configure 方法。

所以我的代码差异如下:

+@SpringBootApplication
 public class WebAppInitializer extends SpringBootServletInitializer {

+        @Override
+       protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
+               return application.sources(App.class);
+       }

我仍然有一个错误,但只是在 spring 开始之后,但我会在另一个问题中询问这个问题。