无法使用maven过滤应用程序上下文从eclipse运行服务器

时间:2013-12-07 01:41:26

标签: java eclipse spring maven tomcat

我正在使用基于Eclipse的Spring Tool Suite和我的webapp以及Maven Filtering将数据库凭据注入我的根上下文。

我的pom.xml包含以下内容:

<profiles>
    <profile>
        <id>debug</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <environment.type>debug</environment.type>

            <db.driver>com.mysql.jdbc.Driver</db.driver>
            <db.url>jdbc:mysql://xxx:3306/xxxxx</db.url>
            <db.username>xxxxxx</db.username>
            <db.password>xxxxxx</db.password>

            <tomcat.server>xxxxx</tomcat.server>
            <tomcat.path>/xxxxx</tomcat.path>
            <tomcat.url>http://xxx:8080/manager/text</tomcat.url>
            <tomcat.username>xxx</tomcat.username>
            <tomcat.password>xxx</tomcat.password>
        </properties>
    </profile>
</profiles>
...
<build>
        <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <webResources>
                    <resource>
                        <filtering>true</filtering>
                        <directory>${basedir}/src/main/webapp/WEB-INF</directory>
                        <targetPath>WEB-INF</targetPath>
                        <includes>
                            <include>**/root-context.xml</include>
                        </includes>
                    </resource>
                </webResources>
            </configuration>
    </plugin>
</plugins>
</build>

虽然我的root-context.xml包含以下内容:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
    <property name="driverClass" value="${db.driver}" />
    <property name="jdbcUrl" value="${db.url}" />
    <property name="user" value="${db.username}" />
    <property name="password" value="${db.password}" />
    <property name="maxIdleTime" value="60" />
    <property name="idleConnectionTestPeriod" value="55" />
</bean>

当我部署到Tomcat时,我没有遇到任何问题,同样,当我使用Maven构建它时,我的目标文件夹包含已过滤的root-context.xml。

但是,当我尝试使用“Run on Server ...”从Eclipse运行此webapp时,我收到以下错误:

WARN : com.mchange.v2.c3p0.DriverManagerDataSource - Could not load driverClass ${db.driver}
java.lang.ClassNotFoundException: ${db.driver}

似乎被复制的root-context.xml是未过滤的。有没有办法来解决这个问题?

1 个答案:

答案 0 :(得分:2)

在深入了解其他stackoverflow问题/答案后,答案在Web resources filtering with Maven war plugin does not work in Eclipse with m2e

如果将root-context.xml移动到src / main / resources目录,它将包含在eclipse目标文件夹中并正确运行。

附加的pom.xml片段,使其全部工作。不要忘记为测试过滤测试资源文件夹!

    <build>
    ....
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    <testResources>
        <testResource>
            <directory>src/test/resources</directory>
            <filtering>true</filtering>
        </testResource>
    </testResources>
    ....
    </build>

这就是我必须要做的事情,maven-war-plugin不需要额外的配置,我链接的另一个stackoverflow问题表明你需要。