使用 maven 为 spring-boot web 应用程序生成一个工作 war 文件
我们有一个 maven 管理的 spring-boot 应用程序,我们现在需要使用 WAR 文件在 tomcat 中部署它.在开发过程中,我们使用maven启动了一个嵌入式tomcat,命令如下:
We have a maven-managed spring-boot application that we now need to deploy in tomcat using a WAR file. During development, we used maven to start an embedded tomcat with the command:
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,就会产生一个错误:
I can build a war file by running mvn war:war
, but if I try to deploy the resulting war, an error is produced:
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
我尝试在 maven-war-plugin
的配置中添加一个 mainClass
指令,但无济于事.
I tried adding a mainClass
directive to the maven-war-plugin
's configuration, but to no avail.
我按照邓尼的建议成功地发动了一场战争.
I managed to build a war following dunni's advice.
基本上,我需要在扩展 SpringBootServletInitializer
的类上添加一个 @SpringBootApplication
注释,并覆盖它的 configure
方法.
Basically, I needed to add a @SpringBootApplication
annotation on the class extending SpringBootServletInitializer
, and override its configure
method.
所以我的代码的差异就像:
So the diff of my code is like:
+@SpringBootApplication
public class WebAppInitializer extends SpringBootServletInitializer {
+ @Override
+ protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
+ return application.sources(App.class);
+ }
我仍然有错误,但只有在 spring 开始之后,但我会在另一个问题中询问.
I still have an error but only after spring starts, but I will ask about that in a different question.