以ant形式build tomcat能夠參考官方文檔:Building Tomcat。html
如今主流的構建工具仍是maven,因此仍是以maven形式來build tomcat。java
我使用的tomcat源碼爲apache-tomcat-8.0.42-src。 開始前先安裝好apache-ant,並配置好環境變量。web
下面是正式的步驟。apache
1.在源碼根目錄下建立pom.xml文件,輸入如下內容api
<?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目錄tomcat
3.在源碼根目錄下,運行an命令cookie
會在源碼根目錄下生成output文件夾。將/output/build/ 下的temp、bin、conf、webapps、logs 複製到catalina-home下。session
此時,可將源碼目錄下多餘的目錄刪除,可刪除bin/,modules/,res/,test/,build.properties等。app
4.將源碼導入IDEAeclipse
在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/,熟悉的界面出來了,說明一切OK了。