Tomcat源码浅析(一)

Tomcat源码目录:

  • Tomcat源码浅析(一):下载源码,导入eclipse并且运行
    Tomcat源码浅析(二):Tomcat原理
    Tomcat源码浅析(三):自己动手写一个Tomcat
    Tomcat源码浅析(四):总结

     

                                 Tomcat源码浅析(一)

 目录:   

 

 前言

            Tomcat源码下载后并不能直接运行,因为缺少jar,我们需要使用Ant去编译,构建成一个Eclipse项目后,开始Tomcat源码学习。

    

 

1.版本

工具版本下载Url
apache-tomcat7.0.90http://archive.apache.org/dist/tomcat/
apache-ant1.9.13https://ant.apache.org/bindownload.cgi
eclipse4.4.2(Luna)https://www.eclipse.org/downloads/packages/release/luna/sr2/eclipse-ide-java-ee-developers
Java SE1.7https://www.oracle.com/technetwork/cn/java/javase/downloads/java-se-jdk-7-download-432154-zhs.html
 1.6https://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase6-419409.html

 

 

 

 

 

 

 

 

2.tomcat源码下载

                          如图:

                                                                                                   图2-1

                                                                                                   图2-2

                          下载源码后解压到目录:F:\TestCode\workspace\apache-tomcat-7.0.90-src

 

 

3.ANT安装

    a.ant下载

            如图:

                                                                                        图3-a-1

 

             下载后将其解压到你的硬盘上,例如:D:\developmentTools\apache-ant-1.9.13。

             如图:

                                                                                              图3-a-2

 

   b.ant环境配置

          window中设置ant环境变量:

ANT_HOME    D:\developmentTools\apache-ant-1.9.13
path             ;%ANT_HOME%\bin;
classpath      ;%ANT_HOME%\lib;

 

 

 

c.ant环境验证

                                如图:

                                                                         图3-c-1

 ok,到这,an环境就配好了,可以开始构建项目了。

 

4.用Ant将Tomcat源码构建成Eclipse项目

         a.打开cmd命令窗口。

         b.进到tomcat目录

(我的目录):F:\TestCode\workspace\apache-tomcat-7.0.90-src

F:\TestCode\workspace\apache-tomcat-7.0.90-src>ant -p
Buildfile: F:\TestCode\workspace\apache-tomcat-7.0.90-src\build.xml

Main targets:

 build-docs                      Builds all documentation from XML sources
 clean                           Delete the default output folders
 clean-depend                    Deletes the dependencies that are built from source
 cobertura-instrument            Adds Cobertura instrumentation to the compiled bytecode
 cobertura-report                Creates report from gathered Cobertura results
 deploy                          Default. Builds a working Tomcat instance
 dist-deployer                   Create the Tomcat deployer binary
 download-cobertura              Download the Cobertura code coverage tool
 download-compile                Download (and build) components necessary to compile
 download-dist                   Download additional components for a distribution
 download-test-compile           Download additional components for the tests
 download-validate               Download components necessary to validate source
 embed                           Creates the experimental embedded release
 embed-extras                    Embedded packaging for those extras that can use it
 embed-jars                      Create experimental embedded jars
 embed-sources                   Create source jars for embedded jars
 examples-sources                Create examples sources
 extras                          Build all extras packages
 extras-commons-logging          Build JULI for log4j extras package
 extras-commons-logging-prepare  Prepare to build web services extras package
 extras-jmx-remote               Build JMX remote extras package
 extras-webservices              Build web services extras package
 extras-webservices-prepare      Prepare to build web services extras package
 ide-eclipse                     Prepares the source tree to be built in Eclipse
 ide-eclipse-websocket           Prepares the source tree to be built in Eclipse - separate project to build classes that require Java 7
 installer                       Create Windows installer
 javadoc                         Create the Tomcat javadoc
 release                         Create a Tomcat 7 packaged distribution
 test                            Runs the JUnit test cases
 test-apr                        Runs the JUnit test cases for APR. Does not stop on errors.
 test-apr-exists                 Checks for APR lib
 test-bio                        Runs the JUnit test cases for BIO. Does not stop on errors.
 test-nio                        Runs the JUnit test cases for NIO. Does not stop on errors.
 validate                        Uses Checkstyle tool to perform style check for the source code
 validate-eoln                   Validate that the source files have correct line ends
