How to change hibernate.cfg.xml file path through maven when building project?

4.1k views Asked by At

I have Date base project as snapshot in nexus server which is using as dependency in my two web projects(test and production). but I am using two different databases for those two web projects. I want to use test data base for test web project and production data base for production web project. So I want to change hibernate configuration file path based on web project when the project is building in jenkins. My code snippet like this.

DBUtil.java

public class DBUtils {
private static SessionFactory sessionFactory;

private DBUtils() {
}

static {
    Configuration configuration = new Configuration();
    configuration.configure("/hibenateconfigpath");
    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
            .applySettings(configuration.getProperties()).build();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}
}

pom.xml

<repositories>
    <repository>
        <id>snapshots</id>
        <name>Snapshots</name>
        <url>url/snapshots/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>com.my</groupId>
        <artifactId>DBAccess</artifactId>
        <version>0.0002-SNAPSHOT</version>
    </dependency>

Please provide any solution to this with maven profiles or what ever it is.

2

There are 2 answers

3
Baldo On

You can use maven profiles to build your project. You have to define the profiles in your pom.xml:

<profiles>
    <profile>
        <id>test</id> 
        <properties>
            <db.driverClass>com.mysql.jdbc.Driver</db.driverClass>
            <db.jdbcUrl>jdbc:mysql://xxxxx:3306/test</db.jdbcUrl>
            <db.user>test-user</db.user>
            <db.password>test-pass</db.password>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <db.driverClass>com.mysql.jdbc.Driver</db.driverClass>
            <db.jdbcUrl>jdbc:mysql://yyyyy:3306/prod</db.jdbcUrl>
            <db.user>prod-user</db.user>
            <db.password>prod-pass</db.password>
        </properties>
    </profile>
</profiles>

In hibernate.cfg.xml file you can use the defined properties like this:

<hibernate-configuration>

    <session-factory>

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

    </session-factory>

</hibernate-configuration>

Then, you have to configure your build section in pom.xml:

<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <warName>${project.artifactId}</warName>
                <webResources>
                    <resource>
                        <filtering>true</filtering> <!-- THIS IS IMPORTANT! It tells maven to replace your variables with the properties values -->
                        <directory>src/main/webapp</directory>
                        <includes>
                            <include>**/hibernate.cfg.xml</include> <!-- the path to hibernate.cfg.xml -->
                        </includes>
                    </resource>
                </webResources>
            </configuration>
        </plugin>
    </plugins>
</build>

Then you can call mvn clean install -Pdev|prod. You can also tell jenkins which profile you wish to build in the maven configuration.

3
Mehdi On

create a property file and when your going to start building the project using jenkins, write a bash script to access the property file from your source code and change your desired configuration regarding your build plan (Production, Test).

after your maven build your source code, desired output will be provided.


if your jdbc database jar is wrapped inside another jar and that jar exist inside your nexus server, you should go to the wrapping project source first add your other database jar jdbc dependencies in there, then deploy the project into the nexus again. when the new version of wrapped jar was provided, you can decide which database you wish to connect using the above explanation with changing these properties using your written bash script (you have to go inside your jenkins and write this bash script before your maven clean install deploy plan start):

  1. hibernate.connection.driver_class (ex: com.mysql.jdbc.Driver)
  2. hibernate.connection.url property (ex: jdbc:mysql://localhost:3306/mehdi)
  3. hibernate.dialect property (ex: org.hibernate.dialect.MySQLDialect)

and because this will make your project to use specific config regarding your plans (Test, Production), even if there be different jdbc drivers jars within your wrapped jar file, It does not create any conflict and problem because each database have different property configs.