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



我正在尝试使用不同的概要文件通过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

已编辑

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

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

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

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

当您使用网络资源时,请尝试将maven war插件配置更改为

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

最新更新