I just want to know is there any difference between I start a spring boot application with :
mvn spring-boot:run
and
java -jar target/the-packaged-file.war (Or in IDE, right click and run java application)
The Spring Boot Maven Plugin provides Spring Boot support in Apache Maven. It allows you to package executable jar or war archives, run Spring Boot applications, generate build information and start your Spring Boot application prior to running integration tests.
Method main is an entry point for standalone applications, so if you want to use spring boot standalone application, usually (if not always) packaged into a JAR - then yes, you should use main method.
A Spring Boot application's main class is a class that contains a public static void main() method that starts up the Spring ApplicationContext. By default, if the main class isn't explicitly specified, Spring will search for one in the classpath at compile time and fail to start if none or multiple of them are found.
The alternate to run the spring boot without building the JAR file is to run the command mvnspring-boot:run. This command makes sure that the POM. xml has the plugin which signifies that we want to use Tomcat to run our code. When the code is run in the project root folder the plugin reads the POM.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With