无法使用不同的配置文件构建maven项目

时间:2014-10-01 11:23:14

标签: maven properties intellij-idea

我尝试使用不同的配置文件通过maven构建项目。 我存储数据以连接到文件jdbc.properties中的数据库:

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>

1 个答案:

答案 0 :(得分:0)

您必须使用maven-resources-plugin根据配置文件中设置的属性在构建期间自动更新jdbc.properties

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

您必须设置<filtering>true</filtering>以在资源文件中启用属性替换

在使用网络资源时,请尝试将maven-war-plugin配置更改为

<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>