How to run a Spring Boot maven project in VSCode and how to config the base url of a spring boot web application

I have a Maven project which I need to run from VSCode. Right now the way I do it is:

  1. Open the project folder in VSCode. Edit the java, js, html files etc.
  2. Start my tomcat by running bin/startup.sh; tail -f logs/* ; in the apache tomcat’s directory.
  3. Open terminal in the project directory and run mvn clean install -DskipTests.

  4. Then run cp /Users/path-to-my-project/target/myWebApp.war ~/apache-tomcat-8.5.23/webapps/ to copy the war file into the tomcat’s webapp directory.

After which I can access my web application at localhost:8080/myWebApp.

Is it possible to do all of this in one click (or command) in VSCode. I know it can be done in Eclipse or IntelliJ but I want to work with VSCode.

I have installed the Spring Boot Extension Pack and Java Extension Pack in VSCode. I am just confused on how to setup the path to my tomcat, build the project and then copy the war file to the tomcat webapps folder.

Answer

Is it possible to do all of this in one click (or command) in VSCode. I know it can be done in Eclipse or IntelliJ but I want to work with VSCode.

To make your webapp visitable, just type:

Ctrl+`   

in the VS Code to get a terminal, and then type:

mvnw spring-boot:run

in the application there is a suffix after the locahost:8080. So something like localhost:8080/mywebapp/…rest of the url. With the above method everything works but I loose that web app name suffix. Any idea how I can get it back?

In your application.properties file(in the src/main/resources folder in your spring boot project), add below line(for Spring boot v2.0.5):

server.servlet.context-path=/mywebapp

For older version, you may need:

server.contextPath=/mywebapp    

See these links for reference:

How to set base url for rest in spring boot?

What is the purpose of mvnw and mvnw.cmd files?

how to run springboot app in visual studio code? is it possible or not?

Leave a Reply

Your email address will not be published. Required fields are marked *