首页 > 解决方案 > 无法将 Maven 配置文件绑定到 Spring Boot 配置文件

问题描述

我已经看到了有关此问题的所有问题和帖子,因此请不要将其标记为重复或将我转至这些问题,我已尝试实施这些解决方案,但到目前为止没有任何效果。

我有配置文件特定的 application.properties 文件,即application-prod.properties, application-dev.properties, application-int.properties etc

pom.xml

<profiles>
    <profile>
        <id>prod</id>
        <properties>
            <activeProfile>prod</activeProfile>
        </properties>
    </profile>
    <profile>
        <id>int</id>
        <properties>
            <activeProfile>int</activeProfile>
        </properties>
    </profile>
    <profile>
        <id>dev</id>
        <properties>
            <activeProfile>dev</activeProfile>
        </properties>
    </profile>
</profiles>

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    …
</build>
<plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <index>true</index>
                    <manifest>
                        <mainClass>com.demo.Application</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-help-plugin</artifactId>
        <executions>
            <execution>
                <id>show-profiles</id>
                <phase>compile</phase>
                <goals>
                    <goal>active-profiles</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

    </plugins>

应用程序属性

spring.profiles.active=@activeProfile@

我正在做mvn clean install -Pprod然后运行应用程序。我确定 maven 配置文件在构建过程中正在执行,因为我在构建过程中得到了这个

The following profiles are active:

 - prod (source: my-project-snapshot)

这是我在运行应用程序时得到的:

: The following profiles are active: @activeProfile@

任何人都可以在这里帮助我。

更新 当我关闭我的 IDE(STS)并进行 Maven 构建时,它正在工作。任何有关此信息的信息将不胜感激。

标签: springspring-bootmavenspring-profilesmaven-profiles

解决方案


您是否启用了资源过滤pom.xml?由于您使用的是 spring-boot 这可以很容易地启用,在您的pom.xml

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    …
</build>

更新 我已经把你所有的插件都放在一个示例项目中,它pom.xml也与你的 spring-boot 版本相匹配:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://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>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>in.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>

    <profiles>
        <profile>
            <id>prod</id>
            <properties>
                <activeProfile>prod</activeProfile>
            </properties>
        </profile>
        <profile>
            <id>dev</id>
            <properties>
                <activeProfile>dev</activeProfile>
            </properties>
        </profile>
    </profiles>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <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>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-help-plugin</artifactId>
                <executions>
                    <execution>
                        <id>show-profiles</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>active-profiles</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <index>true</index>
                        <manifest>
                            <mainClass>com.example.demo.DemoApplication</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

当我跑

mvn clean package spring-boot:run -Pdev -DskipTests

我在日志文件中看到

com.example.demo.DemoApplication         : The following profiles are active: dev

当我在targetdir...中检查生成的 jar 时,target\demo-0.0.1-SNAPSHOT.jar\BOOT-INF\classes\过滤已经成功,因为application.properties我得到了这条线:

spring.profiles.active=dev

在源代码application.properties中是

spring.profiles.active=@activeProfile@

所以如果你有一个类似于上述过滤的 pom 文件应该可以工作。

在此处输入图像描述


推荐阅读