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

Tomcat7源码学习指南
本文详细介绍了如何下载Tomcat7源代码并通过Eclipse进行配置和运行的过程。包括使用svn下载源码、通过ant创建Eclipse项目、解决编译错误、运行及调试程序等步骤。同时提供了配置详细日志的方法。
准备好好研究学习一下tomcat 7 的源代码,那么第一步就是下载,导入eclipse并且运行。 
1. 下载源代码 
tomcat 7 的源代码在svn上,所以你需要svn的安装。创建一个目录tomcat7,并用命令行进入这个目录,之后用下面的命令下载 
Java代码  收藏代码
  1. svn co http://svn.apache.org/repos/asf/tomcat/tc7.0.x/tags/TOMCAT_7_0_27/ ./  

这里我下载的是tag里面的,你也可以下载别的branch,比如trunk,但是我觉得tag的是比较稳定的。 

2. 创建eclipse项目 
tomcat 7 是用ant来构建和管理的,所以你需要安装ant。在tomcat7目录下执行: 
Java代码  收藏代码
  1. ant ide-eclipse  


其实,你看一下build.xml就知道它就是copy了两个现成的文件 

Java代码  收藏代码
  1. <target name="ide-eclipse" depends="deploy, extras-webservices-prepare"  
  2.           description="Prepares the source tree to be built in Eclipse">  
  3.   
  4.     <!-- Copy the sample project files into the root directory -->  
  5.     <copy file="${tomcat.home}/res/ide-support/eclipse/eclipse.project" tofile="${tomcat.home}/.project"/>  
  6.     <copy file="${tomcat.home}/res/ide-support/eclipse/eclipse.classpath" tofile="${tomcat.home}/.classpath"/>  
  7.   
  8.     <echo>Eclipse project files created.  
  9. Read the Building page on the Apache Tomcat documentation site for details on how to configure your Eclipse workplace.</echo>  
  10.   </target>  


这样你就可以在eclipse里面导入这个tomcat 项目了 
3. 在eclipse里面添加lib变量 
导入项目以后你会发现有几个错误导致不能编译,其实是缺少lib变量: 
ANT_HOME, 指向ant的安装目录 
TOMCAT_LIBS_BASE,指向依赖库的位置,如果你还没有在tomcat7下执行: 
Java代码  收藏代码
  1. ant  

来build和deploy这个项目,就先执行它。 
这样它就会下载需要的lib。
如果你不确定放到哪里了,你可以搜索ecj-3.7.2,把它的上级目录指定给这个变量就好了。 
添加好之后,eclipse会提示你重新build项目,当然要。之后就会没有错误了。 

4. 运行 
右击这个tomcat-7.0.x项目,选择run as / Java Application,它会自动搜索项目中的所有main函数,你选择org.apache.catalina.startup.Bootstrap的main函数就是。 选择好了之后,你也许会看到有tomcat-start和tomcat-stop两个启动项让你选择,其实这个是tomcat为你准备好的launch文件,在tomcat7\res\ide-support\eclipse下面有两个launch文件。也就是说你直接可以在run configuration里面找到这两个启动配置,从而加以利用。在run configuration的配置里面的Common tab里面你还可以设置选中debug,从而你可以在debug菜单下显示它了。 
问题:为什么这个函数的注释写仅仅为了测试: 
Java代码  收藏代码
  1. /** 
  2.     * Main method, used for testing only. 
  3.     * 
  4.     * @param args Command line arguments to be processed 
  5.     */  
  6.    public static void main(String args[]) {  

5. 启动好以后你可以打开下面的网址: 
Java代码  收藏代码
  1. http://localhost:8080/  


你会看到欢迎页面,大功告成!你可以在main里面设置断点debug了。 

6. 配置更为详细的log
 
看更为详细的log有利于我们准确地跟踪tomcat的执行过程。tomcat的日志框架允许我们配置第三方的log系统实现,比如log4j。默认情况下它使用java.util.logging. 
需要两步来实现更为详细的log的配置 
1)运行参数,加上 
Java代码  收藏代码
  1. -Djava.util.logging.config.file=${project_loc:/tomcat-7.0.x}/conf/logging.properties  

2)修改log level的配置,把FINE,改成FINEST。并加入: 
Java代码  收藏代码
  1. org.apache.catalina.level = FINEST  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值