如何在构建项目时通过maven更改hibernate.cfg.xml文件路径?

时间:2015-06-11 05:31:35

标签: java hibernate maven-3

我将日期基础项目作为nexus服务器中的快照,它在我的两个Web项目(测试和生产)中用作依赖项。但我正在为这两个网络项目使用两个不同的数据库。我想将测试数据库用于测试Web项目和生产Web项目的生产数据库。因此,当项目在jenkins中构建时,我想基于Web项目更改hibernate配置文件路径。我的代码片段就像这样。

DBUtil.java

With ActiveSheet
If ( .Range("A1").Value = .Range("A2").Value ) Then
  .Range("A4") = <your value>
EndIf

的pom.xml

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

请使用maven配置文件提供任何解决方案。或者它是什么。

2 个答案:

答案 0 :(得分:4)

创建属性文件,当您开始使用jenkins构建项目时,编写一个bash脚本以从源代码访问属性文件并更改有关构建计划的所需配置(生产,测试)。

在你的maven构建源代码之后,将提供所需的输出。

如果您的jdbc数据库jar被包装在另一个jar中并且该jar存在于您的nexus服务器中,您应该首先在包装项目源中添加其他数据库jar jdbc依赖项,然后再将项目部署到nexus中。 当提供新版本的包装jar时,您可以使用上面的说明决定使用您编写的bash脚本更改这些属性来决定要连接的数据库(您必须进入jenkins并在maven clean install之前编写此bash脚本)部署计划开始):

  1. hibernate.connection.driver_class(例如:com.mysql.jdbc.Driver)
  2. hibernate.connection.url属性(例如:jdbc:mysql:// localhost:3306 / mehdi)
  3. hibernate.dialect属性(例如:org.hibernate.dialect.MySQLDialect)
  4. 并且因为这将使您的项目使用关于您的计划(测试,生产)的特定配置,即使您的包装jar文件中有不同的jdbc驱动程序jar,它也不会产生任何冲突和问题,因为每个数据库都有不同财产配置。

答案 1 :(得分:3)

您可以使用maven配置文件来构建项目。您必须在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>

在hibernate.cfg.xml文件中,您可以使用如下定义的属性:

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

然后,您必须在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>

然后你可以调用mvn clean install -Pdev | prod。 您还可以告诉jenkins您希望在maven配置中构建哪个配置文件。