Default target: deploy

F:\TestCode\workspace\apache-tomcat-7.0.90-src>

         键入:ant  ide-eclipse  

   如下:

F:\TestCode\workspace\apache-tomcat-7.0.90-src>ant ide-eclipse
Buildfile: F:\TestCode\workspace\apache-tomcat-7.0.90-src\build.xml

download-compile:

proxyflags:

setproxy:

testexist:
     [echo] Testing  for C:\Users\Administrator/tomcat-build-libs/commons-daemon-1.1.0/commons-daemon-1.1.0.jar

downloadgz-2:

proxyflags:

setproxy:

testexist:
     [echo] Testing  for C:\Users\Administrator/tomcat-build-libs/commons-pool-1.5.7-src/build.xml

downloadgz-2:

proxyflags:

setproxy:

testexist:
     [echo] Testing  for C:\Users\Administrator/tomcat-build-libs/commons-dbcp-1.4-src/build.xml

downloadgz-2:

build-prepare:
   [delete] Deleting directory F:\TestCode\workspace\apache-tomcat-7.0.90-src\output\build\temp
    [mkdir] Created dir: F:\TestCode\workspace\apache-tomcat-7.0.90-src\output\build\temp

build-manifests:

build-tomcat-dbcp:

testexist:
     [echo] Testing  for C:\Users\Administrator/tomcat-build-libs/ecj-4.4.2/ecj-4.4.2.jar

downloadfile-2:

extras-prepare:

extras-webservices-prepare:

proxyflags:

setproxy:

testexist:
     [echo] Testing  for C:\Users\Administrator/tomcat-build-libs/jaxrpc-1.1-rc4/geronimo-spec-jaxrpc-1.1-rc4.jar

downloadfile:

proxyflags:

setproxy:

testexist:
     [echo] Testing  for C:\Users\Administrator/tomcat-build-libs/wsdl4j-1.6.2/wsdl4j-1.6.2.jar

downloadfile:

download-test-compile:

proxyflags:

setproxy:

testexist:
     [echo] Testing  for C:\Users\Administrator/tomcat-build-libs/junit-4.11/junit-4.11.jar

downloadfile:

proxyflags:

setproxy:

testexist:
     [echo] Testing  for C:\Users\Administrator/tomcat-build-libs/hamcrest-1.3/hamcrest-core-1.3.jar

downloadfile:

proxyflags:

setproxy:

testexist:
     [echo] Testing  for C:\Users\Administrator/tomcat-build-libs/easymock-3.2/easymock-3.2.jar

downloadzip:

proxyflags:

setproxy:

testexist:
     [echo] Testing  for C:\Users\Administrator/tomcat-build-libs/cglib-2.2.3/cglib-nodep-2.2.3.jar

downloadfile:

proxyflags:

setproxy:

testexist:
     [echo] Testing  for C:\Users\Administrator/tomcat-build-libs/objenesis-1.2/objenesis-1.2.jar

downloadzip:

ide-eclipse:
     [echo] Eclipse project files created.
     [echo] Read the Building page on the Apache Tomcat documentation site for details on how to configure your Eclipse workspace.

BUILD SUCCESSFUL
Total time: 1 second

F:\TestCode\workspace\apache-tomcat-7.0.90-src>

    

会出现一些问题,无法构建,编译失败,其实都是小问题,有解决办法见目录 6常见问题

 

 

          再次键入ant 对bulid.xml 编译,我们能得到一个tomcat工具,看看是不是很熟悉,

  你会发现,再次ant 构建,编译完成后,会在源码目录下生成output文件夹,这就是我们平时用来发布web项目的Tomcat工具

  运行Tomcat的方法,就跟平时手动运行

  F:\TestCode\workspace\apache-tomcat-7.0.90-src/output/build/bin/startup.bat  一样,就启动Tomcat

F:\TestCode\workspace\apache-tomcat-7.0.90-src>ant
Buildfile: F:\TestCode\workspace\apache-tomcat-7.0.90-src\build.xml

