2024-11-25《Vscode热部署》

VsCode配置Javaweb热部署(Deploy)

 

  最近又开始使用VsCode来进行web开发,但是每次都需要package一下项目再放到Tomcat下面太繁琐了,就想着能不能像IDEA一样可以进行热部署,经过不懈百度后终于发现了解决方法。首先需要以下三个插件:

image-20221028164047290

  首先我们要去创建一个webapp项目,具体的VsCode创建maven-webapp项目大家可以百度,教程有很多,创建好web项目之后我们有两种选择,一种是在全局设置,也就是在设置里进行配置,另一种就是在工作区设置,具体使用起来没啥区别,我比较懒,所以我就直接在全局设置里设置了,如果你想要在工作区设置的话那就ctrl+shift+P,搜索settings,然后将下列代码放进去,全区配置也是一样的:

  "java.configuration.maven.userSettings":"D:\\Learning\\Environments\\Maven\\conf\\settings.xml",
  "maven.terminal.useJavaHome": true,
  "maven.terminal.customEnv": [
  {
  "environmentVariable": "JAVA_HOME",
  "value": "D:\\Learning\\Environments\\JDK\\JDK17"
  }
  ],
  "java.configuration.maven.globalSettings":"D:\\Learning\\Environments\\Maven\\conf\\settings.xml",
  "maven.executable.path": "D:\\Learning\\Environments\\Maven\\bin\\mvn.cmd",
  "maven.settingsFile": "D:\\Learning\\Environments\\Maven\\conf\\settings.xml",
  "java.maven.downloadSources": true,
  "maven.executable.options": "-DarchetypeCatalog=internal",
  //关闭自动编译
  "java.autobuild.enabled": false,
  //关闭debug前强制编译
  "java.debug.settings.forceBuildBeforeLaunch": false,
  //maven、gradle的配置文件变更后自动更新
  "java.configuration.updateBuildConfiguration": "automatic",
  //java源文件路径
  //使用Deploy插件来进行热部署时所需要的配置信息
  "deploy": {
  "packages": [
  {
  "name": "前端",
  "description": "webapp里面的所有文件",
  "files": [
  "src/main/webapp/*",
  "src/main/webapp/*/*",
  "src/main/webapp/*.*",
  "src/main/webapp/*/*.*",
  "src/main/webapp/*/*/*.*",
  "src/main/webapp/*/*/*/*.*",
  "src/main/webapp/*/*/*/*/*.*",
  "src/main/webapp/*/*/*/*/*",
  "src/main/webapp/*/*/*/*/*/*.*",
  ],
  "exclude": [
  "src/main/webapp/test/*"
  ],
  "deployOnSave": true,
  "useTargetList": true,
  "button": {
  "text": "热部署",
  "tooltip": "点击这里将前端部署到hotsite",
  "targets": [
  "HOTSITE"
  ]
  },
  }
  ],
  "targets": [
  {
  "type": "local",
  "name": "HOTSITE",
  "description": "A local folder",
  "dir": "target/lht/",
  "mappings": [
  {
  "source": "src/main/webapp",
  "isRegEx": false,
  "target": "/"
  }
  ]
  }
  ]
  },
  "java.project.sourcePaths": [
  "src/main/java",
  "src/test/java",
  ],

  上面target后面的名字就是我们热部署的文件夹,其他的我们就对应好我们的,我们编译好的class文件都会放在web-inf目录下,然后我们还需要在maven项目的pom.xml文件里配置一下我的编译输出目录:

  <sourceDirectory>src/main/java</sourceDirectory>
  <resources>
  <resource>
  <directory>src/main/resources</directory>
  <includes>
  <include>db.properties</include>
  </includes>
  <filtering>true</filtering>
  </resource>
  </resources>
  <outputDirectory>${basedir}/target/lht/WEB-INF/classes</outputDirectory>

  要放在<build></build>之间,然后我们打开命名面板,选择maven,选择compile,然后编译好了之后我们再去看就会发现已经编译到我们指定的目录下了:

image-20221028170911792

  然后左下角打开我们的server,开始配置Tomcat,第一步选择no,在本地选择到Tomcat的安装目录,然后无脑finish就可以了。

image-20221028171027032

image-20221028171040242

image-20221028171057521

image-20221028171125060

  然后点击这个add deployment,选择exploded将我们自定义的名字的那个热部署的文件夹选择到,然后选择no,最后打开Tomcat服务,点击热部署按钮(在左下方有这三个汉字,直接点击即可)。最后就是将我们的服务部署到Tomcat上,点击pulish server,然后就可以开始浏览了,每次当我们Ctrl+s的时候,就会自动部署到Tomcat,到时候我们直接点击刷新就好了。

image-20221028171313794

image-20221028171308583

image-20221028171410680

image-20221028171450900

  最后就是如果你想快速的打开网页,我们可以点击这个server action,然后选择show in browser,选择带有我们自定义文件夹那个链接即可。

image-20221028171746965

image-20221028171754041

image-20221028171812663

posted @   new菜鸟  阅读(155)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示