tomcat7-maven-plugin无法在类路径上找到属性文件

时间:2013-09-18 01:00:32

标签: maven tomcat7 integration-testing

我正在尝试使用“tomcat7-maven-plugin”对war文件运行集成测试。当战争被部署时,它基本上会加载spring applicaitoncontext,后者又会在类路径上预期属性文件。在常规的tomcat实例中,我将属性文件保存在我的机器上,然后配置catalina.properties - > common.loader指向该目录。但我不知道如何使用这个插件实现这一点。

这是我试过的插件配置:

<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>

1 个答案:

答案 0 :(得分:6)

我们遇到了同样的问题。我为此提交了一份JIRA(MTOMCAT-246)。事实证明它只适用于tomcat7:run目标,而不是仅用于run-war。他们计划在3.0版本中修复它。我们有一个解决方法,见下文。

实现它的一种方法是将属性文件打包为jar文件,并将它们包含在插件的依赖项中。

在我们的例子中,这意味着在父项目的配置文件夹中创建一个maven模块(存储各种环境的配置):

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <descriptors>
                    <descriptor>distribution.xml</descriptor>
                </descriptors>
            </configuration>
        </plugin>
    </plugins>

    <resources>
        <resource>
            <!-- add property replacement to state environment name -->
            <directory>build</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
</build>

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>

然后在插件部分:

<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>

您添加了包含这些属性文件的config.jar文件的链接。