0

I'm currently working on some ant for applying liquibase changes to databases. I'd like to be able to handle errors that I get in ant from the liquibase updateDatabase task. Here is what I have right now in my build file (bear in mind what I have now works fine I just need to be able to handle errors I might get from running the liquibase).

<target name="update_db" depends="prepare">
  <taskdef resource="liquibasetasks.properties">
    <classpath refid="classpath"/>
  </taskdef>    

  <updateDatabase
        changeLogFile="${db.changelog.file}"
        driver="${database.driver}"
        url="jdbc:mysql://localhost/${db.name}"
        username="${user}"
        password="${password}"
        promptOnNonLocalDatabase="not local database"
        dropFirst="false"
        classpathref="classpath"
  />    
</target> 

Currently when I get an error I get something similar to this (from a situation I created to demonstrate):

BUILD FAILED
MYPATH\build.xml:15: The following error occurred while executing this line:
MYPATH\\build.xml:117: liquibase.exception.MigrationFailedException: Migration failed   
 for change set PATH/2.20.9/tables.xml::FFP-1384::AUSER:
 Reason: liquibase.exception.DatabaseException: Error executing SQL ALTER TABLE        
test.widget ADD full_screen BIT(1) DEFAULT 0: Duplicate column name   'full_screen'
.............. and a the wall of text continues

Ideally I would like to be able to get the return code (rather than this block of text) from liquibase into ant and then based on that do something such as :

<echo message="this failed because ${reason}"/>

but not limited to that.

Is there some way for me to obtain the return code from liquibase? My best guess is that similar to the ant exec task, by default the return code is ignored and I'm hoping there is some way for me to get at it. Any suggestions welcome.

edit: Vaguely similar question https://stackoverflow.com/questions/17856564/liquibase-3-0-2-logging-to-error-console

4

1 回答 1

0

ant contrib trycatch 任务使我能够处理错误,结果证明这是一个合适的修复,因为无论如何查看堆栈跟踪实际上很有用。

http://ant-contrib.sourceforge.net/tasks/tasks/trycatch.html

<trycatch property="foo" reference="bar">
  <try>
    <fail>Tada!</fail>
  </try>

  <catch>
    <echo>In &lt;catch&gt;.</echo>
  </catch>

  <finally>
    <echo>In &lt;finally&gt;.</echo>
  </finally>
</trycatch>

您需要下载 ant contrib jar,如果您不想将其放在 ANT_HOME 中,则可以使用

<taskdef resource="net/sf/antcontrib/antcontrib.properties">
  <classpath>
    <pathelement location="PATH TO JAR"/>
  </classpath>
</taskdef>
于 2013-09-02T10:32:04.800 回答