【问题标题】:How to set liquibase classpath如何设置 liquibase 类路径
【发布时间】:2018-11-01 19:53:53
【问题描述】:

我创建了一个 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>

运行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 entityjhipster import-jdl 时每次由jhipster 添加变更集时编辑文件。

【问题讨论】:

  • 你能设置两次classpath吗?我看到两个 --classpath 标志。另请注意,昨天已更改,并且在 JHipster 的未来版本中将删除类路径 github.com/jhipster/generator-jhipster/pull/6121
  • 我放了两个--classpath,因为我找不到单个--classpath 的语法。我尝试使用; 分隔符但没有用。看来删除classpath: 将是未来的解决方案

标签: jhipster liquibase


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    因此,从@Sydney 的建议开始,我决定在 POM 中编写一个新的配置文件,在流程资源阶段更改 'classpath:' 单词,为此我使用了一个 ANT 插件进行制作将master.xml 文件替换为&lt;empty&gt;。所以这是一个结果:

        <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://&lt;IP&gt;:&lt;PORT&gt;/&lt;DATABASE&gt;" -Dliquibase.password="&lt;supersecret&gt;" -Ponly-liquibase

    或者这个:

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

    我真的希望这对你有帮助!!!

    【讨论】:

    • 请注意 maven 配置中的changeLogFile,因为这是设置为target 文件夹而不是src 文件夹。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 2011-11-27
    • 2012-11-04
    • 2013-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多