Skip to content

Commit 7628bef

Browse files
Add sample for tomcat debugging (#698)
* Add sample for tomcat debugging Signed-off-by: Jinbo Wang <[email protected]> * Use paragraph to format attach samples Signed-off-by: Jinbo Wang <[email protected]> * Fix json indentation Signed-off-by: Jinbo Wang <[email protected]> * Fix typos Signed-off-by: Jinbo Wang <[email protected]>
1 parent 571e26f commit 7628bef

File tree

1 file changed

+95
-15
lines changed

1 file changed

+95
-15
lines changed

Configuration.md

Lines changed: 95 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,16 @@ Before attaching to a debuggee, your debuggee program must be started with debug
126126
}
127127
```
128128

129-
In some cases, you may want to start your program with the external builder and launcher, then you can configure these jobs in [tasks.json](https://code.visualstudio.com/docs/editor/tasks) and then attach to it. For example, launching springboot application via mvn command, and then attach a debugger.
130-
- Attaching to mvn task
131-
- Configure your command in tasks.json - The debug job is background task, you should use *problemMatcher* filter to tell VSCode it's ready.
132-
```json
129+
In some cases, you may want to start your program with the external builder and launcher, then you can configure these jobs in [tasks.json](https://code.visualstudio.com/docs/editor/tasks) and attach to it. For example, launching springboot application via mvn command, and then attach a debugger.
130+
#### Attach to mvn task
131+
1) Configure your command in .vscode/tasks.json - The mvn task is a background task, you should use *problemMatcher* filter to tell VS Code it's ready.
132+
```json
133133
{
134134
"label": "mvnDebug",
135135
"type": "shell",
136136
"command": "mvn spring-boot:run -Dspring-boot.run.jvmArguments=\"-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005\"",
137137
"isBackground": true,
138-
"problemMatcher": [{
138+
"problemMatcher": [{
139139
"pattern": [{
140140
"regexp": "\\b\\B",
141141
"file": 1,
@@ -150,7 +150,7 @@ In some cases, you may want to start your program with the external builder and
150150
}]
151151
}
152152
```
153-
- Launch the task before launch and attach to the debug port - You need configure the task label to `preLaunchTask` in launch.json.
153+
2) Configure `preLaunchTask` and the debug port in .vscode/launch.json.
154154
```json
155155
{
156156
"type": "java",
@@ -161,11 +161,90 @@ In some cases, you may want to start your program with the external builder and
161161
"preLaunchTask": "mvnDebug"
162162
}
163163
```
164-
![attachToMvn](https://user-images.githubusercontent.com/14052197/67262705-4f2d6880-f4d8-11e9-9e2d-9c35a6613c08.gif)
164+
3) <b>F5</b> will launch the mvn task, and attach the debugger. See the demo.
165+
![attachToMvn](https://user-images.githubusercontent.com/14052197/67262705-4f2d6880-f4d8-11e9-9e2d-9c35a6613c08.gif)
165166

166-
- Use javac as the builder and attach to java process
167-
tasks.json sample:
167+
#### Attach to embedded maven tomcat server
168+
- pom.xml sample for embedded tomcat server.
169+
```xml
170+
...
171+
<plugin>
172+
<groupId>org.apache.tomcat.maven</groupId>
173+
<artifactId>tomcat7-maven-plugin</artifactId>
174+
<version>2.2</version>
175+
</plugin>
176+
...
177+
```
178+
- The steps to attach to the embedded maven tomcat server.
179+
1) Use .vscode/tasks.json to configure *run-tomcat* and *stop-tomcat* tasks.
168180
```json
181+
{
182+
"version": "2.0.0",
183+
"tasks": [
184+
{
185+
"label": "run-tomcat",
186+
"type": "shell",
187+
"command": "MAVEN_OPTS=\"$MAVEN_OPTS -agentlib:jdwp=transport=dt_socket,address=5005,server=y,suspend=n\" ./mvnw tomcat7:run",
188+
"group": "build",
189+
"isBackground": true,
190+
"problemMatcher": [{
191+
"pattern": [{
192+
"regexp": "\\b\\B",
193+
"file": 1,
194+
"location": 2,
195+
"message": 3
196+
}],
197+
"background": {
198+
"activeOnStart": true,
199+
"beginsPattern": "^.*Listening for",
200+
"endsPattern": "^.*transport dt_socket at address.*"
201+
}
202+
}]
203+
},
204+
{
205+
"label": "stop-tomcat",
206+
"type": "shell",
207+
"command": "echo ${input:terminate}}",
208+
"problemMatcher": []
209+
}
210+
],
211+
"inputs": [
212+
{
213+
"id": "terminate",
214+
"type": "command",
215+
"command": "workbench.action.tasks.terminate",
216+
"args": "run-tomcat"
217+
}
218+
]
219+
}
220+
```
221+
2) Use .vscode/launch.json to configure the attach configuration. Use `preLaunchTask` to run tomcat before the attach, and `postDebugTask` to stop tomcat after the debug ends.
222+
```json
223+
{
224+
"version": "0.2.0",
225+
"configurations": [
226+
{
227+
"type": "java",
228+
"name": "Debug (Attach)",
229+
"request": "attach",
230+
"hostName": "localhost",
231+
"port": 5005,
232+
"preLaunchTask": "run-tomcat",
233+
"postDebugTask": "stop-tomcat"
234+
}
235+
]
236+
}
237+
```
238+
3) <b>F5</b> will auto start the tomcat server and attach the debugger. The demo below will show how to debug spring mvc in tomcat.
239+
![attachToEmbeddedTomcat](https://user-images.githubusercontent.com/14052197/67541153-80957680-f71a-11e9-9d59-e9aaa752fe33.gif)
240+
241+
> If you want to try to debug your Java webapps in a standalone tomcat server, please try VS Code [Tomcat for Java](https://marketplace.visualstudio.com/items?itemName=adashen.vscode-tomcat) extension.
242+
243+
> If you want to try to debug embedded tomcat server with gradle plugin, see the [gradle sample](https://github.com/microsoft/vscode-java-debug/issues/140#issuecomment-343656398).
244+
245+
#### Use javac as the builder and attach to java process
246+
1) Configure the javac builder and java runner jobs in .vscode/tasks.json.
247+
```json
169248
{
170249
"version": "2.0.0",
171250
"tasks": [
@@ -180,7 +259,7 @@ In some cases, you may want to start your program with the external builder and
180259
"type": "shell",
181260
"command": "java -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005 -cp bin app.SimpleCalc",
182261
"isBackground": true,
183-
"problemMatcher": [{
262+
"problemMatcher": [{
184263
"pattern": [{
185264
"regexp": "\\b\\B",
186265
"file": 1,
@@ -196,9 +275,9 @@ In some cases, you may want to start your program with the external builder and
196275
}
197276
]
198277
}
199-
```
200-
launch.json sample:
201-
```json
278+
```
279+
2) Configure `preLaunchTask` and the debug port in .vscode/launch.json.
280+
```json
202281
{
203282
"version": "0.2.0",
204283
"configurations": [
@@ -212,8 +291,9 @@ In some cases, you may want to start your program with the external builder and
212291
}
213292
]
214293
}
215-
```
216-
![attachToJava](https://user-images.githubusercontent.com/14052197/67263956-3cb52e00-f4dc-11e9-9c78-6e66cb3d7c2b.gif)
294+
```
295+
3) <b>F5</b> will run the tasks and attach the debugger. See the demo.
296+
![attachToJava](https://user-images.githubusercontent.com/14052197/67263956-3cb52e00-f4dc-11e9-9c78-6e66cb3d7c2b.gif)
217297

218298
## Modify the settings.json (User Setting)
219299
- `java.debug.settings.console` - The specified console to launch Java program, defaults to `integratedTerminal`. If you want to customize the console for a specific debug session, please use `console` option in launch.json instead.

0 commit comments

Comments
 (0)