简体   繁体   English

在使用Maven构建或配置文件将工件部署到Nexus服务器的同时,动态选择Hibernate URL和nexus快照存储库

[英]Choose Hibernate url and nexus snapshot repository dynamically while deploying artifact to Nexus server with Maven build or profiles

I have a database project which uses hibernate and is deployed as a snapshot to Nexus server. 我有一个使用hibernate的数据库项目,并作为快照部署到Nexus服务器。 But I want to change url , username and password for test and production environment. 但我想更改测试和生产环境的URL,用户名和密码。 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 ? 有没有办法让我可以在maven构建时更改hibernate.cfg.xml的属性,然后将其部署到nexus服务器,并在两个存储库之间进行选择,我可以部署工件?

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 的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 profiles and resource filtering should get you where you want to go: Maven配置文件和资源过滤应该可以让您到达目的地:

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

mvn clean deploy -Pqa , or mvn clean deploy -Denvironment=qa should all deploy the QA configuration to the QA repository. mvn clean deploy -Pqamvn clean deploy -Denvironment=qa都应该将QA配置部署到QA存储库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM