使用配置文件启动 spring boot

Start springboot with profiles

我们的一位开发人员几个月前离开了团队,由于临时通知我们无法完成 KT。所以,现在,我有一个无法启动的项目(当然是使用较少的组件,但今天,当然,它已经变得至关重要,你知道的,嗯......)。

所以,基本上它是一个 Spring-boot 应用程序,这就是我们在 pom.xml 配置中的内容:

    <build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <testSourceDirectory>test/main/java</testSourceDirectory>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>${java-version}</source>
                <target>${java-version}</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>assembly</id> <!-- this is used for inheritance merges -->
                    <phase>package</phase> <!-- bind to the packaging phase -->
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <finalName>
                            reporting-${owner}-${env}
                        </finalName>
                        <descriptors>
                            <descriptor>reporting-assembly.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<profiles>
    <profile>
        <id>local</id>
        <activation>
            <property>
                <name>env</name>
                <value>local</value>
            </property>
        </activation>
        <properties>
            <config.path>src/main/resources/config/${owner}/local</config.path>
        </properties>
    </profile>
    <profile>
        <id>qa</id>
        <activation>
            <property>
                <name>env</name>
                <value>qa</value>
            </property>
        </activation>
        <properties>
            <config.path>src/main/resources/config/${owner}/qa</config.path>
        </properties>
    </profile>
    <profile>
        <id>staging</id>
        <activation>
            <property>
                <name>env</name>
                <value>staging</value>
            </property>
        </activation>
        <properties>
            <config.path>src/main/resources/config/${owner}/staging</config.path>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <activation>
            <property>
                <name>env</name>
                <value>prod</value>
            </property>
        </activation>
        <properties>
            <config.path>src/main/resources/config/${owner}/prod</config.path>
        </properties>
    </profile>
</profiles>

每个配置文件夹都包含正确的 env.config 文件,但是,当我启动应用程序时,我无法访问正确的文件:基本上我不知道在我的 Run/Debug 配置 owner 和 env 属性。

即使我设置了环境变量 ${env} 和 ${owner}(在调试配置上),我仍然在这里得到空值:

public static <T> T loadJsonResourceIntoClass(String jsonResourcePath, Class<T> clazz) throws Exception {
    URL jsonFilePathUrl = JsonUtil.class.getClassLoader().getResource(jsonResourcePath);
    return objectMapper.readValue(jsonFilePathUrl != null ? jsonFilePathUrl.openStream() : null, clazz);
}

jsonFilePathUrl 始终为空

有线索吗?

安德里亚

在 IntelliJ IDEA 中,我只是在 PHR 或报告模块设置中将必要的 config/{client}/{environment} 文件夹指定为“资源”,hc/local 在这种情况下:项目结构-> 模块 -> YourApplicationName -> 来源 -> 资源和 select 正确的文件。

在 Eclipse 中,同样可以在 运行 配置的帮助下完成:

创建、管理和 运行 配置 -> ClassYouWantTo运行* -> 类路径 -> 用户条目 -> 高级 -> 添加文件夹并选择配置文件夹。

我还在 IDE 上激活了正确的 maven 配置文件。