故事背景
公司的开发环境在远程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
第三步:修改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>