社区版的IntelliJ IDEA上配置Tomcat的Debug环境

故事背景

公司的开发环境在远程ucs2机器上,用的开发工具是社区版的idea,没有tomcat等企业级高级功能,况且不能上外网(有自己的私服仓库),而我又不想每次都用远程debug测试、排除问题,我记得上家公司是内嵌到项目中的jetty容器,这里不适用,所有我选择了Tomcat的Maven插件来弥补这个“不习惯”。只需要做以下几步:

第一步:修改pom.xml

  • <plugins>内新增:

    <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
            <port>18888</port>
            <path>/</path>
            <contextFile>src/main/webapp/META-INF/context.xml</contextFile>
        </configuration>
    </plugin>

    port: 可以配置任意未占用的端口,我配置为18888,;
    contextFile: 指定tomcat的配置的上下文信息,我的项目是src/main/webapp/META-INF/context.xml

  • 因为我们的项目中引入了javax.servlet-api,我在测试过程中发现这个会导致启动报错,故我这里将scope设置成provided,以避之。

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>

第二步:修改context.xml

tomcat-email-context-outter.jpg

第三步:修改log4j.xml(无该需求,请略过)

如果你想要在调试的时候将日志打印在IDEA控制台上,请确保<root>中有ConsoleAppender的引用,本文件已经配置好ConsoleAppender,被命名为Appender1,故可以直接引用如下:

<root>
    <priority value="INFO"/>
    <appender-ref ref="Appender2"/>
    <appender-ref ref="Appender1"/>
</root>

第四步:配置服务启动

  • 点击进入此路径:RUN->Edit Configurations...
    这里写图片描述

  • 然后点击左上角+,选择Maven,

    这里写图片描述

  • 填写配置,请按照途中圈出的4处进行填写。

    这里写图片描述

    Command line: tomcat7:run

  • Runner(去掉User Project settings的勾)

    这里写图片描述

    • VM Options: 设置必要的启动参数
    • 如果需要跳过test,可以直接勾选Skip tests

第五步:执行启动命令

选择上一步配置的maven插件,点击Debug按钮,最后打印如下,即成功

[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ tomcat7 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] <<< tomcat7-maven-plugin:2.2:run (default-cli) < process-classes @ tomcat7 <<<
[INFO] 
[INFO] --- tomcat7-maven-plugin:2.2:run (default-cli) @ tomcat7 ---
[INFO] Running war on http://localhost:18888/
[INFO] Using existing Tomcat server configuration at /**/**/**/**/test-tomcatService/target/tomcat
[INFO] create webapp with contextPath: 
Jul 10, 2018 6:14:55 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-18888"]
Jul 10, 2018 6:14:55 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Tomcat
Jul 10, 2018 6:14:55 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.47

第六步: 测试

这里利用在EmailController上打上断点,利用Postman进行api调用。结果如下图
这里写图片描述
这里写图片描述

Test Request Data:

{
    "request":{
        "requestId":"957452",
        "resourceId":"980767686",
        "requesterName":"zhangsan",
        "requestedFor":"lisi"
    },
    "eventType":"Failure"
}

提醒:

执行启动命令时,可能会有下载插件的过程,如果settings.xml中没有配置repository可能下载不了。我的settings.xml配置如下,仅供参考(因为是公司私服,所以不能示外人,自己替换下即可):

<?xml version="1.0" encoding="UTF-8"?>
<settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd"
          xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <proxies>
        <proxy>
            <active>true</active>
            <protocol>http</protocol>
            <host>*********</host>
            <port>80</port>
            <nonProxyHosts>*.xx.com|localhost</nonProxyHosts>
        </proxy>
    </proxies>

    <!-- 下面的所有配置都是为了idea配置tomcat插件而添加的 -->
    <profiles>
        <profile>
            <id>artifactory</id>
            <pluginRepositories>
                <pluginRepository>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                    <id>xx-release</id>
                    <name>plugins-release</name>
                    <url>http://repo-xx.xx.com/xx/plugins-release</url>
                </pluginRepository>
                <pluginRepository>
                    <snapshots />
                    <id>xx-snapshots</id>
                    <name>plugins-snapshot</name>
                    <url>http://repo-xx.xx.com/xx/plugins-snapshot</url>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>
    <activeProfiles>
        <activeProfile>artifactory</activeProfile>
    </activeProfiles>
</settings>
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值