IDEAでTomcat 8.5.35ソースコードを実行する方法

前提条件として、Java 1.8以上の環境、Maven、IDEAのインストールと設定が完了している必要があります。

  1. Tomcatソースコードのダウンロード:https://tomcat.apache.org/download-80.cgi#8.5.35 ==============================================================

  2. pom.xmlの作成 ===========

Mavenでファイルを整理するために、apache-tomcat-8.5.35-srcルートディレクトリにcatalina-homeディレクトリと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.5</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>3.8.1</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.13.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.easymock</groupId>
            <artifactId>easymock</artifactId>
            <version>4.3</version>
        </dependency>
        <dependency>
            <groupId>ant</groupId>
            <artifactId>ant</artifactId>
            <version>1.10.11</version>
        </dependency>
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.3</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.16.0</version>
        </dependency>
       
    </dependencies>
</project>

apache-tomcat-8.0.53-srcディレクトリにあるconfとwebappsフォルダをcatalina-homeディレクトリにコピーします。

三、IDEAプロジェクトの実行設定

Main classをorg.apache.catalina.startup.Bootstrapに設定します。

VMオプションを追加します:

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

注意:ビルド時にテストコードでエラーが発生する場合は、そのコードをコメントアウトしてください。この記事ではTomcatソースコードのutil.TestCookieFilterクラスでエラーが発生するため、コメントアウトします。

プロジェクトを実行し、http://localhost:8080にアクセスすると、結果が表示されます:

これは、org.apache.catalina.startup.Bootstrapを直接起動した際にorg.apache.jasper.servlet.JasperInitializerが読み込まれず、JSPをコンパイルできないためです。解決策として、tomcatのソースコードorg.apache.catalina.startup.ContextConfigのconfigureStart関数にJSPパーサーの初期化を手動で追加します:

protected synchronized void configureStart() {
        // 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();
        }

        // 必要であれば認証コンポーネントを設定
        if (ok) {
            authenticatorConfig();
        }

        // 要求があれば、このパイプラインの内容をダンプ
        if (log.isDebugEnabled()) {
            log.debug("パイプライン設定:");
            Pipeline pipeline = context.getPipeline();
            Valve valves[] = null;
            if (pipeline != null) {
                valves = pipeline.getValves();
            }
            if (valves != null) {
                for (int i = 0; i < valves.length; i++) {
                    log.debug("  " + valves[i].getClass().getName());
                }
            }
            log.debug("======================");
        }

        // 問題がなければアプリケーションを利用可能にする
        if (ok) {
            context.setConfigured(true);
        } else {
            log.error(sm.getString("contextConfig.unavailable"));
            context.setConfigured(false);
        }

    }

修正後、プロジェクトを再度起動し、ブラウザでhttp://localhost:8080/にアクセスすると、おなじみのウェルカムページが表示されます。

6月4日 21:08 投稿