简体   繁体   English

如何在VSCode中运行Spring Boot Maven项目以及如何配置Spring Boot Web应用程序的基本URL

[英]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. 我有一个Maven项目,需要从VSCode运行。 Right now the way I do it is: 现在,我的方法是:

  1. Open the project folder in VSCode. 在VSCode中打开项目文件夹。 Edit the java, js, html files etc. 编辑Java,JS,HTML文件等。
  2. Start my tomcat by running bin/startup.sh; tail -f logs/* ; 通过运行bin/startup.sh; tail -f logs/* ;启动我的tomcat bin/startup.sh; tail -f logs/* ; bin/startup.sh; tail -f logs/* ; in the apache tomcat's directory. 在apache tomcat的目录中。
  3. Open terminal in the project directory and run mvn clean install -DskipTests . 在项目目录中打开终端,然后运行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. 然后运行cp /Users/path-to-my-project/target/myWebApp.war ~/apache-tomcat-8.5.23/webapps/将war文件复制到tomcat的webapp目录中。

After which I can access my web application at localhost:8080/myWebApp. 之后,我可以通过localhost:8080 / myWebApp访问我的Web应用程序。

Is it possible to do all of this in one click (or command) in VSCode. 是否可以在VSCode中一键(或命令)完成所有这些操作。 I know it can be done in Eclipse or IntelliJ but I want to work with VSCode. 我知道可以在Eclipse或IntelliJ中完成,但我想使用VSCode。

I have installed the Spring Boot Extension Pack and Java Extension Pack in VSCode. 我已经在VSCode中安装了Spring Boot Extension Pack和Java Extension Pack。 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. 我只是对如何设置tomcat的路径,构建项目然后将war文件复制到tomcat webapps文件夹感到困惑。

Is it possible to do all of this in one click (or command) in VSCode. 是否可以在VSCode中一键(或命令)完成所有这些操作。 I know it can be done in Eclipse or IntelliJ but I want to work with VSCode. 我知道可以在Eclipse或IntelliJ中完成,但我想使用VSCode。

To make your webapp visitable, just type: 要使您的Web应用可访问,只需键入:

Ctrl+`   

in the VS Code to get a terminal, and then type: 在VS Code中获取终端,然后键入:

mvnw spring-boot:run

in the application there is a suffix after the locahost:8080. 在该应用程序中,locahost:8080后有一个后缀。 So something like localhost:8080/mywebapp/...rest of the url. 所以类似localhost:8080 / mywebapp / ... URL的其余部分。 With the above method everything works but I loose that web app name suffix. 使用上述方法,一切正常,但我松开了该Web应用程序名称后缀。 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): 在您的application.properties文件中(在Spring Boot项目的src/main/resources文件夹中),添加以下行(对于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? 如何在Spring Boot中设置基本URL以进行休息?

What is the purpose of mvnw and mvnw.cmd files? mvnw和mvnw.cmd文件的目的是什么?

how to run springboot app in visual studio code? 如何在Visual Studio代码中运行springboot应用程序? is it possible or not? 有没有可能?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM