【问题标题】:Spring Boot: Automatic property expansion using Maven and specific Maven profiles is not workingSpring Boot:使用 Maven 和特定 Maven 配置文件的自动属性扩展不起作用
【发布时间】:2018-05-18 01:58:42
【问题描述】:

我正在尝试使用文档中指定的 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

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

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

<activeByDefault>true</activeByDefault>

占位符已正确解析。

这个配置有什么问题?

【问题讨论】:

  • 不是用 -Pprofilename 激活了 maven 配置文件吗?
  • 配置文件已正确激活,使用“help:active-profiles”目标和 功能进行检查。

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


【解决方案1】:

您已将 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 依赖项中删除 &lt;version&gt; 声明。资源配置也是多余的,所以我建议也删除它。总而言之,这将使您的 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)我已经尝试过这个解决方案,结果是一样的。我需要将 标记设置为 true。 2)如果我使用的项目已经使用了父项目,如何将spring-boot-starter-parent设置为项目的父项目?
  • 你不能。您在问题中链接到的文档已经解释了如果您不使用 spring-boot-starter-parent 作为项目的父级所需的配置。
  • 我已经以另一种方式解决了,请参阅下面的答案。 STS 有问题。
【解决方案2】:

我已经解决了以下配置:

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 中,您需要右键单击项目 -> 属性 -> Maven 选项卡

您需要在此处指定活动配置文件。

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

【讨论】:

    猜你喜欢
    • 2017-11-07
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 2019-06-28
    相关资源
    最近更新 更多