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
Passing an Argument to MavenMaven will use the value (2.5) passed as an argument to replace the COMMON_VERSION_CMD property set in our pom. xml. This is not limited to the package command — we can pass arguments together with any Maven command, such as install, test, or build.
In maven pom. xml , a property is accessed by using ${property_name} . You can define your custom properties in Maven.
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.
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.
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