tomcat maven plugin
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
使用 maven plugin 进行测试的时候,会有一个比较特殊的问题,就是读取 web.xml中的 env-entry 会读取不到。
测试的启动命令是:
tomcat7:run
web.xml:
<env-entry>
<env-entry-name>testEnv</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>hello</env-entry-value>
</env-entry>
Servlet Code:
private @Resource(name = "testEnv") String testEnv;
但是,如果将 项目的war包放到 tomcat服务器中,却可以取得到 env-entry 的值.
用 tomcat maven plugin 进行测试的时候,为了查找原因,debug找到以下线索:
tomcat 加载 web.xml 中的 env-entry 元素,代码是在 WebXml::configureContext():
for (ContextEnvironment environment : envEntries.values()) {
context.getNamingResources().addEnvironment(environment);
}
看来这是放到了某个目录结构下去了.
@Resource寻找的时候:
1. tomcat maven plugin 没有找到;
2. 而在 tomcat server上,却找到了
个人推测:
tomcat maven plugin的 jndi的目录结构 跟 tomcat server 的不一样?