首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在构建项目时通过maven更改hibernate.cfg.xml文件路径?

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

Stack Overflow用户
提问于 2015-06-11 13:31:35
回答 2查看 3.6K关注 0票数 1

我有一个数据库项目作为nexus服务器的快照,它在我的两个web项目(测试和生产)中用作依赖项。但我为这两个web项目使用了两个不同的数据库。我想使用测试网站项目的测试数据库和生产网站项目的生产数据库。因此,当项目在jenkins中构建时,我希望更改基于web项目的hibernate配置文件路径。我的代码片段如下所示。

DBUtil.java

代码语言:javascript
复制
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;
}
}

pom.xml

代码语言:javascript
复制
<repositories>
    <repository>
        <id>snapshots</id>
        <name>Snapshots</name>
        <url>url/snapshots/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>com.my</groupId>
        <artifactId>DBAccess</artifactId>
        <version>0.0002-SNAPSHOT</version>
    </dependency>

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

EN

回答 2

Stack Overflow用户

发布于 2015-06-11 21:50:15

您可以使用maven配置文件来构建您的项目。您必须在pom.xml中定义配置文件:

代码语言:javascript
复制
<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文件中,您可以像这样使用已定义的属性:

代码语言:javascript
复制
<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中配置您的构建部分:

代码语言:javascript
复制
<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配置中构建哪个配置文件。

票数 5
EN

Stack Overflow用户

发布于 2015-06-11 14:08:50

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

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

如果您jdbc数据库jar被包装在另一个jar中,且该jar存在于您的nexus服务器中,那么您应该首先转到包装项目源代码,在其中添加其他数据库jar jdbc依赖项,然后再次将该项目部署到nexus中。当提供了新版本的包装jar时,您可以使用上面的解释决定您希望连接哪个数据库,并使用您编写的bash脚本更改这些属性(您必须在maven干净安装部署计划开始之前进入jenkins并编写此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)

而且因为这将使您的项目使用与您的计划(测试、生产)相关的特定配置,所以即使在打包的jar文件中有不同的jdbc驱动程序jar,它也不会产生任何冲突和问题,因为每个数据库都有不同的属性配置。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30772333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档