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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With