记录下IDEA中调试Tomcat源码的过程
打算动态调试看看Tomcat源码, 发现Tomcat是使用ant构建的, 直接复制源码到IDEA中不太方便,研究了下怎么在IDEA中动态调试Tomcat源码
- 下载 Tomcat源码, 解压到D盘根目录
安装 Ant
- 下载 Ant 二进制包, 解压到D盘根目录
- 配置环境变量
ANT_HOME
=D:\apache-ant-1.10.3
,PATH
中追加;%ANT_HOME%\bin
编译 Tomcat
D: cd D:/apache-tomcat-8.5.29-src ant
IDEA中新建个serlvet+jsp的项目
IndexServlet.java
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/") public class IndexServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().print("Hello World"); } }
EchoServlet.java
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/echo") public class EchoServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().print("echo"); } }
hello.jsp
<%-- Created by IntelliJ IDEA. User: ilanyu Date: 2018/4/9 Time: 10:53 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> hello.jsp </body> </html>
- 生成war包,修改war包名字为ROOT.war
IDEA中新建J2SE项目
com/lanyus/Main.java
package com.lanyus; public class Main { public static void main(String[] args) { org.apache.catalina.startup.Bootstrap.main(args); } }
- 将
D:\apache-tomcat-8.5.29-src\output\build
中bin
、conf
、lib
复制到项目中,将bin
中bootstrap.jar
、commons-daemon.jar
、tomcat-juli.jar
复制到lib
中,在lib
上右键, 选择Add As Library...
- 将
D:\apache-tomcat-8.5.29-src\java
复制到项目中, 将第4步新建的文件复制到java目录中 - 在项目中新建
webapps
,将第4步得到的ROOT.war复制到webapps中 - 单步运行
Main
类,进入org.apache.catalina.startup.Bootstrap.main
后选择源码目录,选择java
目录
最终效果如图
推荐几篇感觉写得还不错的分析Tomcat的文章
1
吖阿斯顿到代收多大V爱的发声如果test