I have a database project which uses hibernate and is deployed as a snapshot to Nexus server. But I want to change url , username and password for test and production environment. Is there a way so that I can change properties of my hibernate.cfg.xml while maven build and then deploy it to nexus server and choose between two repositories to which I can deploy the artifact ?

like

<distributionManagement>
        <snapshotRepository>
            <id>snapshots</id>
            <url>http://nexus:3344/nexus/content/repositories/snapshots</url>
        </snapshotRepository>
        <snapshotRepository>
            <id>Project</id>
            <url>http:/nexus:3344/nexus/content/repositories/Project</url>
        </snapshotRepository>
    </distributionManagement>

Hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>

    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://MyDatabase:3344/project</property>
        <property name="hibernate.connection.username">username</property>
        <property name="hibernate.connection.password">password</property>
        <property name="show_sql">true</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hbm2ddl.auto">validate</property>
        <property name="hibernate.c3p0.max_size">30</property>
        <property name="hibernate.c3p0.min_size">5</property>
        <property name="hibernate.c3p0.timeout">600</property>
        <property name="hibernate.c3p0.aquire_increment">2</property>
        <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
        <mapping class="com.jts1.db.dto.AddNewUserDTO" />
        <mapping class="com.jts1.db.dto.AddProjectDBDTO" />
        <mapping class="com.jts1.db.dto.AssignProjectsDTO"/>
        <mapping class ="com.jts1.db.dto.IssueDBDTO"/>
        <mapping class ="com.jts1.db.dto.AddCommentDTO"/>
    </session-factory>
</hibernate-configuration>
1

There are 1 answers

0
Brad Beck On

Maven profiles and resource filtering should get you where you want to go:

For example:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>hibernate.sample</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
  </build>
  <profiles>
    <profile>
      <id>test</id>
      <activation>
        <activeByDefault>true</activeByDefault>
        <property>
          <name>environment</name>
          <value>test</value>
        </property>
      </activation>
      <properties>
        <hibernate.connection.url>jdbc:mysql://MyDatabase:3344/project</hibernate.connection.url>
        <hibernate.connection.username>user1</hibernate.connection.username>
        <hibernate.connection.password>password1</hibernate.connection.password>
      </properties>
      <distributionManagement>
        <snapshotRepository>
            <id>snapshots</id>
            <url>http://nexus:3344/nexus/content/repositories/snapshots</url>
        </snapshotRepository>
      </distributionManagement>
    </profile>
    <profile>
      <id>qa</id>
      <activation>
        <property>
          <name>environment</name>
          <value>qa</value>
        </property>
      </activation>
      <properties>
        <hibernate.connection.url>jdbc:mysql://MyDatabase:3344/projectQA</hibernate.connection.url>
        <hibernate.connection.username>user2</hibernate.connection.username>
        <hibernate.connection.password>password2</hibernate.connection.password>
      </properties>
      <distributionManagement>
        <snapshotRepository>
            <id>Project</id>
            <url>http:/nexus:3344/nexus/content/repositories/snapshotsQA</url>
        </snapshotRepository>
      </distributionManagement>
    </profile>
  </profiles>
</project>

hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">${hibernate.connection.url}</property>
        <property name="hibernate.connection.username">${hibernate.connection.username}</property>
        <property name="hibernate.connection.password">${hibernate.connection.password}</property>
        <property name="show_sql">true</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hbm2ddl.auto">validate</property>
        <property name="hibernate.c3p0.max_size">30</property>
        <property name="hibernate.c3p0.min_size">5</property>
        <property name="hibernate.c3p0.timeout">600</property>
        <property name="hibernate.c3p0.aquire_increment">2</property>
        <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
        <mapping class="com.jts1.db.dto.AddNewUserDTO" />
        <mapping class="com.jts1.db.dto.AddProjectDBDTO" />
        <mapping class="com.jts1.db.dto.AssignProjectsDTO"/>
        <mapping class ="com.jts1.db.dto.IssueDBDTO"/>
        <mapping class ="com.jts1.db.dto.AddCommentDTO"/>
    </session-factory>
</hibernate-configuration>

This setup should allow you to do the following:

mvn clean deploy, mvn clean deploy -Ptest, or mvn clean deploy -Denvironment=test should all deploy the test configuration to the test repository.

mvn clean deploy -Pqa, or mvn clean deploy -Denvironment=qa should all deploy the QA configuration to the QA repository.