1.将Tomcat fork到自己仓库并将源码下载到本地

git clone https://github.com/XXX/tomcat.git

2.安装Ant

Tomcat是ant构建的项目,所以需要安装Ant,安装步骤如下:
(1)下载安装包(版本号至少是1.9.10)(注意:官方网站上给出了详细的build步骤,参考链接:https://tomcat.apache.org/tomcat-10.0-doc/building.html)
(2)解压安装包unzip apache-ant-1.10.10-bin.zip
(3)编辑.bash_profile

export ANT_HOME=/Users/sunxi/Documents/application/apache-ant-1.10.10
export PATH=$ANT_HOME/bin:$PATH

(4)source .bash_profile
(5)验证ant安装

sunxi@sunxideMacBook-Pro ~ % ant -version
Apache Ant(TM) version 1.10.10 compiled on April 12 2021

3.编译Tomcat源码

(1)拷贝一份build.properties.default文件并将其重命名为build.properties,修改该文件中的base.path(该路径是用来存放构建Tomcat时下载的依赖包,这里是绝对路径)

base.path=/xxx/application/tomcat-build-libs

(2)将build.xml文件中以下内容注释掉,注释的原因是:因为其会在下载依赖时去获取一个nsis.exe的文件,用来生成tomcat在windows平台的安装程序的。我们这里不需要,所以可以在ant的构建文件build.xml里把它去掉。(这里笔者在分别在注释前后都有测试,在笔者环境中没有任何问题,这里笔者给出一个参考链接:https://mp.weixin.qq.com/s/iK12Qhj8vcfLLFLPCHwIrg)

<antcall target="downloadzip">
	<param name="sourcefile" value="${nsis.loc}"/>
	<param name="destfile" value="${nsis.exe}"/>
	<param name="destdir" value="${nsis.home}/.."/>
	<param name="checksum.enabled" value="${nsis.checksum.enabled}"/>
	<param name="checksum.algorithm" value="${nsis.checksum.algorithm}"/>
	<param name="checksum.value" value="${nsis.checksum.value}"/>
</antcall>

(3)在源码的根目录下执行ant命令编译,命令执行成功后会在屏幕中显示BUILD SUCCESSFUL
笔者在编译过程中出现以下报错,参照链接https://blog.csdn.net/u011109679/article/details/117674702进行了修复,具体
报错信息及解决方法如下:

  • 报错信息
BUILD FAILED
/Users/sunxi/Documents/gitcode/tomcat/build.xml:1040: The following error occurred while executing this line:
/Users/sunxi/Documents/gitcode/tomcat/build.xml:3805: Failed to parse date string 06/15/2021 03:04:19 下午.
  • 解决方法
将源码根目录下的build.xml文件中的<format property="tstamp.file" pattern="MM/dd/yyyy hh:mm:ss aa"/>修改为
<format property="tstamp.file" pattern="123"/>(pattern修改为任意字符串即可);
将源码根目录下modules/jdbc-pool/build.xml文件中的<format property="tstamp.file" pattern="MM/dd/yyyy hh:mm:ss aa"/>修改为
<format property="tstamp.file" pattern="123"/>

4.将代码导入IDEA

(1)在源码的根目录下执行以下命令

ant ide-intellij

执行完该命令后界面会如下所示:

sunxi@bogon tomcat % ant ide-intellij
Buildfile: /Users/sunxi/Documents/gitcode/tomcat/build.xml

download-bnd:

testexist:
     [echo] Testing  for /Users/sunxi/Documents/application/tomcat-build-libs/bnd-5.3.0/biz.aQute.bnd-5.3.0.jar

setproxy:

downloadfile:

testexist:
     [echo] Testing  for /Users/sunxi/Documents/application/tomcat-build-libs/osgi-annotations-1.0.0/org.osgi.annotation.bundle-1.0.0.jar

setproxy:

downloadfile:

setup-bnd:

download-compile:

testexist:
     [echo] Testing  for /Users/sunxi/Documents/application/tomcat-build-libs/commons-daemon-1.2.4/commons-daemon-1.2.4.jar

downloadgz-2:

testexist:
     [echo] Testing  for /Users/sunxi/Documents/application/tomcat-build-libs/ecj-4.18/ecj-4.18.jar

downloadfile-2:

testexist:
     [echo] Testing  for /Users/sunxi/Documents/application/tomcat-build-libs/jaxrpc-1.1-rc4/geronimo-spec-jaxrpc-1.1-rc4.jar

setproxy:

downloadfile:

testexist:
     [echo] Testing  for /Users/sunxi/Documents/application/tomcat-build-libs/wsdl4j-1.6.3/wsdl4j-1.6.3.jar

setproxy:

downloadfile:

testexist:
     [echo] Testing  for /Users/sunxi/Documents/application/tomcat-build-libs/migration-1.0.0/jakartaee-migration-1.0.0-shaded.jar

setproxy:

downloadfile:

download-test-compile:

testexist:
     [echo] Testing  for /Users/sunxi/Documents/application/tomcat-build-libs/junit-4.13/junit-4.13.jar

setproxy:

downloadfile:
      [get] Getting: https://repo.maven.apache.org/maven2/junit/junit/4.13/junit-4.13.jar
      [get] To: /Users/sunxi/Documents/application/tomcat-build-libs/download-1854922165.tmp
     [echo] Checksum check for junit-4.13.jar, algorithm MD5|SHA-1: OK
    [mkdir] Created dir: /Users/sunxi/Documents/application/tomcat-build-libs/junit-4.13
     [move] Moving 1 file to /Users/sunxi/Documents/application/tomcat-build-libs/junit-4.13

testexist:
     [echo] Testing  for /Users/sunxi/Documents/application/tomcat-build-libs/hamcrest-2.2/hamcrest-2.2.jar

setproxy:

downloadfile:
      [get] Getting: https://repo.maven.apache.org/maven2/org/hamcrest/hamcrest/2.2/hamcrest-2.2.jar
      [get] To: /Users/sunxi/Documents/application/tomcat-build-libs/download-220608314.tmp
     [echo] Checksum check for hamcrest-2.2.jar, algorithm SHA-512: OK
    [mkdir] Created dir: /Users/sunxi/Documents/application/tomcat-build-libs/hamcrest-2.2
     [move] Moving 1 file to /Users/sunxi/Documents/application/tomcat-build-libs/hamcrest-2.2

testexist:
     [echo] Testing  for /Users/sunxi/Documents/application/tomcat-build-libs/easymock-4.2/easymock-4.2.jar

setproxy:

downloadfile:
      [get] Getting: https://repo.maven.apache.org/maven2/org/easymock/easymock/4.2/easymock-4.2.jar
      [get] To: /Users/sunxi/Documents/application/tomcat-build-libs/download-989123826.tmp
     [echo] Checksum check for easymock-4.2.jar, algorithm SHA-512: OK
    [mkdir] Created dir: /Users/sunxi/Documents/application/tomcat-build-libs/easymock-4.2
     [move] Moving 1 file to /Users/sunxi/Documents/application/tomcat-build-libs/easymock-4.2

testexist:
     [echo] Testing  for /Users/sunxi/Documents/application/tomcat-build-libs/cglib-3.3.0/cglib-nodep-3.3.0.jar

setproxy:

downloadfile:
      [get] Getting: https://repo.maven.apache.org/maven2/cglib/cglib-nodep/3.3.0/cglib-nodep-3.3.0.jar
      [get] To: /Users/sunxi/Documents/application/tomcat-build-libs/download-1431192676.tmp
     [echo] Checksum check for cglib-nodep-3.3.0.jar, algorithm SHA-512: OK
    [mkdir] Created dir: /Users/sunxi/Documents/application/tomcat-build-libs/cglib-3.3.0
     [move] Moving 1 file to /Users/sunxi/Documents/application/tomcat-build-libs/cglib-3.3.0

testexist:
     [echo] Testing  for /Users/sunxi/Documents/application/tomcat-build-libs/objenesis-3.1/objenesis-3.1.jar

setproxy:

downloadfile:
      [get] Getting: https://repo.maven.apache.org/maven2/org/objenesis/objenesis/3.1/objenesis-3.1.jar
      [get] To: /Users/sunxi/Documents/application/tomcat-build-libs/download-945284156.tmp
     [echo] Checksum check for objenesis-3.1.jar, algorithm SHA-512: OK
    [mkdir] Created dir: /Users/sunxi/Documents/application/tomcat-build-libs/objenesis-3.1
     [move] Moving 1 file to /Users/sunxi/Documents/application/tomcat-build-libs/objenesis-3.1

testexist:
     [echo] Testing  for /Users/sunxi/Documents/application/tomcat-build-libs/unboundid-5.1.4/unboundid-ldapsdk-5.1.4.jar

setproxy:

downloadfile:
      [get] Getting: https://repo.maven.apache.org/maven2/com/unboundid/unboundid-ldapsdk/5.1.4/unboundid-ldapsdk-5.1.4.jar
      [get] To: /Users/sunxi/Documents/application/tomcat-build-libs/download-1631250359.tmp
     [echo] Checksum check for unboundid-ldapsdk-5.1.4.jar, algorithm SHA-512: OK
    [mkdir] Created dir: /Users/sunxi/Documents/application/tomcat-build-libs/unboundid-5.1.4
     [move] Moving 1 file to /Users/sunxi/Documents/application/tomcat-build-libs/unboundid-5.1.4

ide-intellij:
     [copy] Copying 10 files to /Users/sunxi/Documents/gitcode/tomcat/.idea
     [echo] IntelliJ IDEA project directory created. Please create PATH VARIABLES for
     [echo] 
     [echo]       ANT_HOME          = /Users/sunxi/Documents/application/apache-ant-1.10.10
     [echo]       TOMCAT_BUILD_LIBS = /Users/sunxi/Documents/application/tomcat-build-libs
     [echo]     

BUILD SUCCESSFUL
Total time: 1 minute 1 second

(2)将代码导入IDEA并在IDEA中配置好ANT_HOME和TOMCAT_BUILD_LIBS,如下图所示:
在这里插入图片描述
(3)在本地创建catalina-home目录并将编译成功后的源码的output目录下的build目录中的所有文件拷贝至catalina-home

(4)在IDEA中配置Tomcat的入口、VM参数,具体如下:

配置项value
Main classorg.apache.catalina.startup.Bootstrap
VM options-Dcatalina.home=/Users/sunxi/Documents/application/catalina-home -Duser.language=en -Duser.region=US -Dfile.encoding=UTF-8

在这里插入图片描述

5.在IDEA中启动Tomcat

启动方法是运行org.apache.catalina.startup.Bootstrap的main方法,启动完成后打开浏览器输入127.0.0.1:8080后页面显示如下:
在这里插入图片描述

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