【问题标题】:How to use Maven Profile Name as a variable如何使用 Maven 配置文件名称作为变量
【发布时间】:2016-01-06 20:15:27
【问题描述】:

我需要构建一个可以根据配置文件名称调用 JDBC 连接的 war 文件。

当我使用 dev 配置文件构建时,我需要它来调用 dev.properties 文件。当我使用 prod 配置文件构建时,我需要从 prod.properties 文件中读取它。

我想出的想法是以某种方式使用配置文件名称作为变量,但我不知道该怎么做。

POM.XML

<profile>
    <id>dev</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
        <profile-id>dev</profile-id>
    </properties>
</profile>
<profile>
    <id>prod</id>
    <properties>
        <profile-id>prod</profile-id>
    </properties>
</profile>

servlet-context.xml

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:${profile-id}.properties</value>
    </property>
</bean>
<property name="dataSourceProperties">
    <props>                 
        <prop key="url" >${jdbc}</prop>
        <prop key="user">user</prop>
        <prop key="password">password</prop>    
        <prop key="implicitCachingEnabled">true</prop>          
    </props>
</property>

【问题讨论】:

    标签: java maven profiles


    【解决方案1】:

    你必须使用maven的过滤概念。见http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

    在你的 pom 中添加如下内容:

      <resource>
        <directory>directory.to.servlet/servlet-context.xml</directory>
        <filtering>true</filtering>
      </resource>
    

    通过将 directory.to.servlet 替换为真实目录

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-11
      • 2014-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-08
      • 1970-01-01
      相关资源
      最近更新 更多