IDEA in the configuration file properties

Since the recovery of production rights, problems occur simultaneously: inconsistent test database and a production database, the production test cluster configuration and cluster configuration files are inconsistent and so on, very uncomfortable ,,,
I admit that before the development of non-standard, uh uh uh. . .
Scala jar opted for reading the configuration file encapsulated by:
Here Insert Picture Description
(1) New and pro test subdirectory under the resources directory
new file config.properties

hive.database = test

(2) modify the pom.xml are packaged
new profiles

<profiles>
        <profile>
            <!-- 测试环境 -->
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <!-- 生产环境 -->
            <id>pro</id>
            <properties>
                <profiles.active>pro</profiles.active>
            </properties>
        </profile>
    </profiles>

Under modify build resources

 <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>test/*</exclude>
                    <exclude>pro/*</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources/${profiles.active}</directory>
            </resource>
        </resources>

(3) Packaging class ConfigManager

import java.util.Properties
object ConfigManager {

    def main(args: Array[String]): Unit = {
        println(getConfigValue("hive.database","pro/config.properties"))
    }
    /**
      * 获取配置名称
      */
    def getConfigValue(key : String,path:String="config.properties"): String = {
    val stream =Thread.currentThread.getContextClassLoader.getResourceAsStream(path)
    val prop : Properties= new Properties()
    prop.load(stream)
    prop.getProperty(key)
    }
}

Which is switched by modifying pom.xml test and pro profiles,
local test & play cluster tests are OK, the perfect solution and more environmental problems -

Published 118 original articles · won praise 25 · Views 150,000 +

Guess you like

Origin blog.csdn.net/lhxsir/article/details/90903903