Because of learning needs, I need to compile and run tomcat to perform a simple tracking analysis of its source code. Since I have not been exposed to java-related knowledge before, the installation resistance is huge. Finally, I asked my development friend to solve the final problem. Sort it out so that everyone can quickly complete the relevant deployment. This article only solves the source code compilation problem of the tomcat-8.5.46 version.
1. Obtain tomcat source code
Download link: https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.46/src/apache-tomcat-8.5.46-src.zip
2. Unzip to a directory with an English path
Add a directory named catalina-home under this directory, and copy the conf and webapps folders in the source code decompression file to the instance folder.
And create empty ilb, logs and temp folders under this directory
3. Create two pom.xml files
The pom.xml file at the same level as the decompression directory:
<?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>gxf</groupId>
<artifactId>apache-tomcat-8</artifactId>
<name>apache-tomcat-8-source</name>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>apache-tomcat-8.5.46-src</module>
</modules>
</project>
The pom.xml file at the same level as the decompressed source code directory:
<?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.46</artifactId>
<name>Tomcat8.5.46</name>
<version>8.5</version>
<build>
<finalName>Tomcat8.0</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.0.2</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>3.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</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.6.1</version>
</dependency>
</dependencies>
</project>
4. The IDE imports the project file
Open the IDE directly. click open
See clearly, it must be the same level pom.xml of the imported root directory. Here I am doing a second demonstration so some traces can be seen on the left.
5. Project environment configuration - program entry
Click the configuration bar in the upper right corner to add applocation configuration
Configuration item entry: The previous java version is irrelevant, mainly because the middle and rear entries must be found correctly. Otherwise, there will be a situation where the module cannot be loaded.
org.apache.catalina.startup.Bootstrap
Here you need to configure environment variables, click modify opiitions to add environment variables:
Copy the following configuration into
-Dcatalina.home=catalina-home
-Dcatalina.base=catalina-home
-Djava.io.tmpdir=catalina-home/temp
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Djava.util.logging.config.file=catalina-home/conf/logging.properties
Remember to delete our test file here, otherwise it will cause a compilation error
6. Manually register, compile and run
If you want to compile tomcat8 and later, you need to manually load it org.apache.jasper.servlet.JasperInitializer
to start the parsing function of the middleware.
Find the file and add a line of code:
context.addServletContainerInitializer(new JasperInitializer(), null);
You can see that such a line of error is caused because we have not imported the corresponding package. We stop the mouse on it, and alt+enter can automatically import related modules to solve the dependency problem.
After the color changes, click Run to successfully access the tomcat middleware.
http://127.0.0.1:8080/