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

sub*_*ala 1 java hibernate maven-3

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

数据库实用程序

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;
}
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

请使用 maven 配置文件或其他任何内容提供任何解决方案。

小智 5

您可以使用 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>
Run Code Online (Sandbox Code Playgroud)

在 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>
Run Code Online (Sandbox Code Playgroud)

然后,您必须在 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>
Run Code Online (Sandbox Code Playgroud)

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