javapostgresqljbosswildfly

How to define datasource properties for WildFly bootable JAR without openshift CLI?


So normally you could use the standalone.xml to do this, but the wildfly bootable JAR seems not to have a standalone.xml since it's all within a single JAR.

The examples that JBoss provides assume you'll only ever use OpenShift for some reason and uses some arcane OpenShift CLI command (below) that just somehow creates the right file in the right spot. https://github.com/wildfly-extras/wildfly-jar-maven-plugin/tree/4.0.0.Final/examples/postgresql

oc new-app --name database-server \
  --env POSTGRESQL_USER=admin \
  --env POSTGRESQL_PASSWORD=admin \
  --env POSTGRESQL_DATABASE=sampledb \
  postgresql

However, there is no config file created with that command (or they didnt check it in), and the documentation doesn't say anything about how to do the same for non-OpenShift projects.

Trying to find any info on how to do configure a (postgres) data-source for a non-OpenShift deployment.


Solution

  • Figured this out on my own with some experimentation. WildFly documentation on bootable jars is still really minimal and lacking lots of detail that required lots of guessing / experimenting.

    While there is an overlay that lets you specify DB info via environment variables, that's a bit hacky and doesn't allow you to define more than one datasource nor can you specify the JNDI name. Instead, I used a CLI script which gets fed into the jar builder plugin.

    datasource.cli

    data-source add --name=<name> --jndi-name=java:jboss/datasources/<schema> --driver-name=postgresql --connection-url=jdbc:postgresql://localhost:5432/<db> --user-name=<user> --password=<pass>
    

    Make sure to use your own values for placeholders <name>, <schema>, <db>, <user>, <pass> and swap out the hostname / port if needed.

    pom.xml (snippet)

    <configuration>
        <cli-sessions>
            <cli-session>
                <script-files>
                    <script>scripts/datasource.cli</script>
                </script-files>
                <resolve-expressions>true</resolve-expressions>
            </cli-session>
        </cli-sessions>
        <feature-packs>
            <feature-pack>
                <location>wildfly@maven(org.jboss.universe:community-universe)#23.0.0.Final</location>
            </feature-pack>
            <feature-pack>
                <groupId>org.wildfly</groupId>
                <artifactId>wildfly-datasources-galleon-pack</artifactId>
                <version>1.2.2.Final</version>
            </feature-pack>
        </feature-packs>
        <layers>
            <layer>jaxrs-server</layer>
            <layer>postgresql-driver</layer>
        </layers>
    </configuration>
    

    In the above XML config, items of note are

    1. <script> value tells the builder where to find the CLI script that will do the datasource adding
    2. <feature-pack> for the datasource pack. Though I didn't test if this was required... maybe the driver layer is all that's needed. Worth trying if you have time.
    3. <layer> that specifies the postgresql-driver value. This is required and without it, the bootup will complain about missing a driver when the CLI script runs.