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

标签 java hibernate maven-3

我将 Date 基础项目作为 Nexus 服务器中的快照,它在我的两个 Web 项目(测试和生产)中用作依赖项。但我为这两个网络项目使用了两个不同的数据库。我想将测试数据库用于测试 Web 项目,将生产数据库用于生产 Web 项目。所以我想在jenkins中构建项目时根据web项目更改hibernate配置文件路径。我的代码片段是这样的。

DBUtil.java

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

<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 配置文件或任何其他方式提供对此的任何解决方案。

最佳答案

您可以使用 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。 您还可以在 maven 配置中告诉 jenkins 您希望构建哪个配置文件。

关于java - 构建项目时如何通过maven更改hibernate.cfg.xml文件路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30772333/

相关文章:

maven - 无法将本地 Maven 人工制品安装到远程存储库 google-android-maps

Maven 忽略子模块配置文件中 pluginManagement 的插件版本

java - 在 Maven 多模块项目上将一个模块设置为父配置中其他模块的依赖项

java - 如何使用 Html.fromHtml 在 android 中显示心脏标志

java - 仅当作为参数传递的 List 包含元素时才考虑 JPA 'where in'

java - 验证插入查询是否在 hibernate 中完成

java - java和c#应用程序之间的套接字通信

java - 运行时在项目中找不到主类 - Java Netbeans

java - Spring boot、hibernate、hikariCP 和 mysql 不活动后无通信

Maven 构建在 Jenkins 上失败,但可以在本地计算机上运行