Reputation: 1990
I know this is probably a dumb question but I can't figure it out for the life of me. Basically I am using maven to set my dataSource username, password, and driver class name. When I look in the effective Pom.xml it all appears fine as follows
<dataSource.driverClassName>oracle.jdbc.driver.OracleDriver</dataSource.driverClassName>
<dataSource.username>someUsername</dataSource.username>
<dataSource.password>somePassword</dataSource.password>
I am trying to use this information when declaring a spring datasource. The code appears as follows.
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${dataSource.driverClassName}"/>
<property name="url" value="${dataSource.url}"/>
<property name="username" value="${dataSource.username}"/>
<property name="password" value="${dataSource.password}"/>
</bean>
I then pass the datasource into a jdbcTemplate but when I use the template to run sql statements in my code I get an error saying that no driver with the name ${dataSource.driverClassName} can be found. This is obviously because the string constant is being passed rather than the variable. What am I missing?
Thanks
Upvotes: 15
Views: 29365
Reputation: 1087
A simple way to configure the Maven Resources plugin without adding Java classes is to define a resource filter. For the purposes of an example, we'll configure spring-config.xml
using properties defined in pom.xml
.
The pom.xml
file defines a property as follows:
<project>
<properties>
<environment.deploy>local</environment.deploy>
</properties>
...
Then, in the build
section, define the location of resources (files) that will have variables replaced. In the following example, only the file spring-config.xml
will be updated:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>spring-config.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
The syntax for pom.xml
is powerful, and can be used to substitute values within a number of files, such as:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*included_filename.xml</include>
</includes>
</resource>
</resources>
Exclusions are also possible:
<excludes>**/*excluded_filename.xml</excludes>
If only one file needs to have variables applied, set up different resource
s, such as:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>spring-config.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
This will ensure that all files are bundled, but only spring-config.xml
is changed.
Located in src/main/resources
, relative to the project's root directory, the properties loaded by Maven can be used. For example:
<beans ...>
<bean ...>
<property name="deploymentEnvironment">
<list>
<value>${environment.deploy}</value>
</list>
</property>
</bean>
During the build process, Maven replaces the value
above inside the target's spring-config.xml
file:
<beans ...>
<bean ...>
<property name="deploymentEnvironment">
<list>
<value>local</value>
</list>
</property>
</bean>
For the truly adventurous, Maven properties can also be loaded from external files.
Upvotes: 11
Reputation: 3687
I think that you cannot do it in that way, I mean, from pom.xml to spring application context xml.
Put your properties in a property file, something like:
dataSource.username=${dataSource.username}
dataSource.driverClassName=${dataSource.driverClassName}
dataSource.username=${dataSource.username}
dataSource.password=${dataSource.password}
Then, use PropertyConfigurationPlaceholder
to load the property file and make properties availables on spring applicationt context file.
Upvotes: 3
Reputation: 120771
Maven can not transfer any data to the application, because maven works at compile time and the parameter a evaluated at runtime.
But you can use maven filters. To substiture some markers with values while maven is processing the resources.
Upvotes: 2
Reputation: 308753
Spring is expecting to find those values in a .properties file.
Inject a PropertyConfigurationPlaceholder with those values and Spring will find them.
Look for example 3.8.2.1 below:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html
Upvotes: 2