WildFly CLI: How to use properties from a file

In this tutorial we will learn how to use a property file to define the variables that you can use in your WildFly CLI scripts.

Firstly, we recommend checking this tutorial to learn how to use variables in the CLI: How to use variables in WildFly CLI

Now that you know how to use variables, let’s see how to keep these variables in a property file. The tweak is to use the –properties argument when you are launching the CLI. See the following example:

./jboss-cli.sh -c --properties=file.properties --file=script.cli

A practical example

Next, let’s see in practice how to use it to create a Datasource. You have to prepare two files: the file createDataSource.cli which will be a general purpose script fit for any database:

set jdbcUrl=${jdbcUrl}  
set username=${username}  
set password=${password}  
set driver=${driver}  
set module=${module}  
set resource=${resource}
set jndiname=${jndiname}
set name=${name}
set resource=${resource}


module add --name=$module --resources=$resource --dependencies=javax.api,javax.transaction.api
 
/subsystem=datasources/jdbc-driver=$driver:add(driver-name=$driver,driver-module-name=$module)

data-source add --jndi-name=$jndiname --name=$name --connection-url=$jdbcUrl --driver-name=$driver --user-name=$username --password=$password

reload

As you can see, no reference is included about the specific database JDBC settings or driver. And now let’s include the above properties in a configuration file named mysql.properties:

jdbcUrl=jdbc:mysql://localhost:3306/as7development  
username=jboss  
password=jboss  
driver=mysql  
module=com.mysql  
resource=/home/jboss/Downloads/mysql-connector-java-5.1.24-bin.jar
name=MySQLPool
jndiname=java:/MySqlDS

Adapt the above properties to your need. Once done, execute from the shell:

./jboss-cli.sh -c --properties=mysql.properties --file=createDataSource.cli

Now switching to another DB is just a matter of updating the properties. Here are the properties you can use, for example, with PostgreSQL:

jdbcUrl=jdbc:postgresql://localhost/postgres  
username=jboss  
password=jboss  
driver=postgresql  
module=com.postgresql  
resource=/home/jboss/Downloads/postgresql-9.3-1101.jdbc41.jar
name=PostgreSQLPool
jndiname=java:/PostgreDS

Using Environment variables in the Property file

If you want to use environment variables in the property file you can do that as follows. Declare in your property file the variables as system properties. Sample cli.properties file:

${PROPERTY_NAME}=foo

Your CLI script (example.cli) will use the above variable to build the command:

/system-property=${PROPERTY_NAME}:add(value="bar")

Finally, in order to inject the actual environment variable, use the -D flag when running the CLI. Example:

$ export PROPERTY_NAME=foo
$ ./jboss-cli.sh -c --file=example.cli --properties=cli.properties -DPROPERTY_NAME=$PROPERTY_NAME
Found the article helpful? if so please follow us on Socials