使用配置文件启动springboot

时间:2017-03-13 15:09:09

标签: java spring maven spring-boot spring-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文件但是,当我启动应用程序时,我无法访问正确的文件:基本上我不知道在我的运行/调试配置中设置所有者和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 始终为空

有任何线索吗?

安德烈

1 个答案:

答案 0 :(得分:0)

在IntelliJ IDEA中,我只需在PHR或报告模块设置中将必要的config / {client} / {environment}文件夹指定为“Resources”,在这种情况下为hc / local:项目结构 - &gt;模块 - &gt; YourApplicationName - &gt;来源 - &gt;资源并选择适当的文件。

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

创建,管理和运行配置 - &gt; ClassYouWantToRun * - &gt; ClassPath - &gt;用户条目 - &gt;高级 - &gt;添加文件夹并选择配置文件夹。

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

相关问题