Solon v3.0.3

三、应用配置引用 maven、gradle 里的变量

</> markdown

这是编译工具的功能。通过 resources 处理配置,添加需要被渲染的资源:

1、配置参考

  • maven
<properties>
    <demo.env>local</demo.env>
</properties>
    
<build>
   <resources>
       <resource>
          <directory>src/main/resources</directory>
          <filtering>true</filtering>
          <includes>
              <include>*.yml</include>
          </includes>
       </resource>
       <resource>
          <directory>src/main/resources</directory>
          <filtering>false</filtering>
          <excludes>
              <exclude>*.yml</exclude>
          </excludes>
       </resource>
    </resources>
</build>

当有 "resource" 配置时,需要把所有资源都包括进去。

  • gradle
ext {
    demo_env = "local"
}

import org.apache.tools.ant.filters.ReplaceTokens
processResources {
    filesMatching('app.yml') {
        def tokens = [:]
        project.properties.each { key, value ->
            if (value != null) {
                tokens[key] = value.toString()
            }
        }

        filter(ReplaceTokens, tokens: tokens)
    }
}

2、引用方式

  • yaml
solon.env: @demo.env@
  • properties
solon.env=@demo.env@