Configure Datasource using WildFly Maven Plugin

JBoss WildFly uses Java JNDI to configure Datasource information, allowing deployed applications to use this Datasource information to connect and work with the database. If you use JBoss WildFly standalone, you can configure this Datasource information in the standalone.xml file of JBoss WildFly. If you use the WildFly Maven Plugin during development, how will you configure this Datasource information? We will find out together in this tutorial!

Before going into the Datasource configuration, you need to do one more step, which is: to see whether the Datasource information has been configured in the JBoss WildFly server or not, using the Management Console, please configure the user information for WildFly Maven Plugin first. My example is as follows:

Now, we will start configuring Datasource!

First, we need to add the JDBC Driver for the database that your application will use into JBoss WildFly.

In this example, I will use the PostgreSQL database!

To do this, we will need to declare the dependency for the database JDBC Driver that you will use in the project. I will declare the PostgreSQL JDBC Driver as follows:

Next, we will use the WildFly Maven Plugin’s “deploy-artifact” goal to add the JDBC Driver to JBoss WildFly as follows:

We only need to declare the groupId and artifactId of the JDBC Driver, no version is needed! The goal “deploy-artifact” of WildFly Maven Plugin will be executed in the “install” phase, which means when you run “mvn install”!

Now we can use the WildFly Maven Plugin’s “add-resource” goal to add Datasource to JBoss WildFly.

You can add a new execution as follows:

The information in the <address> tag will help us tell WildFly Maven Plugin that we need to add to the “datasources” subsystem, a new Datasource, with information about the database configured in the <properties> tag.

Tag <driver-name>, you declare the driver name we configured above. The <use-ccm> tag is related to the Cache Connection Manager feature of JBoss WildFly. This Datasource information can be used by applications through Java JNDI!

You can declare and use the WildFly Maven Plugin’s “deploy” goal to deploy the application when running the “mvn install” command. For example:

At this point, we have completed configuring Datasource using the WildFly Maven Plugin!

Now, you can run the application with the command:

We need to execute goal “wildfly:start” to start a JBoss WildFly first. Because we must have the JBoss WildFly server started first before we can add the JDBC Driver and configure the Datasource.

After the Maven command finishes running, go to the Management Console page of JBoss WildFly at http://localhost:9990/, then go to the Configuration section, check Subsystems -> Datasources & JDBC Drivers, you will see the results like this:

JDBC Drivers:

Datasources:

So we have successfully configured Datasource using WildFly Maven Plugin!

Add Comment