转 : ANT 调用sqlplus 客户端

Jenkins安装与配置  (版本控制)

1

用ant脚本执行sql语句
现在我需要写一个ant脚本来实现项目安装,情况是这样的,客户现在运行的版本可能是2.0,安装目录里可能有REL_1_0,REL_2_0这些文件夹,里面有sql脚本,这些都是已经运行过的sql脚本,现在我发一个新的版本给客户,这个时候增加了一个新的sql脚本,所以安装目录里会有REL_1_0,REL_2_0,REL_3_0,我怎么写ant脚本,让它只运行REL_3_0里的sql脚本呢? 收起
JavaMenSu | 浏览 3310 次
发布于2010-08-04 14:18 最佳答案

两步即可:

1.将执行sql语句的命令写在一个 xx.bat文件里面

2.在build.xml脚本里 使用 exec标签来执行bat文件即可

例如:<exec executable=" xx.bat" ></exec>

你们数据库是什么数据库呢?如果是oracle数据库,那么执行sql语句的命令是用 sqlplus user/pwd@db example.sql

关于ant脚本标签,请参考:http://hi.baidu.com/woxfy/blog/item/8359887a281899fe0ad1878d.html

2
1、build.xml 中执行sql 脚本

autocommit="true"
url="jdbc:oracle:thin:@10.128.x.x:1521:xxsid" src="data.sql" print="yes" output=

"sql_out.txt">

autocommit="true"
url="jdbc:oracle:thin:@10.128.x.x:1521:xxsid" src="data.sql" print="yes" output=

"sql_out.txt">

也可以在 ... 中直接包含一条或多条 sql 语句,默认分号分隔。脚本文件 data.sql 中可以写多个语句,也是默认分号分隔,可含 -- 注释符。说白了就是基本在 PL/SQL Developer 中可以怎么写,你的脚本文件中也可以怎么写,并且还能支持象 MySQL 的 // 那样的注释符。

像如下那样的SQL脚本内容(data.sql)


3.

相信大部分开发人员都对ant有相当程度的了解,为什么呢?简而言之,ant是个好工具,真的不错。
本人从ant学习过程中遇到一些问题,后来在解决的过程中,对ant有了更深一步的了解。所以决定把学习过程中一些不太清楚的地方总结一下,和大家交流沟通一下,对于我一个新手来说肯定有许多认识上的不足,希望大家多给指导。
<target name="buildLog">
<exec executable="svn" output="${build.logs}/buildLog.xml">
<arg line="log ../. --xml -r HEAD"/>
</exec>
<echo message="SVN log created successfully!" level="info"/>
</target>
这是我现在从事项目的一段ant脚本里面有一些不太明白的地方:
(1)exec 有什么作用?
exec可以调用系统命令,比如:
<target name="test">
<exec executable="cmd.exe">
<arg line="/c dir"/>
</exec>
</target>
上边的这段代码所起到的作用是打开命令输入框,/c转到c盘根目录,执行dir命令。
(line 空格分隔的命令行变量列表。)
(2)
<exec executable="svn" output="${build.logs}/buildLog.xml">
<arg line="log ../. --xml -r HEAD"/>
</exec>
这个的作用就是在svn工作空间下面执行下面的命令:
svn log ../. --xml -r HEAD,把生成的结果保存到buildLog.xml文件中。


<exec executable="${base.dir}/email.bat" >
</exec>
<exec executable="cmd" dir="D:" failonerror="true">
<arg line="/c del 1.bat" />
</exec>

failonerror:表示当出现错误时自动停止

4.
arg例子
<arg value="-l -a"/>
是一个含有空格的单个的命令行变量。
<arg line="-l -a"/>
是两个空格分隔的命令行变量。
<arg path="/dir;/dir2:dir3"/>
是一个命令行变量,其值在DOS系统上为dir;dir2;dir3;在Unix系统上为/dir:/dir2:/dir3 。

转自:http://www.cnblogs.com/hoojo/archive/2013/06/14/java_ant_project_target_task_run.html
参考:http://ant.apache.org/manual/Tasks/exec.html


Windows Users
The <exec> task delegates to Runtime.exec which in turn apparently calls ::CreateProcess. It is the latter Win32 function that defines the exact semantics of the call. In particular, if you do not put a file extension on the executable, only ".EXE" files are looked for, not ".COM", ".CMD" or other file types listed in the environment variable PATHEXT. That is only used by the shell.
Note that .bat files cannot in general by executed directly. One normally needs to execute the command shell executable cmd using the /c switch.
<target name="help">
<exec executable="cmd">
<arg value="/c"/>
<arg value="ant.bat"/>
<arg value="-p"/>
</exec>
</target>


5.
sqlplus catv/catv@oracle9 @drop-table.sql;

原文地址:https://www.cnblogs.com/feiyun8616/p/6099843.html