3

I am using Spring Tool Suite which is Eclipse-based for my webapp along with Maven Filtering to inject the database credentials into my root context.

My pom.xml contains the following:

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

While my root-context.xml contains the following:

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

I get no issues when I deploy to Tomcat, and similarly, when I build it with Maven my target folder contains the filtered root-context.xml.

However, when I attempt to run this webapp from Eclipse with "Run on Server..." I get the following error:

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

It seems the root-context.xml that gets copied over is unfiltered. Is there a way to fix this?

hofan41
  • 1,438
  • 1
  • 11
  • 25
  • I'm sorry I don't quite understand what you mean when you say that, could you provide an example? When I interpret your comment I take it to mean my properties should be encapsulated within a tag, which it already is in the example I pasted. – hofan41 Dec 07 '13 at 03:14
  • Yeah, sorry. I was on my phone and didn't see it. Let me take a closer look. – Sotirios Delimanolis Dec 07 '13 at 04:38

1 Answers1

2

After some digging through other stackoverflow questions/answers, the answer was in Web resources filtering with Maven war plugin does not work in Eclipse with m2e .

If you move root-context.xml to src/main/resources directory, it will be included in the eclipse target folder and run correctly.

Attached piece of pom.xml that made it all work. Don't forget to filter the test resources folder too for your tests!

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

That was all I had to do, there was no additional configuring needed for the maven-war-plugin that the other stackoverflow question I linked to stated you needed.

Community
  • 1
  • 1
hofan41
  • 1,438
  • 1
  • 11
  • 25