jdbc - Inside mybatis-config.xml, is possible to read properties from pom.xml in a Maven project? -
i'm developing sample multi-module maven project uses mybatis. dependencies have own modules , mybatis self.
on persistence layer, have created following on pom.xml
:
<properties> <!-- jdbc --> <jdbc.url>jdbc:postgresql://localhost:5432/kpi?autoreconnect=true</jdbc.url> <jdbc.driverclassname>org.postgresql.driver</jdbc.driverclassname> <jdbc.username>postgres</jdbc.username> <jdbc.password>postgres</jdbc.password> <jdbc.initconnections>15</jdbc.initconnections> <jdbc.maxactive>40</jdbc.maxactive> <jdbc.maxidle>5</jdbc.maxidle> </properties>
then, in mybatis-config.xml
inside src/main/resources
, did following:
<environments default='development'> <environment id='development'> <transactionmanager type='jdbc'/> <datasource type='pooled'> <property name='driver' value="${jdbc.driverclassname}"/> <property name='url' value="${jdbc.url}"/> <property name='username' value="${jdbc.username}"/> <property name='password' value="${jdbc.password}"/> </datasource> </environment>
after all, when try run application following error:
org.apache.ibatis.exceptions.persistenceexception: ### error querying database. cause: java.sql.sqlexception: error setting driver on unpooleddatasource. cause: java.lang.classnotfoundexception: cannot find class: ${jdbc.driverclassname}
but, when change above code, explicitly typing information jdbc, works:
<environments default='development'> <environment id='development'> <transactionmanager type='jdbc'/> <datasource type='pooled'> <property name='driver' value="org.postgresql.driver"/> <property name='url' value="jdbc:postgresql://localhost:5432/kpi?autoreconnect=true"/> <property name='username' value="postgres"/> <property name='password' value="postgres"/> </datasource> </environment> </environments>
so, possible make works using information put inside pom.xml
? tried using <properties resource='pom.xml'/>
tag no success.
thanks in advance.
first, technically cannot read properties in pom when mybatis run, because pom used in compilation, , when mybatis run, runtime has passed compilation. therefore not reasonable read data pom (unless mybatis config part of automated testing in build process, seems not in case)
you can achieve similar effect using maven's resource filtering mechanism, placeholders in mybatis-config.xml
replaced properties defined in maven during build process.
http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
you may have settings in pom stop filtering on resources directory caused problem. please check if properties replaced in jar/war after have built application.
however, not suggest doing so. such kind of properties environment specific , shouldn't "hardcoded" in result artifact. should consider updating design, can provide config file or provide command line argument when running application, not need build app again cope environment.
Comments
Post a Comment