idea导入tomcat8源码搭建源码调试环境

海哥  金牌会员 | 2023-2-28 10:39:28 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 534|帖子 534|积分 1602

从apache tomcat下载tomcat8源码

1.下载到源码后,tomcat默认使用ant作为包管理工具,本地调试时创建pom.xml, 手动创建一个pom.xml放入源码根目录
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"   
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">   
  5.    
  6.     <modelVersion>4.0.0</modelVersion>   
  7.     <groupId>org.apache.tomcat</groupId>   
  8.     <artifactId>Tomcat8.0</artifactId>   
  9.     <name>Tomcat8.0</name>   
  10.     <version>8.0</version>   
  11.    
  12.     <build>   
  13.         <finalName>Tomcat8.0</finalName>   
  14.         <sourceDirectory>java</sourceDirectory>   
  15.         <testSourceDirectory>test</testSourceDirectory>   
  16.         <resources>   
  17.             <resource>   
  18.                 <directory>java</directory>   
  19.             </resource>   
  20.         </resources>   
  21.         <testResources>   
  22.             <testResource>   
  23.                 <directory>test</directory>   
  24.             </testResource>   
  25.         </testResources>   
  26.         <plugins>   
  27.             <plugin>   
  28.                 <groupId>org.apache.maven.plugins</groupId>   
  29.                 <artifactId>maven-compiler-plugin</artifactId>   
  30.                 <version>2.0.2</version>   
  31.    
  32.                 <configuration>   
  33.                     <encoding>UTF-8</encoding>   
  34.                     <source>1.8</source>   
  35.                     <target>1.8</target>   
  36.                 </configuration>   
  37.             </plugin>   
  38.         </plugins>   
  39.     </build>
  40.     <dependencies>
  41.       <dependency>
  42.        <groupId>org.easymock</groupId>
  43.        <artifactId>easymock</artifactId>
  44.        <version>5.0.0</version>
  45.        <scope>test</scope>
  46.       </dependency>
  47.     <dependency>
  48.       <groupId>com.unboundid</groupId>
  49.       <artifactId>unboundid-ldapsdk</artifactId>
  50.       <version>6.0.4</version>
  51.       <scope>test</scope>
  52.     </dependency>
  53.         <dependency>
  54.             <groupId>ant</groupId>
  55.             <artifactId>ant</artifactId>
  56.             <version>1.7.0</version>
  57.         </dependency>
  58.         <dependency>
  59.             <groupId>ant</groupId>
  60.             <artifactId>ant-apache-log4j</artifactId>
  61.             <version>1.6.5</version>
  62.         </dependency>
  63.         <dependency>
  64.             <groupId>ant</groupId>
  65.             <artifactId>ant-commons-logging</artifactId>
  66.             <version>1.6.5</version>
  67.         </dependency>
  68.         <dependency>
  69.             <groupId>wsdl4j</groupId>
  70.             <artifactId>wsdl4j</artifactId>
  71.             <version>1.6.2</version>
  72.         </dependency>
  73.         <dependency>
  74.             <groupId>javax.xml.rpc</groupId>
  75.             <artifactId>javax.xml.rpc-api</artifactId>
  76.             <version>1.1</version>
  77.         </dependency>
  78.         <dependency>
  79.             <groupId>org.eclipse.jdt.core.compiler</groupId>
  80.             <artifactId>ecj</artifactId>
  81.             <version>4.6.1</version>
  82.         </dependency>
  83.         <dependency>
  84.             <groupId>junit</groupId>
  85.             <artifactId>junit</artifactId>
  86.             <version>4.12</version>
  87.             <scope>test</scope>
  88.         </dependency>
  89.     </dependencies>
  90. </project>
复制代码
2.使用idea导入tomcat源码 File->Project from Existing Sources -> Maven import
重新导入依赖后TestCookieFilter会报错,本地注释掉单元测试类即可。
3.编辑配置文件,  Edit Configuration, 如下图。
设置catalina.home和tomcat启动类Bootstrap, jdk我选择的是1.8

4.Maven Reload, 把依赖的包重新导入进来。

添加JSP解析器代码:

1.Servlet.service() for servlet [jsp] in context with path [] threw exception [org.apache.jasper.JasperException: Unable to compile class for JSP] with root cause
解决方案:编辑 org.apache.catalina.startup.ContextConfig 文件的 configureStart() 方法,添加初始化 JSP 解析器的代码:
在ContextConfig类的configStart方法中加入以下代码
  1. context.addServletContainerInitializer(new JasperInitializer(), null);
复制代码

我本地下载的tomcat8源码, 以上工作准备完后,直接编译,单元测试也没报错。运行起来,就可以看到猫咪了, 可以开心的看源码了。

我本地的源码config/server.xml里配置的是8080端口, 这里就跑起来了。

tomcat源码目录结构


tomcat核心源码


CATALINA_HOME和CATALINA_BASE

开始阅读tomcat源码之前,先浏览一遍tomcat官方写的介绍,里面内容很全,也是我们学习的第一手资料,这里涉及到两个变量CATALINA_HOME和CATALINA_BASE。

  • CATALINA_HOME:表示 Tomcat 安装的根目录,即D:\Gitee\tomcat8\apache-tomcat-8.5.85-src
  • CATALINA_BASE:表示特定 Tomcat 实例的运行时配置的根。如果你需要在一台机器上配置多个tomcat实例,则需要用到catalina_base变量
为什么使用CATALINA_BASE

默认情况下,CATALINA_HOME和CATALINA_BASE指向同一目录。当您需要在一台计算机上运行多个 Tomcat 实例时,手动设置CATALINA_BASE。这样做具有以下优点:

  • 更轻松地管理升级到较新版本的 Tomcat。由于具有单个CATALINA_HOME位置的所有实例共享一组文件和二进制文件,因此您可以轻松地将文件升级到较新版本,并将更改传播到使用同一CATALIA_HOME目录的所有 Tomcat 实例。
  • 避免重复相同的bin、lib文件夹的文件。
  • 共享某些设置的可能性,例如 shell 或 bat 脚本文件(取决于您的操作系统)。
参考资料

https://2i3i.com/tomcat-code-7.html

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

海哥

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表