使用配置文件启动 springboot

安德里亚·吉拉迪

几个月前,我们的一位开发人员离开了团队,由于通知时间很短,我们无法完成 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>

每个 config 文件夹都包含正确的 env.config 文件,但是,当我启动应用程序时,我无法访问正确的文件:基本上我不知道在我的运行/调试配置中在哪里设置所有者和 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} 文件夹指定为“Resources”,在这种情况下为 hc/local:项目结构 -> 模块 -> YourApplicationName -> 来源 ->资源并选择正确的文件。

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章