在VSCODE中,如何在编辑源文件时自动运行任务?

huangapple go评论64阅读模式
英文:

In VSCODE how to run a task automatically when a source file is edited?

问题

Visual Studio Code(VSCode)中,使用一个名为sfeaappMaven化Java Web应用程序,每当更改sfeaapp项目的源代码或资源时,如何自动执行mvn package任务?

我在tasks.json中定义了以下任务,但我不知道如何使它在编辑并保存项目文件(例如.jsp文件)时自动触发:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "auto package",
            "type": "shell",
            "command": "mvn package -f C:\\VSCODE_PROJECTS\\sfeaapp\\pom.xml",
            "args": [],
            "group": "build",
            "presentation": {
                "reveal": "always",
                "panel": "shared"
            },
            "problemMatcher": []
        }
    ]
}

如果我手动启动任务auto package,它可以正常工作。

英文:

In Visual Studio Code (VSCode), working with a mavenized Java web application, which I call sfeaapp project.

What can be done so that every time a source or resource of that sfeaapp project is changed, the mvn package task is automatically executed?

I have defined the following task in tasks.json, but I don't know how to make it fire automatically when I edit and save a project file, for example a .jsp:

    {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "auto package",
                "type": "shell",
                "command": "mvn package -f C:\\VSCODE_PROJECTS\\sfeaapp\\pom.xml",
                "args": [],
                "group": "build",
                "presentation": {
                    "reveal": "always",
                    "panel": "shared"
                },
                "problemMatcher": []
            }
        ]
    }

The task auto package works fine if I launch it explicitly.

答案1

得分: 1

使用扩展multi-command,您可以重新定义Ctrl+S键绑定以保存文件并在稍等片刻后调用任务。

在您的keybindings.json中添加以下内容:

{
    "key": "ctrl+s",
    "command": "extension.multiCommand.execute",
    "args": {
        "interval": 1000,
        "sequence": [
            "workbench.action.files.save",
            { "command": "workbench.action.tasks.runTask",
              "args": "auto package" }
        ]
    },
    "when": "resourceDirname == /users/mememe/project/src"
}

将目录调整为工作区目录路径。

英文:

with the use of the extension multi-command you can re-define the keybinding for Ctrl+S to save the file and call the task after a small wait.

Add to your keybindings.json

{
    "key": "ctrl+s",
    "command": "extension.multiCommand.execute",
    "args": {
        "interval": 1000,
        "sequence": [
            "workbench.action.files.save",
            { "command": "workbench.action.tasks.runTask",
              "args": "auto package" }
        ]
    },
    "when": "resourceDirname == /users/mememe/project/src"
}

Adjust the directory to the workspace directory path.

huangapple
  • 本文由 发表于 2023年5月29日 19:56:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76357153.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定