vscode-tomcat icon indicating copy to clipboard operation
vscode-tomcat copied to clipboard

Hot replace of static resources (html, png, etc.)

Open kalatchev opened this issue 4 years ago • 5 comments

  • VSCode Version: 1.44.0
  • OS Version: Windows 10
  • Tomcat Extension Version: 0.11.1

Extension works just fine. Excellent job! However, I have a question/issue with hot reload. When I change some java class, hot relaod does work. But when I change some static files, such as html, I am not able to tell the Tomcat to reload it. How can I do that?

kalatchev avatar Apr 16 '20 00:04 kalatchev

any update in this. I am facing the same issue. I try to play around server.xml but I am not having any luck

obarbier avatar May 11 '20 13:05 obarbier

Same issue.

wecharyu avatar May 12 '20 03:05 wecharyu

@kalatchev you can use extension of fsdeploy, the setting follow below in user setting on vscode

"fsdeploy.nodes": [

    {
        "source": "${your java source WEB-INF path}",
        "target": "${your java target WEB-INF path}",
        "include": "**/*.*",
        "exclude": "**/min/*.*"
    }
]

Then you can run tomcat project and modify static resources. After edited, you can refresh web page and you will found it's changed.

HKChen avatar Aug 14 '20 04:08 HKChen

I confirm that fsdeploy is a good solution. Just do it before using fsdeploy: determine Tomcat workspace in the setting of plugin then choose the target from this path

vutahoanghiep avatar Nov 19 '21 11:11 vutahoanghiep

I found a solution inspired by IntelliJ's Smart Tomcat plugin's approach, which does not need repeated copying of files For this, we need to change the configuration of Tomcat a little bit.

  1. First, run mvn compile followed by mvn war:exploded ( no need to generate WAR file, we directly use the webapp folder instead). This can be setup as a VS Code task like following:
{
    "version": "2.0.0",
    "tasks": [
        // ... other tasks ...
        {
            "label": "compile",
            "type": "shell",
            "command": "mvn compile"
        },
        {
            "label": "generate webapp",
            "type": "shell",
            "command": "mvn war:exploded"
        },
        {
            "label": "Build Webapp",
            "dependsOrder": "sequence",
            "dependsOn": [
                "compile", "generate webapp"
            ]
        }
    ]
}

This will create lib and classes directories in newly created target/<project-name>/WEB-INF directory, among others.

  1. Right click on the Tomcat server in left pane, and click on Open Server Configuration. This will open server.xml file

  2. Then, in <Host> element, add the <Context> element as follows:

      <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b"/>

        <!-- ADD THIS -->
        <Context docBase="/<absolute-path-to-project>/src/main/webapp" path="/<project-name>">
          <Resources>
            <PreResources base="/<absolute-path-to-project>/target/<project-name>/WEB-INF/classes" className="org.apache.catalina.webresources.DirResourceSet" webAppMount="/WEB-INF/classes"/>
            <PostResources base="/<absolute-path-to-project>/target/<project-name>/WEB-INF/lib" className="org.apache.catalina.webresources.DirResourceSet" webAppMount="/WEB-INF/lib"/>
          </Resources>
        </Context>
        <!-- ADD THIS -->

      </Host>

Note that the absolute paths to lib and classes directories can be obtained by Right click > Copy path in left pane. Make sure you replace the <project-name>, and <absolute-path-to-project> at 3 places.

Now the Tomcat Setup in ready. Above steps are needed only one time for a project.

Right click on the target/<project-name> folder and choose Debug on Tomcat Server. All files under the src/main/webapp directory will be directly used by Tomcat so changes will reflect with simple browser refresh, and changes to Java files will be reflected with Hot Reload as usual.

If you use the hot reloading feature, I suggest running mvn compile before each time you start the server.

stellarbeam avatar Mar 01 '22 12:03 stellarbeam