打算动态调试看看Tomcat源码, 发现Tomcat是使用ant构建的, 直接复制源码到IDEA中不太方便,研究了下怎么在IDEA中动态调试Tomcat源码
安装 Ant
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>
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的文章
https://blog.csdn.net/cx520forever/article/details/52743166
https://www.cnblogs.com/zhouyuqin/p/5143121.html