我有一个使用hibernate的数据库项目,并作为快照部署到Nexus服务器。但我想更改测试和生产环境的URL,用户名和密码。有没有办法让我可以在maven构建时更改hibernate.cfg.xml的属性,然后将其部署到nexus服务器,并在两个存储库中选择我可以部署工件?
<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>
<!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>
答案 0 :(得分:1)
Maven配置文件和资源过滤应该可以让您到达目的地:
例如:
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>
此设置应允许您执行以下操作:
mvn clean deploy
,mvn clean deploy -Ptest
或mvn clean deploy -Denvironment=test
都应将测试配置部署到测试存储库。
mvn clean deploy -Pqa
或mvn clean deploy -Denvironment=qa
都应将QA配置部署到QA存储库。