【问题标题】:Can't build maven project using different profiles无法使用不同的配置文件构建 Maven 项目
【发布时间】:2014-10-01 11:23:14
【问题描述】:

我正在尝试使用不同的配置文件通过 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 properties intellij-idea


    【解决方案1】:

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

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

    您必须设置&lt;filtering&gt;true&lt;/filtering&gt; 才能在资源文件中启用属性替换

    当您使用网络资源时,请尝试将 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>
    

    【讨论】:

    • 感谢您的回答,我已经更新了我的 pom 并遇到了同样的异常,请参阅上面已编辑的问题,这就是 pom.xml 中构建部分的外观
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    相关资源
    最近更新 更多