build-prepare:
   [delete] Deleting directory F:\TestCode\workspace\apache-tomcat-7.0.90-src\output\build\temp
    [mkdir] Created dir: F:\TestCode\workspace\apache-tomcat-7.0.90-src\output\build\temp

download-compile:

proxyflags:

setproxy:

testexist:
     [echo] Testing  for C:\Users\Administrator/tomcat-build-libs/commons-daemon-1.1.0/commons-daemon-1.1.0.jar

downloadgz-2:

proxyflags:

setproxy:

testexist:
     [echo] Testing  for C:\Users\Administrator/tomcat-build-libs/commons-pool-1.5.7-src/build.xml

downloadgz-2:

proxyflags:

setproxy:

testexist:
     [echo] Testing  for C:\Users\Administrator/tomcat-build-libs/commons-dbcp-1.4-src/build.xml

downloadgz-2:

build-prepare:
   [delete] Deleting directory F:\TestCode\workspace\apache-tomcat-7.0.90-src\output\build\temp
    [mkdir] Created dir: F:\TestCode\workspace\apache-tomcat-7.0.90-src\output\build\temp

build-manifests:

build-tomcat-dbcp:

testexist:
     [echo] Testing  for C:\Users\Administrator/tomcat-build-libs/ecj-4.4.2/ecj-4.4.2.jar

downloadfile-2:

compile-prepare:

download-validate:

validate:

compile-java6:

guess-java7:

check-java7:

compile-java7:

compile:

build-manifests:

package-java7:

package:

build-docs:
     [xslt] Transforming into F:\TestCode\workspace\apache-tomcat-7.0.90-src\output\build\webapps\docs
     [xslt] Transforming into F:\TestCode\workspace\apache-tomcat-7.0.90-src\output\build\webapps\docs\appdev
     [xslt] Transforming into F:\TestCode\workspace\apache-tomcat-7.0.90-src\output\build\webapps\docs\funcspecs
     [xslt] Transforming into F:\TestCode\workspace\apache-tomcat-7.0.90-src\output\build\webapps\docs\config
     [xslt] Transforming into F:\TestCode\workspace\apache-tomcat-7.0.90-src\output\build\webapps\docs\architecture
     [xslt] Transforming into F:\TestCode\workspace\apache-tomcat-7.0.90-src\output\build\webapps\docs\tribes

compile-webapp-examples:

deploy:
     [copy] Copying 1 file to F:\TestCode\workspace\apache-tomcat-7.0.90-src\output\build\conf

examples-sources:
     [echo] Building Tomcat JDBC pool libraries

prepare:

download:

build-src:

build:

BUILD SUCCESSFUL
Total time: 6 seconds

F:\TestCode\workspace\apache-tomcat-7.0.90-src>

 

build-docs:
     [xslt] Transforming into F:\TestCode\workspace\apache-tomcat-7.0.90-src\output\build\webapps\docs
     [xslt] Transforming into F:\TestCode\workspace\apache-tomcat-7.0.90-src\output\build\webapps\docs\appdev
     [xslt] Transforming into F:\TestCode\workspace\apache-tomcat-7.0.90-src\output\build\webapps\docs\funcspecs
     [xslt] Transforming into F:\TestCode\workspace\apache-tomcat-7.0.90-src\output\build\webapps\docs\config
     [xslt] Transforming into F:\TestCode\workspace\apache-tomcat-7.0.90-src\output\build\webapps\docs\architecture
     [xslt] Transforming into F:\TestCode\workspace\apache-tomcat-7.0.90-src\output\build\webapps\docs\tribes
通过构建,会从svn上获取tomcat源码运行的jar包。

目录:C:\Users\Administrator/tomcat-build-libs

 

 

 

导入eclipse

打开eclipse,File->import->existing Projects into workspace,找到源码 F:\TestCode\workspace\apache-tomcat-7.0.90-src,点击finish

源码导入以后 ,在Problem视图中会发现红色叹号,报错了,这个是因为 缺少相关jar包导致,在Java Build Path下发现不存在的jar

重新引入这次jar,ant构建的时候,会从svn中下载这些jar,我的下载目录是:C:\Users\Administrator/tomcat-build-libs

 

6.会碰见的问题:

问题1:
问题2:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值