tomcat7 maven插件无法';在类路径上找不到属性文件
我正在尝试使用“tomcat7 maven插件”在war文件上运行集成测试。当war被dployed时,它基本上会加载spring应用程序上下文,而这反过来又会在类路径上加载属性文件。在普通的tomcat实例中,我将属性文件保存在机器上的某个位置,然后配置catalina.properties->common.loader以指向该目录。但我不知道如何用这个插件实现这一点 以下是我尝试的插件配置:tomcat7 maven插件无法';在类路径上找不到属性文件,maven,tomcat7,integration-testing,Maven,Tomcat7,Integration Testing,我正在尝试使用“tomcat7 maven插件”在war文件上运行集成测试。当war被dployed时,它基本上会加载spring应用程序上下文,而这反过来又会在类路径上加载属性文件。在普通的tomcat实例中,我将属性文件保存在机器上的某个位置,然后配置catalina.properties->common.loader以指向该目录。但我不知道如何用这个插件实现这一点 以下是我尝试的插件配置: <plugin> <groupId>org.apac
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<!-- port>8090</port -->
<url>http://localhost:8080/manager/text</url>
<server>TomcatServer</server>
<path>/${project.build.finalName}</path>
<!-- systemProperties>
<systemProperty>
<maven.tomcat.port>8080</maven.tomcat.port>
</systemProperty>
</systemProperties -->
<!-- if you want to use test dependencies rather than only runtime -->
<useTestClasspath>false</useTestClasspath>
<!-- optional if you want to add some extra directories into the classloader -->
<additionalClasspathDirs>
<additionalClasspathDir>${basedir}/src/test/resources</additionalClasspathDir>
</additionalClasspathDirs>
<additionalConfigFilesDir>${basedir}/src/test/resources</additionalConfigFilesDir>
</configuration>
<executions>
<execution>
<id>tomcat-run</id>
<phase>pre-integration-test</phase>
<goals>
<!-- goal>run</goal -->
<goal>run-war-only</goal>
</goals>
<configuration>
<additionalClasspathDirs>
<additionalClasspathDir>${basedir}/src/test/resources</additionalClasspathDir>
</additionalClasspathDirs>
<additionalConfigFilesDir>${basedir}/src/test/resources</additionalConfigFilesDir>
</configuration>
</execution>
<execution>
<id>tomcat-shutdown</id>
<goals>
<goal>shutdown</goal>
</goals>
<phase>post-integration-test</phase>
</execution>
</executions>
</plugin>
org.apache.tomcat.maven
tomcat7 maven插件
2.1
http://localhost:8080/manager/text
TomcatServer
/${project.build.finalName}
假的
${basedir}/src/test/resources
${basedir}/src/test/resources
雄猫赛跑
预集成测试
只管打仗
${basedir}/src/test/resources
${basedir}/src/test/resources
tomcat关机
关闭
整合后测试
我们遇到了同样的问题。我为此提交了JIRA(MTOMCAT-246)。事实证明,它只适用于tomcat7:run目标,而不适用于run战争。他们计划在3.0版本中修复它。我们有一个适当的解决办法,见下文
实现它的一种方法是将属性文件打包为jar文件,并将它们包含在插件的依赖项中
在我们的例子中,这意味着从父项目(存储各种环境的配置)的config文件夹中创建一个maven模块:
maven汇编插件
2.4
distribution.xml
建造
**/*.物业
**/*.xml
distribution.xml看起来像:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>config</id>
<formats>
<format>jar</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.properties</include>
<include>*.xml</include>
</includes>
</fileSet>
</fileSets>
</assembly>
配置
罐子
${project.build.directory}
/
*.物业
*.xml
然后在插件部分:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
....
<dependencies>
<dependency>
<groupId>com.your.project</groupId>
<artifactId>config</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
org.apache.tomcat.maven
tomcat7 maven插件
2.1
....
com.your.project
配置
1.0-快照
您可以添加一个指向config.jar文件的链接,该文件包含这些属性文件 Spring如何加载这些属性?如果您使用的是
propertyplaceholderconfigure
,您可以添加一个文件模式,通过src/test/resources
为其提供文件。毕竟,后者将是类路径的一部分。如果这似乎是您所缺少的,我可以给出一个更广泛的示例。我使用“classpath:{env}.properties”,当它在生产环境中从容器类路径加载时,我不能更改它。总之,我最终使用Surefire/Cargo来进行单元/容器集成测试。TCargo有这样一个简单的配置,它就像一个符咒:${basedir}/src/test/resources/{env}.properties lib我不明白为什么AdditionalClassPathDir或additionalConfigFilesDir在tomcat插件中不起作用。我是不是遗漏了什么?我可以在tomcat下的分解战争中看到复制到conf文件夹中的文件(通过插件)…但conf文件夹似乎不在类路径上。顺便说一句,src/test/resources位于java构建路径中,因为它在我的porject文件夹中的.classpath文件中有条目,但它不是war工件的一部分,所以你必须从外部告诉plugin/tomcat在哪里查找道具文件。我想src/test/resources
应该已经在类路径上了,而不管其他…
。我从来没用过。这是一个很好的解决办法。在配置使用自定义工厂的JNDI数据源时,我们使用相同的解决方案(向插件添加依赖项)。在普通的Tomcat中,我们必须将jar复制到/lib中。通过添加依赖项,tomcat7插件也可以实现同样的效果。在那里,最后一位评论者声称,他使它在没有“人工”依赖项的情况下工作——dev instance config/dev src/main/resources
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
....
<dependencies>
<dependency>
<groupId>com.your.project</groupId>
<artifactId>config</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>