Spring Boot:使用Maven和特定Maven配置文件的自动属性扩展不起作用

时间:2017-12-04 14:58:55

标签: maven spring-boot-configuration spring-boot-starter

我正在尝试使用文档中指定的72.1.1 Automatic property expansion using Maven使用不同的maven配置文件,但我无法使其与特定配置文件一起使用

我有以下 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <profiles>
        <profile>
            <id>local</id>

            <activation>
                <property>
                    <name>profileName</name>
                    <value>local</value>
                </property>
            </activation>
            <properties>
                <wildcard.for.customproperty>valueA</wildcard.for.customproperty>
            </properties>
        </profile>

        <profile>
            <id>external</id>
            <activation>
                <property>
                    <name>profileName</name>
                    <value>external</value>
                </property>
            </activation>
            <properties>
                <wildcard.for.customproperty>valueB</wildcard.for.customproperty>
            </properties>
        </profile>
    </profiles>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.9.RELEASE</version>
            <type>pom</type>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.9.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>1.5.9.RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

这非常简单 application.properties

custom.property=@wildcard.for.customproperty@

我使用Spring Tool Suite运行maven构建,具有以下目标:

clean install -DprofileName=local

构建SUCCESS,但目标文件夹中的application.properties仍然包含未解析的@...@占位符。

如果我在两个配置文件之一的pom.xml中添加这个简单的行:

<activeByDefault>true</activeByDefault>

占位符已正确解析。

此配置有什么问题?

2 个答案:

答案 0 :(得分:2)

您已将spring-boot-starter-parent声明为项目的依赖项,而不是将其用作项目的父项。它应该像这样声明:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
</parent>

然后,您还应该从其他Spring Boot依赖项中删除<version>声明。资源配置也是多余的,所以我建议删除它。总而言之,这将使您的pom看起来类似于以下内容:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>

    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <profiles>
        <profile>
            <id>local</id>

            <activation>
                <property>
                    <name>profileName</name>
                    <value>local</value>
                </property>
            </activation>
            <properties>
                <wildcard.for.customproperty>valueA</wildcard.for.customproperty>
            </properties>
        </profile>

        <profile>
            <id>external</id>
            <activation>
                <property>
                    <name>profileName</name>
                    <value>external</value>
                </property>
            </activation>
            <properties>
                <wildcard.for.customproperty>valueB</wildcard.for.customproperty>
            </properties>
        </profile>
    </profiles>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

答案 1 :(得分:0)

我已经解决了以下配置:

的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <profiles>
        <profile>
            <id>local</id>

            <activation>
                <property>
                    <name>profileName</name>
                    <value>local</value>
                </property>
            </activation>
            <properties>
                <wildcard.for.customproperty>valueA</wildcard.for.customproperty>
                <activatedProperties>local</activatedProperties>
            </properties>
            <build>
                <finalName>myBuild-local</finalName>
            </build>
        </profile>

        <profile>
            <id>external</id>
            <activation>
                <property>
                    <name>profileName</name>
                    <value>external</value>
                </property>
            </activation>
            <properties>
                <wildcard.for.customproperty>valueB</wildcard.for.customproperty>
                <activatedProperties>external</activatedProperties>
            </properties>
            <build>
                <finalName>myBuild-external</finalName>
            </build>
        </profile>
    </profiles>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.9.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>1.5.9.RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <delimiters>
                        <delimiter>@{*}</delimiter>
                    </delimiters>
                    <useDefaultDelimiters>false</useDefaultDelimiters>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

application.properties:

custom.property=@{wildcard.for.customproperty}
spring.profiles.active=@{activatedProperties}

最后,最重要的一步:

在STS中,你需要在项目中使用clic - &gt;属性 - &gt; Maven标签

您需要指定有效的个人资料。

由于某些原因,即使您将-PprofileName或配置文件直接放在Maven构建的配置文件字段中,STS也不会应用该配置文件。