idea环境源码调试tomcat9

1、源码下载:http://archive.apache.org/dist/tomcat/tomcat-9/v9.0.30/src/

2、创建项目根目录:apache-tomcat-9-debug

3、创建子目录apache-tomcat-9-debug\apache-tomcat-9.0.30_src将tomcat源码解压到apache-tomcat-9.0.30_src文件夹下

4、创建子目录apache-tomcat-9-debug\catalina-home将apache-tomcat-9.0.30_src文件夹下的conf文件夹拷贝到catalina-home目录

5、创建外层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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.yang</groupId>
<artifactId>apache-tomcat-study</artifactId>
<name>Tomcat 9.0 Study</name>
<version>1.0</version>
<packaging>pom</packaging>

<modules>
<module>apache-tomcat-9.0.30_src</module>
</modules>
</project>
目录结构如下

6、创建tomcat9依赖文件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>tomcat9</artifactId>
<name>tomcat9</name>
<version>9.0</version>

<build>
<finalName>apache-tomcat-9.0.0.M26-src</finalName>
<sourceDirectory>java</sourceDirectory>
<!--<testSourceDirectory>test</testSourceDirectory> test 下的有些文件报错,因此将test文件夹去掉了-->
<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.1</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
</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.6.5</version>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>com.sun.xml.messaging.saaj</groupId>
<artifactId>saaj-impl</artifactId>
<version>1.3</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.6.1</version>
</dependency>
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxrpc-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxrpc-api</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
</project>
7、启动idea,选择"打开",打开项目apache-tomcat-9-debug
8、右键点击apache-tomcat-9.0.30_src目录下java文件夹,选择Mark Directory As Source Root
9、添加配置文件,选择application;
main class配置:org.apache.catalina.startup.Bootstrap
vm option 配置:-Dfile.encoding=UTF-8 -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

10、编译项目,可能会报错
Error:(320, 45) java: 找不到符号
符号: 变量 VERSION_9
位置: 类 org.eclipse.jdt.internal.compiler.impl.CompilerOptions
统一将错误改为VERSION_1_8即可
11、运行项目

启动、运行成功;
12、为何会有乱码:
原因:一般是中文编码不匹配导致;经跟踪,发现是ResourceBundle读取org\apache\catalina\startup\LocalStrings_zh_CN.properties文件时没有用utf8解码导致;
解决:修改org.apache.tomcat.util.res.StringManager类中的getString函数;

if (bundle != null) {
str = bundle.getString(key);
}
改为
if (bundle != null) {
str = new String(bundle.getString(key).getBytes(“ISO-8859-1”), “UTF-8”);
}
再次运行,一切正常

Copyright © 2017 ITGATHER.COM - 闽ICP备19016859号-1
扫二维码--> 返回顶部