美文网首页
Tomcat 8.5源码 IDEA打开方式

Tomcat 8.5源码 IDEA打开方式

作者: 但时间也偷换概念 | 来源:发表于2020-05-13 15:51 被阅读0次

    环境

    macos操作系统
    idea 2020版本
    apache-tomcat-8.5版本

    步骤

    1.解压。
    2.手动添加文件夹catalina-home,将conf、webapps两个文件夹复制进来,并且新建lib、logs、temp、work四个空文件夹。
    3.添加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>Tomcat8</artifactId>
        <name>Tomcat8</name>
        <version>8</version>
    
        <build>
            <finalName>Tomcat8.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.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>
            <dependency>
                <groupId>javax.xml.soap</groupId>
                <artifactId>javax.xml.soap-api</artifactId>
                <version>1.4.0</version>
            </dependency>
    
        </dependencies>
    </project>
    

    和下面两张图结构一致即可。

    image.png image.png
    • 用IDEA打开
      在pom.xml鼠标右击选择as maven project,module构建完毕以后,java目录会变色的。


      image.png
    • 通过org.apache.catalina.startup.Bootstrap中的main方法启动。
      我们在IDEA中配置Application方式启动,附带启动参数。


      image.png

    启动参数如下:

    -Dcatalina.home/Users/niezhengyu/Downloads/apache-tomcat-8.5.55-src/catalina-home
    -Dcatalina.base=/Users/niezhengyu/Downloads/apache-tomcat-8.5.55-src/catalina-home
    -Djava.endorsed.dirs=/Users/niezhengyu/Downloads/apache-tomcat-8.5.55-src/catalina-home/endorsed
    -Djava.io.tmpdir=/Users/niezhengyu/Downloads/apache-tomcat-8.5.55-src/catalina-home/temp
    -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
    -Djava.util.logging.config.file=/Users/niezhengyu/Downloads/apache-tomcat-8.5.55-src/catalina-home/conf/logging.properties`
    

    路径前缀自行修改即可。
    然后我们加一行代码到源码中,手动初始化jsp解析器。
    在configureStart类configureStart方法中。
    在webConfig()后添加

       context.addServletContainerInitializer(new JasperInitializer(), null);
    

    参考下图注释jsp的地方。

     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();
            // jsp 
            context.addServletContainerInitializer(new JasperInitializer(), null);
    
            if (!context.getIgnoreAnnotations()) {
                applicationAnnotationsConfig();
            }
            if (ok) {
                validateSecurityRoles();
            }
    
            // Configure an authenticator if we need one
            if (ok) {
                authenticatorConfig();
            }
    
            // Dump the contents of this pipeline if requested
            if (log.isDebugEnabled()) {
                log.debug("Pipeline Configuration:");
                Pipeline pipeline = context.getPipeline();
                Valve valves[] = null;
                if (pipeline != null) {
                    valves = pipeline.getValves();
                }
                if (valves != null) {
                    for (Valve valve : valves) {
                        log.debug("  " + valve.getClass().getName());
                    }
                }
                log.debug("======================");
            }
    
            // Make our application available if no problems were encountered
            if (ok) {
                context.setConfigured(true);
            } else {
                log.error(sm.getString("contextConfig.unavailable"));
                context.setConfigured(false);
            }
    
        }
    
    

    然后启动项目,输入localhost:8080即可。


    image.png

    相关文章

      网友评论

          本文标题:Tomcat 8.5源码 IDEA打开方式

          本文链接:https://www.haomeiwen.com/subject/tkwnnhtx.html