Home
This is simple template processing Gradle plugin. Useful for managing project configurations.
In almost every software development project there is a list of configurations (or settings) that are required for normal project functioning. This could be database connection properties, location of some templates or API keys for third-party services.
In most cases, list of these settings is same for all environments and only values of these settings
vary from environment to environment. For example, database host for QA environment may look
like qa.example.com
but for UAT environment it may look like uat.example.com
.
Usually, every software developer is responsible for updating these configurations for a needed environment. And, unfortunately, all developers make same mistake - forget to replace required configuration and, as a result, SQL scripts are applied on wrong database or some production API keys are used instead of testing API keys.
This plugin is created to minimize the chances of such mistakes.
Take a look at following example.
Here is the example of context.xml
for setting up DataSource for
Apache Tomcat:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/ExampleDB"
url="jdbc:mysql://example.com:3306/db_name"
username="root"
password="root_password"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.cj.jdbc.Driver"
maxWaitMillis="5000"
testOnBorrow="true"
validationQuery="SELECT 1"/>
</Context>
In most cases, this config will be in same shape and only url
, username
and
password
will be different for each environment.
We can replace hardcoded values with template tokens:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/ExampleDB"
url="jdbc:mysql://%{DB_HOST}:%{DB_PORT}/db_name"
username="%{DB_USER_NAME}"
password="%{DB_USER_PASSWORD}"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.cj.jdbc.Driver"
maxWaitMillis="5000"
testOnBorrow="true"
validationQuery="SELECT 1"/>
</Context>
Create generic configs template dbconfig
:
DB_HOST=
DB_PORT=
DB_USER_NAME=
DB_USER_PASS=
Make a copy of generic configs template dbconfig.local
and fill values based on actual environment
values:
DB_HOST=localhost
DB_PORT=3306
DB_USER_NAME=root
DB_USER_PASS=password
Add following to build.gradle
:
plugins {
id 'info.gconfig'
}
gconfig {
configs = files('dbconfig')
environment = 'local'
}
And execute processConfigs
task. At the end you'll get ready-to-use context.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/ExampleDB"
url="jdbc:mysql://localhost:3306/db_name"
username="root"
password="password"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.cj.jdbc.Driver"
maxWaitMillis="5000"
testOnBorrow="true"
validationQuery="SELECT 1"/>
</Context>
You got the idea. Please check documentation and examples