Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mvn spring-boot:run vs java -jar

I know it may sound silly question but I am unable to understand the difference between mvn spring-boot:run and java -jar (.jar file generated with mvn install)

I have a spring boot application with jsp pages in /src/main/resources/META-INF/resources/WEB-INF/. If I use mvn spring-boot:run these pages are served. But If I use java -jar these pages are not found by application.

The application that I am working on is at https://github.com/ArslanAnjum/angularSpringApi

UPDATE: It works with spring boot 1.4.2.RELEASE while I intend to use the latest version i.e., 1.5.8.RELEASE.

UPDATE: Well I solved the problem by putting jsps in src/main/webapp/WEB-INF/views/ and changing packaging type to war and then running this war using java -jar target/myapp.war and its working fine now.

like image 409
ArslanAnjum Avatar asked Nov 13 '17 07:11

ArslanAnjum


People also ask

What does Mvn spring boot run does?

When you run mvn spring-boot:run , you launch a Maven build that will: Run the test-compile lifecycle goals, by default it will be resources:resources , compiler:compile , resources:testResources , compiler:testCompile goals of the Maven Resources and Compiler plugin.

Can we run spring boot application as Java application?

You can run a Spring Boot application from your IDE as a simple Java application, however, first you will need to import your project. Import steps will vary depending on your IDE and build system.


1 Answers

Short answer: spring-boot:run is a java -jar command on steroïd running as part of your Maven build, ensuring all required parameters are passed to your app (such as resources). spring-boot:run will also ensure that your project is compiled by executing test-compile lifecycle goals prior to running your app.

Long answer:

When you run java -jar, you launch a new JVM instance with all the parameters you passed to this JVM. For example, using the Spring doc example

java -Xdebug -Xrunjdwp:server=y, \     transport=dt_socket, address=8000, suspend=     -jar target/myproject-0.0.1-SNAPSHOT.jar 

You will launch a brand new JVM with the given parameters. You need to make sure to include everything needed, such as classpath elements, application parameters, JVM options, etc. on the command line.

When you run mvn spring-boot:run, you launch a Maven build that will:

  • Run the test-compile lifecycle goals, by default it will be resources:resources, compiler:compile, resources:testResources, compiler:testCompile goals of the Maven Resources and Compiler plugin.
  • Launch your application with a bunch of parameters that will depend on the Spring Boot Maven Plugin configuration you defined in your project (your pom.xml, parents and settings, command line, etc.). This includes among other things:
    • A lot of classpath elements: your target/classes folder which may contain resources and libraries required by your app, your Maven dependencies, etc.
    • Whether to fork your JVM or not (whether to create a brand new JVM to run your app or re-use the JVM of the Maven build), see fork and agent parameter of the plugin

As per:

I have a spring boot application with jsp pages in /src/main/resources/META-INF/resources/WEB-INF/. If I use mvn spring-boot:run these pages are served. But If I use java -jar these pages are not found by application.

It's because the mvn spring:boot command will make sure your target/classes folder is present in the Classpath when your app is running. After compilation, this folder will contain target/classes/META-INF/resources/WEB-INF among other things. Your app will then be able to find META-INF/resources/WEB-INF and load them when asked. When you ran java -jar command, this folder was probably not on the classpath, your app was then not able to find your resources. (these resources were copied from the src/main/resources folder during the resources:resources goal)

To have a similar result with your java -jar command, you must include your resources on the classpath such as javar -jar myapp.jar -cp $CLASSPATH;/path/to/my/project/target/classes/

like image 135
Pierre B. Avatar answered Sep 17 '22 17:09

Pierre B.