问题描述
我只想知道我使用以下方式启动 spring boot 应用程序有什么区别:
I just want to know is there any difference between I start a spring boot application with :
mvn spring-boot:run
和
java -jar target/the-packaged-file.war (Or in IDE, right click and run java application)
推荐答案
好问题.
我们打算 mvn spring-boot:run
和从 IDE 运行您的应用程序之间没有区别.两者都使用项目的类路径启动一个主类.当您处理您的应用程序时,强烈建议使用后者:如果您从您的 IDE 运行您的应用程序,您将获得所有不错的功能,例如调试和开发工具.当您需要以标准方式在命令行上运行应用程序时,将使用前者.maven 插件还可以在集成测试阶段之前启动您的应用程序(请参阅 Maven/Gradle 插件的 start
目标).如果您使用构建来过滤某些资源,您的 IDE 应该注意,否则当您从 IDE 启动应用程序时,某些数据可能不会被过滤.
We intend that there's no difference between mvn spring-boot:run
and running your app from your IDE. Both are starting a main class using the classpath of the project. The latter is strongly recommended when you're working on your app: if you run your app from your IDE you'll get all the nice features such as debugging and devtools. The former is used when you need a standard way to run the app on the command-line. The maven plugin can also start your app before the integration-test phase (see the start
goal of the Maven/Gradle plugin). If you use the build to filter some resources, your IDE should be aware of that otherwise some data may not be filtered when you start the app from the IDE.
java -jar yourapp.jar
正在最终工件上运行.Spring Boot 负责构建应用程序的类路径.过滤后的资源存储在 jar 中.但最终,它的目的是做完全相同的事情.
java -jar yourapp.jar
is running on the final artifact. Spring Boot takes care of building the classpath of the application. Filtered resources are stored in the jar. But in the end, it is meant to do the exact same thing.
现在您的问题中包含 war
.你不能指望常规的 war
回调应用于作为主类运行的东西.话虽如此,Spring Boot 尽最大努力为您提供所有工具以使其保持一致.从正在运行的 servlet 容器启动的 war
不会从 main 方法初始化,而是从 servlet 初始化程序初始化.但是,您可以共享初始化代码以使其保持一致.插件的运行目标将添加 src/main/webapp
以便也提供这些文件.
Now your question has war
in it. You can't expect the regular war
callback to apply to something that is ran as a main class. Having said that, Spring Boot does its best to give you all the tools to make that consistent. A war
that is started from an running servlet container won't be initialized from the main method but a servlet initializer. You have a way however to share the initialization code so that it's consistent. The run goal of the plugin will add src/main/webapp
so that those files are served as well.
TL;DR 如果您要在所有环境中寻找一致的故事,请不要使用战争包装.
TL;DR if you're looking for a consistent story across all environments, don't use war packaging.
这篇关于mvn spring-boot:run 和 Application.main() 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!