如何设置 liquibase 类路径

How to set liquibase classpath

我创建了一个 JHipster 项目。我想手动 运行 liquibase 变更集。默认情况下,变更集包含在类路径中。变更日志在 src/main/resources/config/liquibase/master.xml 中,变更集在 src/main/resources/config/liquibase/changelog.

<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
    xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">

    <include file="classpath:config/liquibase/changelog/00000000000000_initial_schema.xml" relativeToChangelogFile="false"/>
    <!-- jhipster-needle-liquibase-add-changelog - JHipster will add liquibase changelogs here -->
    <!-- jhipster-needle-liquibase-add-constraints-changelog - JHipster will add liquibase constraints changelogs here -->
</databaseChangeLog>

当 运行ning mvn liquibase:update 时,我得到一个错误,因为即使文件存在,变更集也不在类路径中:

[ERROR] Failed to execute goal org.liquibase:liquibase-maven-plugin:3.5.3:update (default-cli) on project playground: Error setting up or running Liquibase: liquibase.exception.SetupException: classpath:config/liquibase/changelog/00000000000000_initial_schema.xml does not exist -> [Help 1]

所以我尝试通过设置类路径从命令行运行。

liquibase --classpath=src/main/resources --classpath=postgresql-42.1.3.jar 
--url=jdbc:postgresql://localhost:5432/playground 
--driver=org.postgresql.Driver 
--changeLogFile=src/main/resources/config/liquibase/master.xml 
--username playground --password=***** update

同样的错误:Unexpected error running Liquibase: classpath:config/liquibase/changelog/00000000000000_initial_schema.xml does not exist

一种解决方法是删除包含部分中的引用 classpath: 但我想避免在每次使用 jhipster entity 或 [=19 时由 jhipster 添加变更集时编辑文件=].

解决方法是运行 mvn process-resources 在运行 执行liquibase 命令之前,这样src/main/resources 下的文件就会在target/classes 文件夹中。然后删除 classpath: 部分,如 https://github.com/jhipster/generator-jhipster/pull/6121

中所述

好吧...我遇到了同样的问题,因为我不希望 jhipster 自动更新生产数据库。

所以从@Sydney 的建议开始,我决定在 POM 中写一个新的配置文件,它改变了进程资源阶段的 'classpath:' 词,为此我使用了一个 ANT 插件,用于将 master.xml 文件替换为 <empty>。结果是这样的:

    <profile>
        <id>only-liquibase</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.liquibase</groupId>
                    <artifactId>liquibase-maven-plugin</artifactId>
                    <version>${liquibase.version}</version>
                    <configuration>
                        <changeLogFile>target/classes/config/liquibase/master.xml</changeLogFile>
                        <driver></driver>
                        <url></url>
                        <defaultSchemaName></defaultSchemaName>
                        <username>dentalaser</username>
                        <password></password>
                        <referenceUrl>hibernate:spring:ec.com.dentalaser.domain?dialect=&amp;hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&amp;hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy</referenceUrl>
                        <verbose>true</verbose>
                        <logging>debug</logging>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>org.javassist</groupId>
                            <artifactId>javassist</artifactId>
                            <version>${javassist.version}</version>
                        </dependency>
                        <dependency>
                            <groupId>org.liquibase.ext</groupId>
                            <artifactId>liquibase-hibernate5</artifactId>
                            <version>${liquibase-hibernate5.version}</version>
                        </dependency>
                        <dependency>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-starter-data-jpa</artifactId>
                            <version>${project.parent.version}</version>
                        </dependency>
                        <dependency>
                            <groupId>javax.validation</groupId>
                            <artifactId>validation-api</artifactId>
                            <version>${validation-api.version}</version>
                        </dependency>
                    </dependencies>
                </plugin>
                <plugin>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>process-resources</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>
                                <target>
                                    <echo>Reemplazando el classpath el archivo a desplegar en AWS</echo>
                                    <replace file="${project.build.directory}/classes/config/liquibase/master.xml" token="classpath:" value=""/>
                                </target>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

之后从命令行执行如下操作:

./mvnw process-resources liquibase:updateSQL -DskipTests -Dliquibase.url="jdbc:postgresql://<IP>:<PORT>/<DATABASE>" -Dliquibase.password="<supersecret>" -Ponly-liquibase

或者这个:

./mvnw process-resources liquibase:updateSQL -DskipTests -Dliquibase.url="offline:postgresql" -Ponly-liquibase

真心希望对你有所帮助!!!