如果任何 SQL 在循环内失败,如何使 ANT 构建失败
How to fail ANT build if any SQL fails inside loop
我必须 运行 目录中的所有 SQL 文件。因此,使用 for loop ,我能够实现这一点,但现在构建正在取得成功,即使 SQL 中的任何一个失败。
如果在循环中执行任何 sql 失败,如何使 ANT 构建失败?
build.xml
<?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
您必须将这些子句添加到您的 sqlplus 脚本中,以控制是否存在 sql 错误或 os 错误。
whenever sqlerror exit failure;
whenever oserror exit failure;
然后,你必须在程序中控制return代码来验证脚本是否执行正常。如果不这样做,无论如何 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退出,需要在出错时指示它以error退出。
我必须 运行 目录中的所有 SQL 文件。因此,使用 for loop ,我能够实现这一点,但现在构建正在取得成功,即使 SQL 中的任何一个失败。
如果在循环中执行任何 sql 失败,如何使 ANT 构建失败?
build.xml
<?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
您必须将这些子句添加到您的 sqlplus 脚本中,以控制是否存在 sql 错误或 os 错误。
whenever sqlerror exit failure;
whenever oserror exit failure;
然后,你必须在程序中控制return代码来验证脚本是否执行正常。如果不这样做,无论如何 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退出,需要在出错时指示它以error退出。