为了方便分析Tomcat源码,利用大家习惯的方式来进行Debug调试,那么如何将Tomcat源码导入到Eclipse呢,这就是本文的重点

1 准备

1.1 获取Tomcat源码

获取tomcat源码有多种方式

1)Git方式从github获取,地址:https://github.com/apache/tomcat 或者 git://git.apache.org/tomcat.git

2)SVN方式从Apache官网获取,地址:http://svn.apache.org/repos/asf/tomcat/

3)直接下载源码,地址:http://tomcat.apache.org/download-[tomcat-version]0.cgi

下载下来之后,首先建议大家先看一下BUILDING.txt,这个文档中有详细的编译说明,包括编译依赖、编译过程。同时阅读build.xml,这个里面会有各种编译参数

1.2 安装配置ant

获取ant,地址:http://ant.apache.org/bindownload.cgi

下载后解压,并配置ANT_HOME及将%ANT_HOME%/bin添加到path中,方式同JDK一样

1.3 安装配置JDK

这个没有什么要说的,很基本的。不过需要注意的是在building.txt中会有当前源码对JDK版本的要求。

2 编译

2.1 将tomcat源码编译为eclipse可导入的项目

在执行之前大家先看两项内容

2.1.1 building.txt中的说明

(3.2) Building

 1. The build is controlled by creating a ${tomcat.source}/build.properties
file. It is recommended to always create the file, because of unfortunate
default value of base.path property. You may start with the following
content for the file: # ----- Default Base Path for Dependent Packages -----
# Replace this path with the directory path where dependencies binaries
# should be downloaded
base.path=/home/me/some-place-to-download-to //重点:需要将build.properties.default重命名为build.properties,并且修改bath.path为源码存放目录 2. Configure base.path property by adding it to the
${tomcat.source}/build.properties file. The base.path property specifies the place where Tomcat dependencies
required by the build are downloaded. It is recommended to place this
directory outside of the source tree, so that you do not waste your
time re-downloading the libraries. * NOTE: The default value of the base.path property configures the build script
to download the libraries required to build Tomcat to the
${user.home}/tomcat-build-libs directory. * NOTE: Users accessing the Internet through a proxy must use the properties
file to indicate to Ant the proxy configuration. The following properties should be added to the ${tomcat.source}/build.properties
file. proxy.use=true
proxy.host=proxy.domain
proxy.port=8080
proxy.user=username
proxy.password=password See Apache Ant documentation for the <setproxy> task for details.

2.1.2 build.xml

<!-- ============================ IDE Support ============================ –>
//可以将源码编译为Eclipse或者NetBeans项目
<!-- ============================ Eclipse ================================ --> <target name="ide-eclipse"
depends="download-compile, extras-webservices-prepare, download-test-compile"
description="Prepares the source tree to be built in Eclipse"> <!-- Copy the sample project files into the root directory -->
<copy file="${tomcat.home}/res/ide-support/eclipse/eclipse.project" tofile="${tomcat.home}/.project"/>
<copy file="${tomcat.home}/res/ide-support/eclipse/eclipse.classpath" tofile="${tomcat.home}/.classpath"/>
<!-- Copy compiler settings file -->
<mkdir dir="${tomcat.home}/.settings" />
<copy file="${tomcat.home}/res/ide-support/eclipse/org.eclipse.jdt.core.prefs.properties" tofile="${tomcat.home}/.settings/org.eclipse.jdt.core.prefs"/>
<echo>Eclipse project files created.
Read the Building page on the Apache Tomcat documentation site for details on how to configure your Eclipse workspace.</echo>
</target> <!-- ============================ NetBeans =============================== --> <target name="ide-netbeans"
depends="-ide-netbeans-protect,-ide-netbeans-create"
description="Create NetBeans project files only if none exist">
</target> <target name="-ide-netbeans-protect"
if="tomcat-nb.home.exists"
depends="-ide-netbeans-check">
<!-- Protect existing NetBeans project files -->
<echo>NetBeans project files already exist and have been protected!
Use the "ide-netbeans-replace" target if you wish to overwrite them.</echo>
</target> <target name="-ide-netbeans-check">
<!-- protect existing files if the NetBeans project directory exists -->
<condition property="tomcat-nb.home.exists">
<available file="${tomcat-nb.home}" type="dir"/>
</condition>
</target> <target name="ide-netbeans-replace" depends="-ide-netbeans-create">
<!-- Bypass protection of any existing NetBeans project files -->
</target> <target name="-ide-netbeans-create"
unless="tomcat-nb.home.exists">
<!-- Unconditionally create or overwrite default NetBeans project files -->
<property name="tomcat-nb.dist" value="${tomcat.home}/res/ide-support/netbeans" />
<mkdir dir="${tomcat-nb.home}/"/>
<copy todir="${tomcat-nb.home}" overwrite="true">
<fileset dir="${tomcat-nb.dist}"/>
</copy>
<echo>NetBeans project files created.
Read the Building page on the Apache Tomcat documentation site for details on how to customise your NetBeans project.</echo>
</target>

2.1.3 编译项目

#执行如下命令,其中[G:\learning\tomcat]是我的tomcat源码存放目录,注意:必须联网编译
G:\learning\tomcat>ant ide-eclipse

编译完成后,就可以将tomcat导入到Eclipse中,如下所示

发现有一些依赖不存在,那么我们就修改依赖

需要增加ANT_HOME和TOMCAT_LIBS_BASE两个变量,入下图所示,

大家在设置变量值得时候一定要搞清楚自己编译时下载JAR包的位置

完成后就可以正常编译了。

2.1.3 运行项目

这个与我们平时运行项目一样,如下图所示:

运行完成后,我们访问:http://localhost:8080,如下图所示

接下来大家就可以以DEBUG模式进行Tomcat调试了。

