0

我正在尝试使用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>

占位符已正确解析。

这个配置有什么问题?

4

2 回答 2

2

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

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

<version>然后,您还应该从其他 Spring Boot 依赖项中删除该声明。资源配置也是多余的,所以我建议也删除它。总而言之,这将使您的 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>
于 2017-12-04T17:32:18.147 回答
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>

应用程序属性:

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

最后,最重要的一步:

在 STS 中,您需要右键单击项目 -> 属性 -> Maven 选项卡

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

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

于 2017-12-06T08:49:05.560 回答