2014-10-01 125 views
1

我想通过maven使用不同的配置文件来构建项目。 我存储的数据连接到数据库文件jdbc.properties不能使用不同的配置文件构建maven项目

jdbc.driverClassName=com.mysql.jdbc.Driver 
jdbc.dialect=org.hibernate.dialect.MySQLDialect 
jdbc.databaseurl=jdbc:mysql://${db.connectionURL}/shopsstore?useUnicode=yes&characterEncoding=UTF-8 
jdbc.username=${db.username} 
jdbc.password=${db.password} 

这里是我的mvc-dispatcher.xml

<bean id="propertyConfig" 
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
      p:location="WEB-INF/jdbc.properties" /> 
    <bean id="dataSource" 
      class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" 
      p:driverClassName="${jdbc.driverClassName}" 
      p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" 
      p:password="${jdbc.password}" /> 

而且pom.xml

<profiles> 
     <profile> 
      <id>dev</id> 
      <properties> 
       <db.username>root</db.username> 
       <db.password>root</db.password> 
       <db.connectionURL>localhost:3306</db.connectionURL> 
       <db.driverClass>com.mysql.jdbc.Driver</db.driverClass> 
      </properties> 
     </profile> 
     <profile> 
      <id>prod</id> 
      <properties> 
       <db.username>prod-root</db.username> 
       <db.password>prod-admin</db.password> 
       <db.connectionURL>localhost:3306</db.connectionURL> 
       <db.driverClass>com.mysql.jdbc.Driver</db.driverClass> 
      </properties> 
     </profile> 
    </profiles> 

当我运行构建我得到以下异常:

org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSource' defined in ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]: Could not resolve placeholder 'db.password' in string value "${db.password}" 

看起来像属性不可用。任何想法发生了什么? P.S我使用的IntelliJ IDEA

EDITED

<build> 
     <plugins> 
      <plugin> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <configuration> 
        <source>1.5</source> 
        <target>1.5</target> 
       </configuration> 
      </plugin> 
      <plugin> 
       <artifactId>maven-war-plugin</artifactId> 
       <version>2.0</version> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-resources-plugin</artifactId> 
       <version>2.3</version> 
       <configuration> 
        <!-- specify UTF-8, ISO-8859-1 or any other file encoding --> 
        <encoding>UTF-8</encoding> 
       </configuration> 
      </plugin> 
      <!-- Enabling and configuring web resources filtering --> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-war-plugin</artifactId> 
       <version>2.0.2</version> 
       <configuration> 
        <webResources> 
         <resource> 
          <filtering>true</filtering> 
          <directory>src/main/webapp/WEB-INF/</directory> 
         </resource> 
        </webResources> 
       </configuration> 
      </plugin> 
     </plugins> 
     <resources> 
      <resource> 
       <directory>src/main/webapp/WEB-INF/</directory> 
       <filtering>true</filtering> 
      </resource> 
     </resources> 
    </build> 

回答

0

你将不得不使用maven-resources-plugin自动更新基于配置文件中设置的属性生成过程中的jdbc.properties

参考 http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

你必须设置<filtering>true</filtering>使财产置换的资源文件

当你正在使用的网络资源,尝试改变Maven的战争插件配置到

<configuration> 
<webResources> 
    <resource> 
     <directory>${basedir}/src/main/webapp/WEB-INF</directory> 
     <filtering>true</filtering> 
     <targetPath>WEB-INF</targetPath> 
     <includes> 
      <include>**/jdbc.properties</include> 
     </includes> 
    </resource> 
</webResources> 
</configuration> 
+0

谢谢你的答案,我已经更新了我的POM并得到了同样的异常,请参阅上面编辑的问题,这个如何在pom.xml中看起来如何构建部分 – user3127896 2014-10-01 12:09:52

+0

我已经更新了回答。 – coderplus 2014-10-01 12:29:34

相关问题