wesleyy
wesleyy

Reputation: 2735

Liquibase and Hibernate read from same properties file

I am using both Liquibase and Hibernate libraries in my application. They both work with the same database, but each requires it's own properties file. However, some of the database-specific fields are common. I would like to avoid duplication of those fields and having them in both files. I would like to have those properties in a single file that Liquibase and Hibernate properties file would read from.

Currently I have.

liquibase.properties

driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/mydb
username: root
password: root

hibernate.cfg.xml

<hibernate-configuration>
    <session-factory>
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="hibernate.hbm2ddl.auto">none</property>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/mydb</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>

Note how driver, URL, username and password are duplicated in those 2 files. Ideally, I'd have some 3rd file like this.

database.properties

driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/mydb
username: root
password: root

And then something like.

liquibase.properties

driver: ${driver}
url: ${url}
username: ${username}
password: ${password}

hibernate.cfg.xml

<hibernate-configuration>
    <session-factory>
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="hibernate.hbm2ddl.auto">none</property>

        <!-- Database connection settings -->
        <property name="connection.driver_class">${driver}</property>
        <property name="connection.url">${url}</property>
        <property name="connection.username">${username}</property>
        <property name="connection.password">${password}</property>

Is something like that possible?

Upvotes: 2

Views: 588

Answers (1)

Jens
Jens

Reputation: 6383

Here is a SO answer (and question) that shows how to put hibernate db config in a property file: https://stackoverflow.com/a/25685198/332248

Here is documentation on liquibase how to set a properties file for liquibase (using the --defaultsFile option): http://www.liquibase.org/documentation/command_line.html

So it should be possible to combine everything into one hibernate.properties file and then specify the same file when running liquibase.

Upvotes: 1

Related Questions