Tomcat源码学习(二) 编译源码

4 篇文章 0 订阅
4 篇文章 0 订阅

编译一个Tomcat

最近在学习Tomcat的源码,学习的过程中产生了一个自己动手编译Tomcat 源码的念头。

说干就干。其实编译的步骤其实在Tomcat的官网上已经有了;

地址:https://tomcat.apache.org/tomcat-8.5-doc/building.html

因为我之前学习的是8.5.64 版本这次我们就用这个版本的源码来编译;然后我自己构建的过程中也是借鉴了官方文档,当然也不是一帆风顺。下面我们开始吧

一. 构建前的准备

1. JDK

构建 Apache Tomcat 需要安装 JDK(版本 7最好是以上版本 我这里使用的是JDK8) 安装和设置环境变量,这没什么好说的

C:\Users\sff>java -version
java version "1.8.0_291"
Java(TM) SE Runtime Environment (build 1.8.0_291-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.291-b10, mixed mode)

2.安装 Apache Ant 1.9.10 或更高版本

创建一个 ANT_HOME 环境变量以指向目录${ant.home},并修改 PATH 环境变量以将目录包含 ${ant.home}/bin在其列表中。这使得ant命令行脚本可用,它将用于实际执行构建

C:\Users\sff>ant -v
Apache Ant(TM) version 1.10.10 compiled on April 12 2021
Trying the default build file: build.xml
Buildfile: build.xml does not exist!
Build failed

3.下载Tomcat 源码

我这里下载是直接在GitHub上拉取得:https://github.com/apache/tomcat.git

然后使用git tag 切换到我们使用的版本 ,然后再创建一个分支 便于后期我们把自己的源码加入进来

sff@DESKTOP-9K2HFQ3 MINGW64 /e/Desktop/学习/4-tomcat/tomcat (main)
$ git checkout  8.5.64
Updating files: 100% (3076/3076), done.

sff@DESKTOP-9K2HFQ3 MINGW64 /e/Desktop/学习/4-tomcat/tomcat ((8.5.64))
$ git checkout -b iTomcat
Switched to a new branch 'iTomcat'

拷贝文件 build.properties.default 到build.properties

修改配置

# ----- Source control flags -----
#注释掉代码版本  不然他会自己切换到主分支
#git.branch=main
 
# ----- Default Base Path for Dependent Packages -----
# Please note this path must be absolute, not relative,
# as it is referenced with different working directory
# contexts by the various build scripts.
##tomcat 依赖jar
base.path=E:\Desktop\学习\4-tomcat\build

修改build.xml 文件

修改所有的ISO-8859-1 变成UTF-8 因为我们自己的代码里面有中文的注释 修改后防止乱码;

二.开始编译

直接在命令栏输入 ant

examples-sources:
 [txt2html] Converted 1 file to E:\Desktop\学习\4-tomcat\tomcat\output\build\webapps\examples\jsp\jsp2\simpletag
 [txt2html] Converted 4 files to E:\Desktop\学习\4-tomcat\tomcat\output\build\webapps\examples\jsp\jsp2\simpletag
 [txt2html] Converted 5 files to E:\Desktop\学习\4-tomcat\tomcat\output\build\webapps\examples\jsp\jsp2\simpletag
 [txt2html] Converted 1 file to E:\Desktop\学习\4-tomcat\tomcat\output\build\webapps\examples\jsp\jsp2\jspattribute
 [txt2html] Converted 4 files to E:\Desktop\学习\4-tomcat\tomcat\output\build\webapps\examples\jsp\jsp2\jspattribute
 [txt2html] Converted 4 files to E:\Desktop\学习\4-tomcat\tomcat\output\build\webapps\examples\jsp\cal
 [txt2html] Converted 1 file to E:\Desktop\学习\4-tomcat\tomcat\output\build\webapps\examples\jsp\jsptoserv
 [txt2html] Converted 1 file to E:\Desktop\学习\4-tomcat\tomcat\output\build\webapps\examples\jsp\jsp2\el
 [txt2html] Converted 2 files to E:\Desktop\学习\4-tomcat\tomcat\output\build\webapps\examples\jsp\jsp2\el
 [txt2html] Converted 3 files to E:\Desktop\学习\4-tomcat\tomcat\output\build\webapps\examples\jsp\jsp2\el
 [txt2html] Converted 1 file to E:\Desktop\学习\4-tomcat\tomcat\output\build\webapps\examples\jsp\jsp2\misc
 [txt2html] Converted 3 files to E:\Desktop\学习\4-tomcat\tomcat\output\build\webapps\examples\jsp\jsp2\tagfiles
 [txt2html] Converted 47 files to E:\Desktop\学习\4-tomcat\tomcat\output\build\webapps\examples

BUILD SUCCESSFUL
Total time: 14 seconds

验证在打包输出的output/build 里面会有完整的tomcat 点击bin/startup.bat 运行成功 即编译成功

三. 修改源码编译

把原有的java 代码删除,copy 自己的代码过来 继续编译 编译成功后运行

Connected to the target VM, address: '127.0.0.1:58920', transport: 'socket'
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/sheff/util/LogOutUtil
	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:470)
Caused by: java.lang.ClassNotFoundException: org.apache.sheff.util.LogOutUtil
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
	... 1 more
Disconnected from the target VM, address: '127.0.0.1:58920', transport: 'socket'
Disconnected from server

这个是我重新修改源码后的类,想到应该没有添加到代码里面去 修改build.xml

 <patternset id="files.bootstrap">
    <include name="org/apache/catalina/startup/Bootstrap.*" />
    <include name="org/apache/catalina/startup/catalina.properties" />
    <include name="org/apache/catalina/startup/CatalinaProperties.*" />
    <include name="org/apache/catalina/startup/ClassLoaderFactory.*" />
    <include name="org/apache/catalina/startup/ClassLoaderFactory$*.*" />
    <include name="org/apache/catalina/startup/Constants.*" />
 ## add
	<exclude name="org/apache/sheff/util/**" />
    <include name="org/apache/catalina/startup/SafeForkJoinWorkerThreadFactory.*" />
    <include name="org/apache/catalina/startup/SafeForkJoinWorkerThreadFactory$*.*" />
    <include name="org/apache/catalina/startup/Tool.*" />
    <include name="org/apache/catalina/security/SecurityClassLoad.*" />
    <include name="org/apache/catalina/webresources/war/**" />
    <include name="org/apache/tomcat/util/buf/UriUtil.*" />
  </patternset>

<patternset id="files.catalina">
    <include name="org/apache/catalina/**" />
    <include name="org/apache/naming/**" />
    <!-- Modules -->
    <exclude name="org/apache/catalina/ant/**" />
    ## add
	<exclude name="org/apache/sheff/util/**" />
    <exclude name="org/apache/catalina/ha/**" />
    <exclude name="org/apache/catalina/mbeans/JmxRemote*" />
    <exclude name="org/apache/catalina/tribes/**" />
    <exclude name="org/apache/catalina/storeconfig/**" />
    <exclude name="org/apache/naming/factory/webservices/**" />
  </patternset>

至此 编译源码成功 下面是我的gitd地址 有兴趣的可以拉下来跑一跑;

https://gitee.com/whsff/i-tomcat-v2.git

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值