Home Docs Examples Source code Issues

Examples

Table of contents

  1. Common preparation
  2. Examples
    1. Process configs with default project layout
    2. Process configs with non-default configs location
    3. Process configs with non-default templates location
    4. Process configs with environment-specific configs
    5. Process configs with environment-specific configs defined in command line
    6. Process configs with configs specified in command line
    7. Clean configs with non-default output directory

Common preparation

Warning

To remove text redundancy same templates will be used for almost every example.
Please read details below

someXMLConfig.xml will contain following:

<DBConfig>
  <dbHost>%{DB_HOST}</dbHost>
  <dbPort>%{DB_PORT}</dbPort>
  <dbUserName>%{DB_USER_NAME}</dbUserName>
  <dbUserPass>%{DB_USER_PASS}</dbUserPass>
</DBConfig>

config.properties will hold following:

#Mon Mar 11 23:26:18 EET 2019
APP_API_KEY=%{APP_API_KEY}
DB_PORT=%{DB_PORT}
DB_USER_NAME=%{DB_USER_NAME}
SOME_OTHER_API_KEY=%{APP_API_SECRET_KEY}
DB_HOST=%{DB_HOST}

And config.json defines following:

{
  "appPublicAPIKey":"%{APP_API_KEY}",
  "appPrivateAPIKey":"%{APP_API_SECRET_KEY}"
}

Examples

Process configs with default project layout

1. Files structure:

project
├─ templates
│  ├─ inner
│  │  └─ config.json
│  ├─ config.properties
│  └─ someXMLConfig.xml
├─ configs
└─ build.gradle

2. Your build.gradle:

plugins {
  id 'info.gconfig' version '1.0.1'
}

3. configs:

DB_HOST=example.com
DB_PORT=3306
DB_USER_NAME=root
DB_USER_PASS=password

APP_API_KEY=123456
APP_API_SECRET_KEY=secret_123456

4. Usage:

gradle processConfigs

5. Results:

<DBConfig>
  <dbHost>example.com</dbHost>
  <dbPort>3306</dbPort>
  <dbUserName>root</dbUserName>
  <dbUserPass>password</dbUserPass>
</DBConfig>

#Mon Mar 11 23:26:18 EET 2019
APP_API_KEY=123456
DB_PORT=3306
DB_USER_NAME=root
SOME_OTHER_API_KEY=secret_123456
DB_HOST=example.com

{
  "appPublicAPIKey":"123456",
  "appPrivateAPIKey":"secret_123456"
}

Process configs with non-default configs location

1. Files structure:

project
├─ templates
│  ├─ inner
│  │  └─ config.json
│  ├─ config.properties
│  └─ someXMLConfig.xml
├─ configs_part1
├─ configs_part2
└─ build.gradle

2. Your build.gradle:

plugins {
  id 'info.gconfig' version '1.0.1'
}

gconfig {
  configs = files('configs_part1', 'configs_part2')
}

3. configs_part1:

DB_HOST=example.com
DB_PORT=3306
DB_USER_NAME=root

4. configs_part2:

DB_USER_PASS=password
APP_API_KEY=123456
APP_API_SECRET_KEY=secret_123456

5. Usage:

gradle processConfigs

6. Results:

<DBConfig>
  <dbHost>example.com</dbHost>
  <dbPort>3306</dbPort>
  <dbUserName>root</dbUserName>
  <dbUserPass>password</dbUserPass>
</DBConfig>

#Mon Mar 11 23:26:18 EET 2019
APP_API_KEY=123456
DB_PORT=3306
DB_USER_NAME=root
SOME_OTHER_API_KEY=secret_123456
DB_HOST=example.com

{
  "appPublicAPIKey":"123456",
  "appPrivateAPIKey":"secret_123456"
}

Process configs with non-default templates location

1. Files structure:

project
├─ awesome_templates
│  ├─ inner
│  │  └─ config.json
│  ├─ config.properties
│  └─ someXMLConfig.xml
├─ configs
└─ build.gradle

2. Your build.gradle:

plugins {
  id 'info.gconfig' version '1.0.1'
}

gconfig {
  configTemplates = file('awesome_templates')
}

3. configs:

DB_HOST=example.com
DB_PORT=3306
DB_USER_NAME=root
DB_USER_PASS=password

APP_API_KEY=123456
APP_API_SECRET_KEY=secret_123456

4. Usage:

gradle processConfigs

5. Results:

<DBConfig>
  <dbHost>example.com</dbHost>
  <dbPort>3306</dbPort>
  <dbUserName>root</dbUserName>
  <dbUserPass>password</dbUserPass>
</DBConfig>

#Mon Mar 11 23:26:18 EET 2019
APP_API_KEY=123456
DB_PORT=3306
DB_USER_NAME=root
SOME_OTHER_API_KEY=secret_123456
DB_HOST=example.com

{
  "appPublicAPIKey":"123456",
  "appPrivateAPIKey":"secret_123456"
}

Process configs with environment-specific configs

1. Files structure:

project
├─ templates
│  ├─ inner
│  │  └─ config.json
│  ├─ config.properties
│  └─ someXMLConfig.xml
├─ appConfig
├─ appConfig.local
├─ dbConfig
├─ dbConfig.local
└─ build.gradle

