将tomcat源码导入IDEA步骤记录(maven形式)
官方文档:Building Tomcat
Tomcat源码是用ant工具构建的,毕竟历史比较悠久。但现在主流构建工具还是maven,所以还是以maven形式来构建tomcat。这里我使用的tomcat源码为apache-tomcat-8.0.42-src。
开始前先安装好apache-ant,并配置好环境变量。下面是正式的步骤。
1.在源码根目录下创建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.0</artifactId> <name>Tomcat8.0</name> <version>8.0</version> <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>org.apache.ant</groupId> <artifactId>ant</artifactId> <version>1.9.5</version> </dependency> <dependency> <groupId>javax.xml.rpc</groupId> <artifactId>javax.xml.rpc-api</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>wsdl4j</groupId> <artifactId>wsdl4j</artifactId> <version>1.6.2</version> </dependency> <dependency> <groupId>org.eclipse.jdt.core.compiler</groupId> <artifactId>ecj</artifactId> <version>4.5.1</version> </dependency> </dependencies> <build> <finalName>Tomcat8.0</finalName> <sourceDirectory>java</sourceDirectory> <resources> <resource> <directory>java</directory> </resource> </resources> <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> </plugins> </build> </project>
2.在源码根目录的同级目录下新建catalina_home目录
3.在源码根目录下,运行an命令
会在源码根目录下生成output文件夹。将/output/build/ 下的temp、bin、conf、webapps、logs 复制到catalina-home下。
此时,可将源码目录下多余的目录删除,可删除bin/,modules/,res/,test/,build.properties等。
4.将源码导入IDEA
在Run菜单,点Edit Configurations,点“+”,选Applcation,配置如下:
在Man class中填入: org.apache.catalina.startup.Bootstrap
在VM options中填入如下的VM参数:(这里的cataline-home就是前面创建的目录路径)
-Dcatalina.home=D:/WorkSpace/backend/source/catalina-home -Dcatalina.base=D:/WorkSpace/backend/source/catalina-home -Djava.endorsed.dirs=D:/WorkSpace/backend/source/catalina-home/endorsed -Djava.io.tmpdir=D:/WorkSpace/backend/source/catalina-home/temp -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=D:/WorkSpace/backend/source/catalina-home/conf/logging.properties
然后,就可以启动了。
如果编译时出现test下的util.TestCookieFilter类报错,实际上是不存在该类的,解决方法就是直接新建该类,内容如下。
package util; import java.util.Locale; import java.util.StringTokenizer; public class CookieFilter { private static final String OBFUSCATED = "[obfuscated]"; private CookieFilter() { // Hide default constructor } public static String filter(String cookieHeader, String sessionId) { StringBuilder sb = new StringBuilder(cookieHeader.length()); // Cookie name value pairs are ';' separated. // Session IDs don't use ; in the value so don't worry about quoted // values that contain ; StringTokenizer st = new StringTokenizer(cookieHeader, ";"); boolean first = true; while (st.hasMoreTokens()) { if (first) { first = false; } else { sb.append(';'); } sb.append(filterNameValuePair(st.nextToken(), sessionId)); } return sb.toString(); } private static String filterNameValuePair(String input, String sessionId) { int i = input.indexOf('='); if (i == -1) { return input; } String name = input.substring(0, i); String value = input.substring(i + 1, input.length()); return name + "=" + filter(name, value, sessionId); } public static String filter(String cookieName, String cookieValue, String sessionId) { if (cookieName.toLowerCase(Locale.ENGLISH).contains("jsessionid") && (sessionId == null || !cookieValue.contains(sessionId))) { cookieValue = OBFUSCATED; } return cookieValue; } }
5.启动,并访问项目:http://localhost:8080,熟悉的界面就出来了。
不积跬步,无以至千里。不积小流,无以成江海!