在 src/main/resources 目录下新建 env.properties 作为配置文件, 并写入
host=@host@2. 加入 profiles 标签
在 pom.xml 中加入以下内容
test true 192.168.123.10 prod 192.168.123.11
注:
- 在打包时通过指定 profile 标签中的 id 标签来切换 properties 中的参数, 如 mvn package -P prod
- 所选 profile 下的参数 host 会传入 env.properties 替换掉 @host@
在 pom.xml 中加入以下代码
4. 读取配置文件参数src/main/resources true env.properties
下面是在 Scala 中读取配置文件的一种方式
import java.util.Properties
object Test {
def main(args: Array[String]): Unit = {
val prop = new Properties()
prop.load(getClass.getClassLoader.getResource("env.properties").openStream())
println(prop.getProperty("host"))
}
}


![[Maven] 通过 pom 文件配置多环境切换 [Maven] 通过 pom 文件配置多环境切换](http://www.mshxw.com/aiimages/31/866663.png)
