Liquibase作为Ant任务:找不到changelogs.xml的相对路径。

3
我使用Liquibase 2.0.5版本来测试数据库迁移。目前,如果我在updateDatabase任务中定义changeLogFile属性为<updateDatabase changeLogFile="${changeLogFile}",并且在liquibase.properties中指定了"${changeLogFile}"changeLogFile=com/db/master.changelog.xml,则文件master.changelog.xml将无法找到。
我使用Eclipse和Ant独立运行的Ant任务。
因此,我使用以下修复方法:${basedir}/**src**/
<updateDatabase changeLogFile="${basedir}/src/${changeLogFile}"

但我希望不要使用这种不太优雅的解决方案。

我尝试使用 /bin 目录来指定 XML 文件的类路径。

<fileset dir="bin">        <include name="**/*.xml" />    </fileset>

但是发生了很多异常,所以我注释掉了这个块。

我做错了什么?

liquibase.properties 文件包含:

changeLogFile=com/db/master.changelog.xml
driver=org.h2.Driver
url=jdbc:h2:tcp://localhost/testhiberliq
username=sa
password=

#for diff between two databases
baseUrl=jdbc:h2:tcp://localhost/testhiberliq
baseUsername=sa
basePassword=

dropFirst=false
tag=version 1.4
outputFile=outputFile.xml
outputDiffFile=outputFile.txt

NewFile1.xml(用作build.xml):

<?xml version="1.0" encoding="UTF-8"?>

<project name="liquibase-sample">

    <property file="liquibase.properties" />

    <path id="lib.path">
        <fileset dir="lib">
            <include name="**/*.jar" />
        </fileset>
    <!--    <fileset dir="bin">
            <include name="**/*.xml" />
        </fileset> -->
    </path>

    <taskdef resource="liquibasetasks.properties">
        <classpath refid="lib.path" />
    </taskdef>

    <target name="update-database">
        <!--       <taskdef name="updateDatabase" 
        classpathref="lib.path" />-->
        <updateDatabase changeLogFile="${basedir}/src/${changeLogFile}" driver="${driver}" url="${url}" username="${username}" password="${password}" dropFirst="${dropfirst}" classpathref="lib.path" />
    </target>

</project>

master.changelog.xml:

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

  <include file="changelog/included.changelog.xml" relativeToChangelogFile="true"/> 
  <include file="changelog/included2.changelog.xml" relativeToChangelogFile="true"/> 
  <include file="changelog/included3.changelog.xml" relativeToChangelogFile="true"/> 

</databaseChangeLog> 

目录树如下:

I:.
│   .classpath
│   .project
│   liquibase.properties
│   NewFile.xml
│   NewFile1.xml
│   outputFile.txt
│   outputFile.xml
│
├───.settings
│       org.eclipse.jdt.core.prefs
│
├───bin
│   │   hibernate.cfg.xml
│   │
│   ├───com
│   │   └───db
│   │       │   master.changelog.xml
│   │       │
│   │       └───changelog
│   │               included.changelog.xml
│   │               included2.changelog.xml
│   │               included3.changelog.xml
│   │
│   └───testproject
│       │   Main.class
│       │
│       └───database
│           │   DatabaseDAO.class
│           │
│           └───pojo
│                   Users.class
│
├───lib
│       h2-1.3.168.jar
│       liquibase.jar
│
└───src
    │   hibernate.cfg.xml
    │
    ├───com
    │   └───db
    │       │   master.changelog.xml
    │       │
    │       └───changelog
    │               included.changelog.xml
    │               included2.changelog.xml
    │               included3.changelog.xml
    │
    └───testproject
        │   Main.java
        │
        └───database
            │   DatabaseDAO.java
            │
            └───pojo
                    Users.java
1个回答

0
该任务将从类路径中检索变更日志文件。您需要按照以下方式将运行时根目录添加到路径中:
<path id="lib.path">
    ..
    ..
    <pathelement location="bin"/>
</path>

并将任务更改如下:

<updateDatabase changeLogFile="com/db/master.changelog.xml" ...

而且主要的更改日志文件还需要包含各个更改文件的完整路径:
<databaseChangeLog .. 

  <include file="com/db/changelog/included.changelog.xml"/> 
  <include file="com/db/changelog/included2.changelog.xml"/> 
  <include file="com/db/changelog/included3.changelog.xml"/> 

</databaseChangeLog> 

在运行时,liquibase会查看本地目录或类路径上列出的jar和目录中的内容。
为什么要这样做呢?这种方法使得changelog文件可以作为一个jar归档包打包,可以与您的代码分开或一起使用。非常有用。

嗨,Mark O'Connor,我只有放在类路径中的xml文件,而没有包含更改日志xml的jars。如果我尝试设置“java”类路径(也称为“bin”),那么将抛出大量异常:无法从i:\workspace\TestLqbsProject\bin\com\db\changelog\included.changelog.xml获得资源:java.util.zip.ZipException:打开zip文件时出错。 - Kamen Jahr
嗨,马克·奥康纳,我又来了:-) 如果我使用<fileset>标签检索带有changelog-xmls的类路径,则会发生异常。现在我使用具有位置“bin”的pathelement,因此liquibase ant任务运行!其他更改不需要。非常感谢您的回复。 - Kamen Jahr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接