Tom_No_01 IDEA tomcat 源码环境搭建
1.下载源码
apache-tomcat-8.5.50-src
2.下载源码
放D盘,解压后根目录新建pom.xml和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>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>
catalina-home中赋值根目录中的conf和webapps 还要新建空目录,如图
3.
编辑Configration
- 将apache-tomcat-8.0.53-src项目导入进IDEA中
- 配置启动参数
新建Application
Main class设置为org.apache.catalina.startup.Bootstrap
添加VM options
-Dcatalina.home=D:\Tomcat_Study\apache-tomcat-8.5.38-src\catalina-home
说明:如果编译build的时候出现Test测试代码报错,注释该代码即可。本文中的Tomcat源码util.TestCookieFilter类会报错,将其注释即可。
4.启动报错
项目启动完毕我们可以测试了,在浏览器访问http://localhost:8080/ 发现打不开我们看到的经典欢迎页面了,页面报了一个错
Unable to compile class for JSP
原因是我们直接启动org.apache.catalina.startup.Bootstrap的时候没有加载org.apache.jasper.servlet.JasperInitializer,从而无法编译JSP。解决办法是在tomcat的源码org.apache.catalina.startup.ContextConfig中手动将JSP解析器初始化:
context.addServletContainerInitializer(new JasperInitializer(), null);
修改完后,项目再启动,我们再在浏览器访问http://localhost:8080/ ,就可以看到我们所熟悉的经典欢迎页面了