Fork me on GitHub

tomcat源码环境的安装


⎛⎝。◕⏝⏝◕。⎠⎞⎛⎝。◕⏝⏝◕。⎠⎞

⎛⎝≥⏝⏝≤⎛⎝⎛⎝≥⏝⏝≤⎛⎝

build.xml 编译二进制文件,使用 ant 就可以编译可以发布的 tomcat

  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>tomcat9</artifactId>
    <name>tomcat9</name>
    <version>9.0</version>
    <build>
        <finalName>tomcat9</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.6.0</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>3.0.2</version>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant</artifactId>
            <version>1.9.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant-apache-log4j</artifactId>
            <version>1.9.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant-commons-logging</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.6.1</version>-->
<!--        </dependency>-->
        <!-- 需要注意这个,这里使用的本地的jar,没有使用 maven 仓库中的jar,因为这个 jar 的最新版本在 maven 仓库中没有 -->
        <!-- 这里 jar 是 build.xml 中配置的 jar 信息 -->
        <dependency>
            <groupId>org.eclipse.jdt.core.compiler</groupId>
            <artifactId>ecj</artifactId>
            <version>4.10</version>
            <scope>system</scope>
            <systemPath>D:/.m2/repository/system/ecj-4.10.jar</systemPath>
        </dependency>
    </dependencies>
</project>
  1. 将这个项目转换为 maven 项目,然后 clean, compile 就可以了,一般是不会报错的

  2. 找到 org.apache.catalina.startup.Bootstrap 启动就可以了

  3. 如果不指定 cataline.home 的目录,则会在当前目录下运行,如果指定了 VM 参数,比如 -Dcatalina.home=D:\tomcat\cataline-home 则会在指定的目录下运行
    注意,需要将 conf 目录和 webapps 目录复制到指定的目录下

  4. 避免报错,将 webapps 目录下的 examples 项目删除,它需要额外的编译,会导致启动报错

  5. docs 项目,也需要额外的编译,可以看到原来的都是一些 *.xml 的文件,具体的编译可以查看 build.xml 的 build-docs 任务,所以我的做法是使用 ant 先编译好 docs 复制到 webappas 下

  6. 乱码问题,这里不深究为什么会乱码,也不跟踪乱码的原因,只写一种解决方案

    1. 第一步,修改 org.apache.tomcat.util.res.StringManager.getString(String) 方法,在 return 前添加代码
      // 解决乱码问题
      try {
          str = new String(str.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
      } catch (UnsupportedOperationException e) {
          e.printStackTrace();
      }
      
      return str;
      
    2. 第二步,修改 org.apache.jasper.compiler.Localizer.getMessage(String) 方法,在 return 前添加代码
      // 解决乱码问题
      try {
          errMsg = new String(errMsg.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
      } catch (UnsupportedOperationException e) {
          e.printStackTrace();
      }
      
      return errMsg;
      
  7. 解决 JSP 无法解析的问题,在 org.apache.catalina.startup.ContextConfig.configureStart() 中,webConfig()之后添加:

...
webConfig();

// 初始化 jsp 解析引擎
context.addServletContainerInitializer(new JasperInitializer(), null);
...
posted @ 2021-05-21 22:07  Catalinas  阅读(283)  评论(0编辑  收藏  举报