hibernate - 使用 Maven 构建或配置文件将 Artifact 部署到 Nexus 服务器时,动态选择 Hibernate url 和 Nexus 快照存储库

标签 hibernate maven nexus

我有一个使用 hibernate 的数据库项目,并作为快照部署到 Nexus 服务器。但我想更改测试和生产环境的 url、用户名和密码。有没有办法让我可以在 maven 构建时更改 hibernate.cfg.xml 的属性,然后将其部署到 nexus 服务器,并在可以部署 Artifact 的两个存储库之间进行选择?

喜欢

<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>

最佳答案

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 deploymvn clean deploy -Ptestmvn clean deploy -Denvironment=test 都应将测试配置部署到测试存储库。

mvn clean deploy -Pqamvn clean deploy -Denvironment=qa 都应将 QA 配置部署到 QA 存储库。

关于hibernate - 使用 Maven 构建或配置文件将 Artifact 部署到 Nexus 服务器时,动态选择 Hibernate url 和 Nexus 快照存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30772002/

相关文章:

java - 如何找出用于 hibernate 中特定查询的事务隔离级别?

java - OneToMany 子实体在 save() 上未持久化

java - 如何忽略来自 Eclipse 中特定文件夹的 XML 警告?

maven - Allure:如何重新运行所有损坏的测试,然后生成报告

exception - 无法在本地运行 Sonatype Nexus

java - Hibernate脏检查查询

java - Hibernate 设置的字段可以是最终的吗?

java - 如何确定一组 springframework 依赖项的 <parent> 依赖项

nexus 2 获取 "raw"存储库中文件的校验和

Maven:是否有一个简单的命令来测试存储库访问?