IDEA导入tomcat源码

ima
ima
2021-02-28 / 1 评论 / 235 阅读
温馨提示:
本文最后更新于2021-02-28,若内容或图片失效,请留言反馈。

IDEA导入tomcat源码

下载源码

https://tomcat.apache.org/download-80.cgi

image-20210227152346686

解压压缩包

解压压缩包 并新建home文件夹(可自定义文件夹名)

将conf、webapps文件夹移动到home文件中

文件根目录新建pom.xml

image-20210227154153102

<?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</groupId>
    <artifactId>tomcat</artifactId>
    <name>apache-tomcat-8.5.24</name>
    <version>8.5.24</version>

    <build>
        <finalName>Tomcat-8.5.24</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>3.5.1</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>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant</artifactId>
            <version>1.10.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>
        <!-- https://mvnrepository.com/artifact/org.glassfish/javax.xml.rpc -->
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.xml.rpc</artifactId>
            <version>3.0.1-b03</version>
        </dependency>

    </dependencies>
</project>

idea新建空项目

image-20210227152627474

将tomcat文件夹移动到项目中

image-20210227152721933

配置maven

下载maven

https://archive.apache.org/dist/maven/maven-3/3.5.2/binaries/

最好是3.5.2版本 不然其他版本好像会出现奇奇怪怪的问题(3.6.3版本亲测失败)。

解压,并新建一个工作目录

image-20210227153056622

conf/setings.xml

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  -->
  <localRepository>E:\developement\maven\localhouse</localRepository>

image-20210227153227674

定位mirrors添加如下代码

     <mirror>
      <id>AliMaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>        
    </mirror>

image-20210227153348147

保存文件

环境变量

新建系统环境变量 M2_HOME
路径就写maven的目录
path变量中添加%M2_HOME%\bin
查看是否成功
cmd
mvn -v

image-20210227153716439

image-20210227153734552

image-20210227153756905

IDEA配置

image-20210227153953338

image-20210227154232885

image-20210227154258825

image-20210227154326371

等待右下角下载进度完成

若出现错误

image-20210227154437826

下载完成之后配置main方法

这里注意下是否配置了SDK

image-20210227154645752

正常开始下一步

image-20210227154718954

image-20210227154753403

image-20210227154938436

image-20210227155041684

image-20210227155130163

VM option 路径自己改

‐Dcatalina.home=E:\ideaprojects\tomcatdemo\apache-tomcat-8.5.42-src\home
‐Dcatalina.base=E:\ideaprojects\tomcatdemo\apache-tomcat-8.5.42-src\home
‐Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
‐Djava.util.logging.config.file=E:\ideaprojects\tomcatdemo\apache-tomcat-8.5.42-src\home\conf\logging.properties

注意最前面的符号 是居中的横线 导入idea后可能会发生变化

我在网上还找到一个比较全的配置

-Dcatalina.home=E:\tomcat\tomcatdemo\apache-tomcat-8.5.42-src
-Dcatalina.base=E:\tomcat\tomcatdemo\apache-tomcat-8.5.42-src
-Djava.endorsed.dirs=E:\tomcat\tomcatdemo\apache-tomcat-8.5.42-src
-Djava.io.tmpdir=E:\tomcat\tomcatdemo\apache-tomcat-8.5.42-src
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Djava.util.logging.config.file=E:\tomcat\tomcatdemo\apache-tomcat-8.5.42-src\conf\logging.properties

启动Tomcat

image-20210227155243957

image-20210227155227138

发生错误

出现上述异常的原因,是我们直接启动org.apache.catalina.startup.Bootstrap的时候没有加载JasperInitializer,从而无法编译JSP。解决办法是在tomcat的源码ContextConfig中的configureStart函数中手动将JSP解析器初始

context.addServletContainerInitializer(new JasperInitializer(), null);

image-20210227155830818

image-20210227155902868

image-20210227155924051

image-20210227155949772

成功访问

image-20210227160034611