What's the difference of mvn spring-boot:run and Application.main()

后端 未结 1 396
心在旅途
心在旅途 2021-01-04 18:43

I just want to know is there any difference between I start a spring boot application with :

mvn spring-boot:run

and

java -         


        
1条回答
  •  一整个雨季
    2021-01-04 19:11

    Great question.

    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 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.

    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 if you're looking for a consistent story across all environments, don't use war packaging.

    0 讨论(0)
提交回复
热议问题