欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

荐 轻松构建Tomcat源码

程序员文章站 2022-04-21 10:40:07
目录1.源码下载2.源码构建2.1配置2.2源码导入2.3启动源码运行1.源码下载下载链接:https://tomcat.apache.org/download-80.cgi#8.5.352.源码构建2.1配置解压缩: 首先idea编译器创建空项目tomcat_original,然后将源代码压缩包解答到该项目当前目录中;配置:进入解压缩文件目录下即D:\JetBrains\worksapce\tomcat_original,创建文件夹home,并将conf,wappers目录移到该文...

1.源码下载

下载链接:https://tomcat.apache.org/download-80.cgi#8.5.35

荐
                                                        轻松构建Tomcat源码

2.源码构建

2.1配置

  • 解压缩: 首先idea编译器创建空项目tomcat_original,然后将源代码压缩包解答到该项目当前目录中;
    荐
                                                        轻松构建Tomcat源码
  • 配置
  1. 进入解压缩文件目录下即D:\JetBrains\worksapce\tomcat_original,创建文件夹home,并将conf,wappers目录移到该文件夹中;
    荐
                                                        轻松构建Tomcat源码
  2. 在tomcat源码目录下即D:\JetBrains\worksapce\tomcat_original\apache-tomcat-8.5.55-src,创建 pom.xml文件,引入tomcat依赖;
  3. pom文件中具体内容如下:注意改变artifactId标签的值
<?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>apache-tomcat-8.5.55-src</artifactId>
    <name>Tomcat8.5</name>
    <version>8.5</version>
 
    <build>
        <finalName>Tomcat8.5</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.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.easymock</groupId>
            <artifactId>easymock</artifactId>
            <version>3.4</version>
        </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.5.1</version>
        </dependency>
       
    </dependencies>
</project>

2.2源码导入

荐
                                                        轻松构建Tomcat源码

2.3启动源码

  • 主启动类即main方法,路径:tomcat_original\apache-tomcat-8.5.55-src\java\org\apache\catalina\startup\Bootstrap.java
    荐
                                                        轻松构建Tomcat源码
  • 配置启动类
    荐
                                                        轻松构建Tomcat源码
  • Vm options参数配置
-Dcatalina.home=D:/JetBrains/worksapce/tomcat_original/apache-tomcat-8.5.55-src/home
-Dcatalina.base=D:/JetBrains/worksapce/tomcat_original/apache-tomcat-8.5.55-src/home
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Djava.util.logging.config.file=D:/JetBrains/worksapce/tomcat_original/apache-tomcat-8.5.55-src/home/conf/logging.properties
  • 启动过程中如果控制台日志存在乱码现象,则VM options中加入以下配置:
-Duser.language=en -Duser.region=US -Dfile.encoding=UTF-8
  • 配置org.apache.jasper.servlet.JasperInitializer:
    在tomcat的源码org.apache.catalina.startup.ContextConfig中的configureStart函数中手动将JSP解析器初始化:
    context.addServletContainerInitializer(new JasperInitializer(), null);
 protected synchronized void configureStart() {
        // Called from StandardContext.start()

        if (log.isDebugEnabled()) {
            log.debug(sm.getString("contextConfig.start"));
        }

        if (log.isDebugEnabled()) {
            log.debug(sm.getString("contextConfig.xmlSettings",
                    context.getName(),
                    Boolean.valueOf(context.getXmlValidation()),
                    Boolean.valueOf(context.getXmlNamespaceAware())));
        }

        webConfig();
        
        //初始化,引入下面一行代码
        context.addServletContainerInitializer(new JasperInitializer(), null);

        if (!context.getIgnoreAnnotations()) {
            applicationAnnotationsConfig();
        }
        if (ok) {
            validateSecurityRoles();
        }
        。。。。
        }

注意:除Tomcat,我还分享了最新Java架构项目实战教程+大厂面试题库,有兴趣的 点击此处免费获取,没基础勿进!

运行

将源码构建运行后,在浏览器输入http://localhost:8080,则成功进入tomcat的欢迎页面。

本文地址:https://blog.csdn.net/xuan_lu/article/details/106314487

相关标签: Tomcat