2. Your build.gradle:

plugins {
  id 'info.gconfig' version '1.0.1'
}

gconfig {
  configs = files('dbConfig', 'appConfig')
  environment = 'local'
}

3. Configs:

3.1. appConfig:

APP_API_KEY=123456
APP_API_SECRET_KEY=secret_123456

3.2. appConfig.local:

APP_API_SECRET_KEY=another_secret_123456

3.3. dbConfig

DB_HOST=example.com
DB_PORT=3306
DB_USER_NAME=root
DB_USER_PASS=password

3.4. dbConfig.local:

DB_HOST=localhost
DB_USER_NAME=some_local_db_user

4. Usage:

gradle processConfigs

5. Results:

<DBConfig>
  <dbHost>localhost</dbHost>
  <dbPort>3306</dbPort>
  <dbUserName>some_local_db_user</dbUserName>
  <dbUserPass>password</dbUserPass>
</DBConfig>

#Mon Mar 11 23:26:18 EET 2019
APP_API_KEY=123456
DB_PORT=3306
DB_USER_NAME=some_local_db_user
SOME_OTHER_API_KEY=another_secret_123456
DB_HOST=localhost

{
  "appPublicAPIKey":"123456",
  "appPrivateAPIKey":"another_secret_123456"
}

Process configs with environment-specific configs defined in command line

1. Files structure:

project
├─ templates
│  ├─ inner
│  │  └─ config.json
│  ├─ config.properties
│  └─ someXMLConfig.xml
├─ appConfig
├─ appConfig.local
├─ dbConfig
├─ dbConfig.local
└─ build.gradle

2. Your build.gradle:

plugins {
  id 'info.gconfig' version '1.0.1'
}

gconfig {
  configs = files('dbConfig', 'appConfig')
}

3. Configs:

3.1. appConfig:

APP_API_KEY=123456
APP_API_SECRET_KEY=secret_123456

3.2. appConfig.local:

APP_API_SECRET_KEY=another_secret_123456

3.3. dbConfig:

DB_HOST=example.com
DB_PORT=3306
DB_USER_NAME=root
DB_USER_PASS=password

3.4. dbConfig.local:

DB_HOST=localhost
DB_USER_NAME=some_local_db_user

4. Usage:

gradle processConfigs -Pgc.env=local

5. Results:

<DBConfig>
  <dbHost>localhost</dbHost>
  <dbPort>3306</dbPort>
  <dbUserName>some_local_db_user</dbUserName>
  <dbUserPass>password</dbUserPass>
</DBConfig>

#Mon Mar 11 23:26:18 EET 2019
APP_API_KEY=123456
DB_PORT=3306
DB_USER_NAME=some_local_db_user
SOME_OTHER_API_KEY=another_secret_123456
DB_HOST=localhost

{
  "appPublicAPIKey":"123456",
  "appPrivateAPIKey":"another_secret_123456"
}

Process configs with configs specified in command line

1. Files structure:

project
├─ templates
│  ├─ inner
│  │  └─ config.json
│  ├─ config.properties
│  └─ someXMLConfig.xml
├─ appConfig
├─ appConfig.local
├─ dbConfig
├─ dbConfig.local
└─ build.gradle

2. Your build.gradle:

plugins {
  id 'info.gconfig' version '1.0.1'
}

gconfig {
  configs = files('dbConfig', 'appConfig')
}

3. Configs:

3.1. appConfig:

APP_API_KEY=123456
APP_API_SECRET_KEY=secret_123456

3.2. appConfig.local:

APP_API_SECRET_KEY=another_secret_123456

3.3. dbConfig:

DB_HOST=example.com
DB_PORT=3306
DB_USER_NAME=root
DB_USER_PASS=password

3.4. dbConfig.local:

DB_HOST=localhost
DB_USER_NAME=some_local_db_user

4. Usage:

gradle processConfigs
  -Pgc.env=local
  -Pgc.prop.DB_USER_NAME=overriddenUser
  -Pgc.prop.APP_API_KEY=9876543

5. Results:

<DBConfig>
  <dbHost>localhost</dbHost>
  <dbPort>3306</dbPort>
  <dbUserName>overriddenUser</dbUserName>
  <dbUserPass>password</dbUserPass>
</DBConfig>

#Mon Mar 11 23:26:18 EET 2019
APP_API_KEY=9876543
DB_PORT=3306
DB_USER_NAME=overriddenUser
SOME_OTHER_API_KEY=another_secret_123456
DB_HOST=localhost

{
  "appPublicAPIKey":"9876543",
  "appPrivateAPIKey":"another_secret_123456"
}

Clean configs with non-default output directory

1. Files structure:

project
├─ configs
│  └─ innerConfigsDir
│     ├─ someConfig
│     │  └─ test1.xml
|     └─ config2.json
└─ build.gradle

2. Your build.gradle:

plugins {
  id 'info.gconfig' version '1.0.1'
}

gconfig {
  outputDir = file('configs/innerConfigsDir')
}

3. Usage:

gradle cleanConfigs