0

How to set the value of a jdbc-connection-pool property in glassfish-resources.xml with a property from pom.

For example, my pom.xml

...
<profiles>
  <profile>
    <id>dev</id>
      <properties>
        <database.dbname>Xpto</database.dbname>
        ...
      </properties>
    </profile>
  ...
</profiles>
...

And the glassfish-resources.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1       Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
<custom-resource res-type="java.util.Properties" jndi-name="jndi/iprofile" 
                factory-class="org.glassfish.resources.custom.factory.PropertiesFactory">
    <property name="name" value="${webapp.profile}" />
</custom-resource> 
<jdbc-resource enabled="true" jndi-name="jdbc/users" object-type="user" pool-name="MY-POOL">
    <description/>
</jdbc-resource>
<jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.microsoft.sqlserver.jdbc.SQLServerXADataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="MY-POOL" non-transactional-connections="false" ping="false" pool-resize-quantity="2" pooling="true" res-type="javax.sql.XADataSource" statement-cache-size="0" statement-leak-reclaim="false" statement-leak-timeout-in-seconds="0" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
    <property name="serverName" value=""/>
    <property name="PortNumber" value=""/>
    <property name="DatabaseName" value=""/>
    <property name="User" value=""/>
    <property name="Password" value=""/>
</jdbc-connection-pool>

I want to set the DatabaseName property with the value Xpto defined in the pom. Other profiles have different values for the database.dbname property.

  • what to do if i want to add one more connection pool connection to different DB in the glassfish-resources.xml file – avi Jun 03 '21 at 14:16

1 Answers1

1

You can solve this kind of task with the maven-replacer-plugin. It can be used to replace values in files.

Here is an example:

Define your property like you already did:

<properties>
 <database.dbname>Xpto</database.dbname>
</properties>

Change the property you want to replace in your glassfish-resources.xml to look like this:

<property name="DatabaseName" value="DATABASENAME"/>

The value DATABASENAME is used for the replacement. You can use any value but it must be unique in the file you want to modify.

Change the configuration of the maven-war-plugin to look like this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1.1</version>
    <executions>
        <execution>
            <id>prepare-war</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>exploded</goal>
            </goals>
        </execution>
        <execution>
            <id>default-war</id>
            <phase>package</phase>
            <goals>
                <goal>war</goal>
            </goals>
            <configuration>
                <warSourceDirectory>${project.build.directory}/${project.build.finalName}</warSourceDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

From the maven-replacer-plugin wiki (have a look at this page for details how the maven-replacer-plugin and the maven-war-plugin work together):

Currently, the replacer plugin needs access to a target resource before it is packaged into an archive. However, standard WAR packaging does not expose web resources (anything under src/main/webapp) for use in other plugins and runs as a single execution. Fortunately, we can invoke war:exploded to copy these resources earlier in the build lifecycle so that they can be used by the maven-replacer-plugin. The standard package usage will then use the modified web resources when creating the WAR artifact.

Add the following configuration of the maven-replacer-plugin to your pom.xml:

<plugin>
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>replacer</artifactId>
    <version>1.5.3</version>
    <executions>
        <execution>
            <phase>prepare-package</phase>
            <goals>
                <goal>replace</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <file>target/${project.build.finalName}/WEB-INF/glassfish-resources.xml</file>
        <token>DATABASENAME</token>
        <value>${database.dbname}</value>
    </configuration>
</plugin>

As you can see the token DATABASENAME is replaced with the value of ${database.dbname}. This is done in the prepare-package phase, i.e. after the web application contents are copied to the exploded WAR directory, but before the directory is packaged as WAR file.

If the replacement doesn't work, you may have to downgrade the maven-war-plugin to version 2.0.1.

If you want to replace multiple different values you can replace token and value in the configuration with something like this:

<replacements>
    <replacement>
        <token>DATABASENAME</token>
        <value>${database.dbname}</value>               
    </replacement>
</replacements>

See also:

Community
  • 1
  • 1
unwichtich
  • 13,712
  • 4
  • 53
  • 66