Tomcat源码目录:
-
Tomcat源码浅析(一):下载源码,导入eclipse并且运行 Tomcat源码浅析(二):Tomcat原理 Tomcat源码浅析(三):自己动手写一个Tomcat Tomcat源码浅析(四):总结
Tomcat源码浅析(一)
目录:
前言:
Tomcat源码下载后并不能直接运行,因为缺少jar,我们需要使用Ant去编译,构建成一个Eclipse项目后,开始Tomcat源码学习。
1.版本
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: