测试环境为:Ubuntu 14.04 LTS + Tomcat 7.0.63

下载Tomcat源码

cd ~/Downloads
wget -c http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.63/src/apache-tomcat-7.0.63-src.tar.gz
tar -xvf apache-tomcat-7.0.63-src.tar.gz
cd apache-tomcat-7.0.63-src

转换成Maven工程

cd ~/Downloads/apache-tomcat-7.0.63-src
touch pom.xml

pom.xml 文件内容如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


    <modelVersion>4.0.0</modelVersion>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>Tomcat7.0</artifactId>
    <name>Tomcat7.0</name>
    <version>7.0</version>

    <build>
        <finalName>Tomcat7.0</finalName>
        <sourceDirectory>java</sourceDirectory>
        <testSourceDirectory>test</testSourceDirectory>
        <resources>
            <resource>
                <directory>java</directory>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>test</directory>
            </testResource>
        </testResources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3</version>

                <configuration>
                    <encoding>UTF-8</encoding>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>ant</groupId>
            <artifactId>ant</artifactId>
            <version>1.7.0</version>
        </dependency>
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>javax.xml</groupId>
            <artifactId>jaxrpc</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jdt.core.compiler</groupId>
            <artifactId>ecj</artifactId>
            <version>4.4.2</version>
        </dependency>
    </dependencies>

</project>

注意点:当你使用不同版本的Tomcat时,修改一下dependencies里相应的依赖版本即可,版本请参考Tomcat源码目录的build.properties.default文件里相应依赖的版本作修改即可。

导入到Eclipse

File -> import -> Maven -> Existing Maven Projects 然后定位到 ~/Downloads/apache-tomcat-7.0.63-src目录即可

启动Tomcat

打开org.apache.catalina.startup.Bootstrap这个启动类,然后右键选择Run As -> Run Configurations -> Arguments -> VM arguments 输入以下参数(相应的目录,请自己作修改)

-Dcatalina.home=/home/yang/Downloads/apache-tomcat-7.0.63-src  -Dcatalina.base=/home/yang/Downloads/apache-tomcat-7.0.63-src
-Djava.endorsed.dirs=/home/yang/Downloads/apache-tomcat-7.0.63-src/endorsed -Djava.io.tmpdir=/home/yang/Downloads/apache-tomcat-7.0.63-src/temp
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Djava.util.logging.config.file=/home/yang/Downloads/apache-tomcat-7.0.63-src/conf/logging.properties

OK

一切都没有问题时,启动的结果,类似如下:

Aug 25, 2015 2:32:09 PM org.apache.catalina.startup.ClassLoaderFactory validateFile
WARNING: Problem with directory [/home/yang/Downloads/apache-tomcat-7.0.63-src/lib], exists: [false], isDirectory: [false], canRead: [false]
Aug 25, 2015 2:32:09 PM org.apache.catalina.startup.ClassLoaderFactory validateFile
WARNING: Problem with directory [/home/yang/Downloads/apache-tomcat-7.0.63-src/lib], exists: [false], isDirectory: [false], canRead: [false]
Aug 25, 2015 2:32:09 PM org.apache.catalina.startup.ClassLoaderFactory validateFile
WARNING: Problem with directory [/home/yang/Downloads/apache-tomcat-7.0.63-src/lib], exists: [false], isDirectory: [false], canRead: [false]
Aug 25, 2015 2:32:09 PM org.apache.catalina.startup.ClassLoaderFactory validateFile
WARNING: Problem with directory [/home/yang/Downloads/apache-tomcat-7.0.63-src/lib], exists: [false], isDirectory: [false], canRead: [false]
Aug 25, 2015 2:32:09 PM org.apache.catalina.startup.Catalina initDirs
SEVERE: Cannot find specified temporary folder at /home/yang/Downloads/apache-tomcat-7.0.63-src/temp
Aug 25, 2015 2:32:09 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/@VERSION@
Aug 25, 2015 2:32:09 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          @VERSION_BUILT@
Aug 25, 2015 2:32:09 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         @VERSION_NUMBER@
Aug 25, 2015 2:32:09 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Linux
Aug 25, 2015 2:32:09 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            3.16.0-30-generic
Aug 25, 2015 2:32:09 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Aug 25, 2015 2:32:09 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             /home/yang/Java/jdk1.8.0_51/jre
Aug 25, 2015 2:32:09 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_51-b16
Aug 25, 2015 2:32:09 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Aug 25, 2015 2:32:09 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         /home/yang/Downloads/apache-tomcat-7.0.63-src
Aug 25, 2015 2:32:09 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         /home/yang/Downloads/apache-tomcat-7.0.63-src
Aug 25, 2015 2:32:09 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=/home/yang/Downloads/apache-tomcat-7.0.63-src
Aug 25, 2015 2:32:09 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/home/yang/Downloads/apache-tomcat-7.0.63-src
Aug 25, 2015 2:32:09 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=/home/yang/Downloads/apache-tomcat-7.0.63-src/endorsed
Aug 25, 2015 2:32:09 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.io.tmpdir=/home/yang/Downloads/apache-tomcat-7.0.63-src/temp
Aug 25, 2015 2:32:09 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
Aug 25, 2015 2:32:09 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.util.logging.config.file=/home/yang/Downloads/apache-tomcat-7.0.63-src/conf/logging.properties
Aug 25, 2015 2:32:09 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=UTF-8
Aug 25, 2015 2:32:09 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Aug 25, 2015 2:32:09 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8090"]
Aug 25, 2015 2:32:09 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8019"]
Aug 25, 2015 2:32:09 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 355 ms
Aug 25, 2015 2:32:09 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Aug 25, 2015 2:32:09 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/@VERSION@
Aug 25, 2015 2:32:09 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /home/yang/Downloads/apache-tomcat-7.0.63-src/webapps/manager
Aug 25, 2015 2:32:10 PM org.apache.catalina.startup.TldConfig execute
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Aug 25, 2015 2:32:10 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /home/yang/Downloads/apache-tomcat-7.0.63-src/webapps/manager has finished in 159 ms
Aug 25, 2015 2:32:10 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /home/yang/Downloads/apache-tomcat-7.0.63-src/webapps/examples
Aug 25, 2015 2:32:10 PM org.apache.catalina.startup.TldConfig execute
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Aug 25, 2015 2:32:10 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: One or more listeners failed to start. Full details will be found in the appropriate container log file
Aug 25, 2015 2:32:10 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/examples] startup failed due to previous errors
Aug 25, 2015 2:32:10 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /home/yang/Downloads/apache-tomcat-7.0.63-src/webapps/examples has finished in 99 ms
Aug 25, 2015 2:32:10 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /home/yang/Downloads/apache-tomcat-7.0.63-src/webapps/docs
Aug 25, 2015 2:32:10 PM org.apache.catalina.startup.TldConfig execute
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Aug 25, 2015 2:32:10 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /home/yang/Downloads/apache-tomcat-7.0.63-src/webapps/docs has finished in 20 ms
Aug 25, 2015 2:32:10 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /home/yang/Downloads/apache-tomcat-7.0.63-src/webapps/ROOT
Aug 25, 2015 2:32:10 PM org.apache.catalina.startup.TldConfig execute
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Aug 25, 2015 2:32:10 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /home/yang/Downloads/apache-tomcat-7.0.63-src/webapps/ROOT has finished in 24 ms
Aug 25, 2015 2:32:10 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /home/yang/Downloads/apache-tomcat-7.0.63-src/webapps/host-manager
Aug 25, 2015 2:32:10 PM org.apache.catalina.startup.TldConfig execute
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Aug 25, 2015 2:32:10 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /home/yang/Downloads/apache-tomcat-7.0.63-src/webapps/host-manager has finished in 19 ms
Aug 25, 2015 2:32:10 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8090"]
Aug 25, 2015 2:32:10 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8019"]
Aug 25, 2015 2:32:10 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 370 ms