Maven 3个不同的配置文件 - Spring propertyPlaceHolderConfig不能使用pom文件中的值

Nit*_*tro 3 spring maven-3

我在使用maven 3时遇到了一些问题并且正在加载正确的.properties文件.

我想要实现的目标如下:使用mvn -Plocal我想加载setting-local.properties,如果使用prod运行我想加载settings-prod.properties.

它通过使用mvn -Denv = local工作,但是当我尝试使用-Plocal时,未加载变量(settings - $ {env} .properties不存在).

我的pom.xml:

<profiles>
    <profile>
        <id>local</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <env>local</env>
        </properties>
    </profile>
</profiles>
Run Code Online (Sandbox Code Playgroud)

在我的applicationcontext中,我想加载env-variable:

<bean id="propertyPlaceholderConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
                <list>
                        <value>classpath:settings-${env}.properties
                        </value>
                </list>
        </property>
        <property name="ignoreUnresolvablePlaceholders">
                <value>true</value>
        </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

那么问题是什么,它不应该双向工作吗?

Mar*_*ren 5

我认为你将Spring的PropertyPlaceholderConfigurerMaven的过滤机制混淆了.这些是相似但完全独立的机制(但它们可以一起使用).

  • Spring的PropertyPlaceholderConfigurer允许您从属性文件中获取值,以便在Spring应用程序上下文中使用.

  • Maven的过滤允许您使用Maven属性和环境中的值替换文本文件(包括属性文件)中的值.

你可以将它们组合起来然后它变成一个两阶段的过程.您的Maven构建使用过滤将值放在属性文件中,然后Spring可以读取它. 困惑?(我是)