メインコンテンツまでスキップ

デプロイの自動化

VS Codeのタスクの作成

VS Code のタスク機能を使って、デプロイを自動化します。.vscode/tasks.jsonを作成します。

tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Build WAR (WSL)",
"type": "shell",
"command": "bash -lc 'mvn -q -DskipTests=false clean package'",
"options": { "cwd": "${workspaceFolder}" },
"problemMatcher": []
},
{
"label": "Stop Tomcat10 (only if running)",
"type": "shell",
"command": "bash -lc '[ \"$(systemctl is-active tomcat10)\" = \"active\" ] && sudo systemctl stop tomcat10'"
},
{
"label": "Clean exploded webapp",
"type": "shell",
"command": "bash -lc 'sudo rm -rf /var/lib/tomcat10/webapps/servlet-demo'",
"problemMatcher": []
},
{
"label": "Deploy WAR to Tomcat (WSL)",
"type": "shell",
"command": "bash -lc 'sudo cp -f ${workspaceFolder}/target/servlet-demo.war /var/lib/tomcat10/webapps/servlet-demo.war'",
"problemMatcher": []
},
{
"label": "Start Tomcat10",
"type": "shell",
"command": "bash -lc 'sudo systemctl start tomcat10'"
},
{
"label": "Restart Tomcat10",
"type": "shell",
"command": "bash -lc 'sudo systemctl restart tomcat10'"
},
{
// まとめタスク:一括で「ビルド→停止→クリーン→デプロイ→起動」
"label": "Deploy & Start (Tomcat10, WSL)",
"dependsOn": [
"Build WAR (WSL)",
"Stop Tomcat10",
"Clean exploded webapp",
"Deploy WAR to Tomcat (WSL)",
"Start Tomcat10"
],
"dependsOrder": "sequence",
"problemMatcher": []
}
]
}

VS Codeの起動コマンドの登録

VS Code のデバッグで実行するコマンドを登録します。.vscode/launch.jsonファイルを作成します。

{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Attach to Tomcat10 (WSL)",
"request": "attach",
"hostName": "127.0.0.1",
"port": 8000,
"preLaunchTask": "Deploy & Start (Tomcat10, WSL)",
"sourcePaths": [
"${workspaceFolder}/src/main/java"
]
}
]
}

sudoコマンドの設定変更

VS Code のタスクでsudoコマンドを複数回使います。パスワードを入力する必要がないように、/etc/sudoersファイルを修正します。 sudoersファイルはvisudoコマンドで修正します。

sudo visudo
%sudo   ALL=(ALL:ALL) NOPASSWD: ALL # 修正箇所