2.2 将tomcat源码编译为发布版本

#在源码目录中直接执行ant即可
G:\learning\tomcat>ant

完成后会生成G:\learning\tomcat\output\build目录,这个目录里面的内容看起来大家都很熟悉了,这与官网下载的发布版本一模一样。

大家启动G:\learning\tomcat\output\build\bin\startup.bat就可以运行tomcat了。

运行完后访问:http://localhost:8080,如下图所示

参考:

[1]eclipse 导入tomcat7源码

[2]Tomcat 7 源码分析 - 下载 tomcat source code 并导入eclipse

Tomcat启动分析(二)-自己编译Tomcat的更多相关文章

  1. [转]Tomcat启动分析

    [转]Tomcat启动分析 原帖 http://docs.huihoo.com/apache/tomcat/heavyz/01-startup.html 以下摘录了部分 --------------- ...

  2. Tomcat启动分析(转自:http://docs.huihoo.com/apache/tomcat/heavyz/01-startup.html)

    Tomcat启动分析 1 - Tomcat Server的组成部分 1.1 - Server A Server element represents the entire Catalina servl ...

  3. Tomcat启动分析(我们为什么要配置CATALINA_HOME环境变量)

    原文:http://www.cnblogs.com/heshan664754022/archive/2013/03/27/2984357.html Tomcat启动分析(我们为什么要配置CATALIN ...

  4. Tomcat源码分析一:编译Tomcat源码

    Tomcat源码分析一:编译Tomcat源码 1 内容介绍 在之前的<Servlet与Tomcat运行示例>一文中,给大家带来如何在Tomcat中部署Servlet应用的相关步骤,本文将就 ...

  5. Tomcat源码分析 (七)----- Tomcat 启动过程(二)

    在上一篇文章中,我们分析了tomcat的初始化过程,是由Bootstrap反射调用Catalina的load方法完成tomcat的初始化,包括server.xml的解析.实例化各大组件.初始化组件等逻 ...

  6. Tomcat启动分析(一)-从脚本到main函数分析

    当我们在Linux下启动tomcat的时候,通过ps查看其进程信息为,接下来的内容我们就以此进行分析: [tomcat@fdd ~]$ ps -ef |grep java tomcat : tty1 ...

  7. SpringBoot源码解析:tomcat启动分析

    >> spring与tomcat的启动分析:war包形式 tomcat:xml加载规范 1.contex-param: 初始化参数 2.listener-class: contextloa ...

  8. Tomcat架构解析(二)-----Connector、Tomcat启动过程以及Server的创建过程

    Connector用于跟客户端建立连接,获取客户端的Socket,交由Container处理.需要解决的问题有监听.协议以及处理器映射等等. 一.Connector设计   Connector要实现的 ...

  9. maven项目使用tomcat启动报错:Server Tomcat v8.5 Server at localhost failed to start

    背景说明:1)该项目为maven项目,使用的maven的本地仓库里有不少之前使用过下载的jar包: 2)从svn下载该项目后,无报错情况: 3)部署到tomcat启动报错 如下 : 4)在网上搜索了很 ...

随机推荐

  1. Go-获取变量数据类型

    package main import ( "fmt" "reflect" //这个包里的TypeOf方法获取变量数据类型 ) func main(){ b : ...

  2. arcgis api 4.x for js 地图加载多个气泡窗口展示(附源码下载)

    前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 4.x for js:esri 官网 api,里面详细的介绍 arcgis api 4.x 各个类 ...

  3. RDIFramework.NET敏捷开发框架WinForm新增文件中心-实现附件集中管理

    1.引言 文件中心类似附件管理是一个非常实用功能,可以归档自己平时所需要的文件,也可以把文件分享给别人,更像一个知识中心.文件中心主界面如下图所示,左侧"附件分类"展示了用户对文件 ...

  4. 记录NodeJs常用工具

    安装模块 npm install [-g] module express supervisor:改代码自动重启服务 用法>> supervisor xxx.js 搜索模块 npm sear ...

  5. Internet Download Manager是什么?

    在互联网下载管理器(也被称为IDM)是一款共享软件下载管理器,这意味着你可以下载该程序,并尝试它的试用期内免费. 以下是IDM最佳功能的列表: 支持多种浏览器和应用程序 一键下载文件 内置防病毒检查 ...

  6. eNSP仿真软件之利用三层交换机实现VLAN间路由

    1.实验原理: VLAN将一个物理的LAN在逻辑上划分成多个广播域.VLAN内的主机间可以直接通信,而VLAN间不能直接互通. 在现实网络中,经常会遇到需要跨VLAN相互访问的情况,工程师通常会选择一 ...

  7. 反卷积(Transposed Convolution)

    反卷积的具体计算步骤 令图像为 卷积核为 case 1 如果要使输出的尺寸是 5x5,步数 stride=2 ,tensorflow 中的命令为: transpose_conv = tf.nn.con ...

  8. TensorFlow从1到2(十一)变分自动编码器和图片自动生成

    基本概念 "变分自动编码器"(Variational Autoencoders,缩写:VAE)的概念来自Diederik P Kingma和Max Welling的论文<Au ...

  9. JavaScript-----6.流程控制:分支

    1.流程控制 就是来控制代码按照何种顺序来执行,流程控制有三种结构:顺序结构.分支结构.循环结构 2.顺序流程控制 程序按照代码先后顺序依次执行 3.分支流程控制 JS中提供两种分支结构语句:if语句 ...

  10. 改编《OI抄》

    最近经历的事情比较多,网上常常流传着<锦鲤抄>修改版,于是就再修改了修改,就能唱起来了. 算是一种情怀吧. 请欣赏: OI抄 作词:某些dar佬 FYHSSGSS ssdfzhyf 作曲: ...