Search code examples
javagitmavenherokuwildfly-swarm

Properties file - Remote x Local


I have a properties file to handle my datasource. This file is loaded by properties maven plugin. When loaded I use on wildfly Swarm plugin.

database.properties:

database.server=localhost
database.server.port=3306
database.name=XXXXXXX
database.options=
database.user=XXXXXXX
database.password=XXXXXXXX
database.pool.min=1
database.pool.max=10
database.pool.preFill=true

Properties maven plugin:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>${properties-maven.version}</version>
        <executions>
            <execution>
                <phase>initialize</phase>
                    <goals>
                        <goal>read-project-properties</goal>
                    </goals>
            </execution>
        </executions>
        <configuration>
            <files>
                <file>src/main/resources/database.properties</file>
            </files>
        </configuration>
</plugin>

Wildfly Swarm plugin:

<plugin>
    <groupId>org.wildfly.swarm</groupId>
    <artifactId>wildfly-swarm-plugin</artifactId>
    <version>${version.wildfly.swarm}</version>
        <configuration>
            <properties>
                <swarm.datasources.data-sources.CondominioDs.driver-name>mysql</swarm.datasources.data-sources.CondominioDs.driver-name>
                <swarm.datasources.data-sources.CondominioDs.connection-url>jdbc:mysql://${database.server}:${database.server.port}/${database.name}${database.options}</swarm.datasources.data-sources.CondominioDs.connection-url>
                <swarm.datasources.data-sources.CondominioDs.initial-pool-size>${database.pool.min}</swarm.datasources.data-sources.CondominioDs.initial-pool-size>
                <swarm.datasources.data-sources.CondominioDs.max-pool-size>${database.pool.max}</swarm.datasources.data-sources.CondominioDs.max-pool-size>
                <swarm.datasources.data-sources.CondominioDs.pool-prefill>${database.pool.preFill}</swarm.datasources.data-sources.CondominioDs.pool-prefill>
                <swarm.datasources.data-sources.CondominioDs.user-name>${database.user}</swarm.datasources.data-sources.CondominioDs.user-name>
                <swarm.datasources.data-sources.CondominioDs.password>${database.password}</swarm.datasources.data-sources.CondominioDs.password>
                <swarm.datasources.data-sources.CondominioDs.check-valid-connection-sql>SELECT 1</swarm.datasources.data-sources.CondominioDs.check-valid-connection-sql>
            </properties>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>package</goal>
                </goals>
            </execution>
        </executions>
</plugin>

Everything works well. Both remotely and locally.

I use Git too and I have two branchs. One (master) to update my remote repository and other (development) to code and tests.

Finally, I use Heroku to deploy my application and I use maven plugin for this (not git).

My question is about the datasource properties file. I don't know how to have two versions of file. One to use when I'm coding (locally) and other to use when I needs deploy the application on Heroku.

Of course, the address, the user and the password of locally database is different the remotely database.

This moment, I put remotely information in the properties file and deploy on Heroku. Then I came back with the local information to continue coding.


Solution

  • I see you're using the properties format for configuration; I'd suggest moving to the YAML format. Using that, WildFly Swarm supports configuration profiles.

    In your case, which is probably fairly typical, I'd put common configuration into project-defaults.yml, your local configuration into project-local.yml and your Heroku/production configuration into project-prod.yml (or project-heroku.yml, your choice). Then, you pick the correct configuration when running the app: java -jar myapp-swarm.jar -Slocal (or -Sprod).

    For more info, see http://docs.wildfly-swarm.io/2017.11.0/#_configuration_of_a_wildfly_swarm_application