如果任何sql在循环内失败,如何使ant构建失败

chhkpiq4  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(375)

我必须运行目录中的所有sql文件。所以,使用for循环,我能够实现这一点,但是现在构建正在获得成功,即使任何sql都失败了。
如果任何sql在循环中执行失败,如何使ant构建失败?
编译文件

<?xml version="1.0"?>
<project name="run_build" default="full_build">

        <target name="init" description="Initialize the Ant variables">
                <taskdef resource="net/sf/antcontrib/antlib.xml"/>
        </target>

        <target name="run_sqls" depends="init" description="Run SQLs">
                <for param="sqlfile">
                        <path>
                                <fileset dir="." includes="*.sql"/>
                        </path>
                        <sequential>
                                <property name="sqlfilename" value="@{sqlfile}"/>
                                <echo message = "Running SQL file : ${sqlfilename} "/>
                                <exec executable="${ORACLE_HOME}/bin/sqlplus" failonerror="true">
                                  <arg value="dbuser/dbpass@dbinstance"/>
                                  <arg value="@${sqlfilename}"/>
                                </exec>
                        </sequential>
                </for>
        </target>

        <target name="full_build" description="Running full build">
          <antcall target="run_sqls"/>
        </target>

</project>

当前输出:即使sql失败,生成也成功。

Buildfile: build.xml

full_build:

init:

run_sqls:
     [echo] Running Schema SQL file : abc.sql
     [exec]
     [exec] SQL*Plus: Release 11.2.0.3.0 Production on Wed Aug 5 14:20:25 2020
     [exec]
     [exec] Copyright (c) 1982, 2011, Oracle.  All rights reserved.
     [exec]
     [exec]
     [exec] Connected to:
     [exec] Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
     [exec] With the Partitioning, OLAP, Data Mining and Real Application Testing options
     [exec]
     [exec] update table_sdj set first_name='FirstName' where id=123
     [exec]        *
     [exec] ERROR at line 1:
     [exec] ORA-00942: table or view does not exist
     [exec]
     [exec]
     [exec]
     [exec] Commit complete.
     [exec]
     [exec] SQL> Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
     [exec] With the Partitioning, OLAP, Data Mining and Real Application Testing options

BUILD SUCCESSFUL
Total time: 0 seconds
w9apscun

w9apscun1#

您必须将这些子句添加到sqlplus脚本中,以控制是否存在sql错误或os错误。

whenever sqlerror exit failure;
whenever oserror exit failure;

然后,必须控制程序中的返回代码,以验证脚本是否正常执行。否则,不管发生什么,sqlplus总是以0退出。
例子

$ cat test.sql
set echo on
drop table tx purge;
exit;
$ sqlplus / as sysdba @test.sql

SQL*Plus: Release 12.2.0.1.0 Production on Wed Aug 5 12:11:31 2020

Copyright (c) 1982, 2016, Oracle.  All rights reserved.

Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production

SQL> drop table tx purge;
drop table tx purge
           *
ERROR at line 1:
ORA-00942: table or view does not exist

SQL> exit;
Disconnected from Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
$ echo $?
0

包括正确的条款

$ cat test.sql
whenever sqlerror exit failure;
whenever oserror exit failure;
set echo on
drop table tx purge;
exit;
$ sqlplus / as sysdba @test.sql

SQL*Plus: Release 12.2.0.1.0 Production on Wed Aug 5 12:13:26 2020

Copyright (c) 1982, 2016, Oracle.  All rights reserved.

Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production

SQL> drop table tx purge;
drop table tx purge
           *
ERROR at line 1:
ORA-00942: table or view does not exist

Disconnected from Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
$ echo $?
1

默认情况下,如图所示,sqlplus总是以0退出。当错误发生时,您需要指示它带着错误退出。
每当sqlerror

相关问题