启动oracle数据库,操作有两个:
1. 启动监听:
<exec executable="lsnrctl" >
<arg line=" start "/>
</exec>
2. 打开数据库实例:
<exec executable='cmd' >
<arg line="/c net start oracleservice${ORA_SID} "/>
</exec>
第一个操作在这里没有什么区别,就是ant调用系统命令的标准用法,我们来看一下第二个命令, net start oracleservice${ORA_SID},如果你直接放入到executable属性中,执行结果一定会是:
BUILD FAILED: E:\java\TestAnt\build.xml:44: Execute failed: java.io.IOException: CreateProcess: "net start oracleservicemy_oracle" error=2
痛苦吧,带有空格的命令应该使用以下方法:
<exec executable='cmd' >
<arg line="/c net start oracleservice${ORA_SID} "/>
</exec>
使用 tstamp target可以非常方便的定义日期属性:
<tstamp>
<format property="DB_BACKUP_TIME" pattern="yyyy-MM-dd"/>
</tstamp>
附录:参考脚本
<!-- 2005 by guipei. -->
<project name="TestAntOracle" default="demo" basedir="./">
<!-- set global properties for this build -->
<property name="DB_BACKUP_DIR" value="." />
<property name="ORA_SID" value="my_oracle"/>
<property name="ORA_USER" value="scott"/>
<property name="ORA_PWD" value="tiger"/>
<property name="ORA_RESTORE_FILE" value="db_back.dmp"/>
<target name="demo" >
<echo> ant db_backup to backup db </echo>
<echo> ant db_restore to restore db </echo>
<echo> ant db_start to start db </echo>
<echo> ant db_stop to stop db </echo>
</target>
<target name="db_backup">
<tstamp>
<format property="DB_BACKUP_TIME" pattern="yyyy-MM-dd"/>
</tstamp>
<property name="DB_BACKUP_FILE" value="MYDB_${DB_BACKUP_TIME}.DMP" />
<echo>will backup db at ${DB_BACKUP_FILE}</echo>
<exec dir="${DB_BACKUP_DIR}" executable="exp" >
<arg line=" ${ORA_USER}/${ORA_PWD}@${ORA_SID} file=MYDB_${DB_BACKUP_TIME}.DMP "/>
</exec>
</target>
<target name="db_restore">
<exec dir="${DB_BACKUP_DIR}" executable="imp" >
<arg line=" ${ORA_USER}/${ORA_PWD}@${ORA_SID} file=${ORA_RESTORE_FILE} full=y"/>
</exec>
</target>
<target name="db_start">
<exec executable="lsnrctl" >
<arg line=" start "/>
</exec>
<exec executable='cmd' >
<arg line="/c net start oracleservice${ORA_SID} "/>
</exec>
</target>
<target name="db_stop">
<exec executable="lsnrctl" >
<arg line=" stop "/>
</exec>
<exec executable='cmd' >
<arg line="/c net stop oracleservice${ORA_SID} "/>
</exec>
</target>
</project>
1. 启动监听:
<exec executable="lsnrctl" >
<arg line=" start "/>
</exec>
2. 打开数据库实例:
<exec executable='cmd' >
<arg line="/c net start oracleservice${ORA_SID} "/>
</exec>
第一个操作在这里没有什么区别,就是ant调用系统命令的标准用法,我们来看一下第二个命令, net start oracleservice${ORA_SID},如果你直接放入到executable属性中,执行结果一定会是:
BUILD FAILED: E:\java\TestAnt\build.xml:44: Execute failed: java.io.IOException: CreateProcess: "net start oracleservicemy_oracle" error=2
痛苦吧,带有空格的命令应该使用以下方法:
<exec executable='cmd' >
<arg line="/c net start oracleservice${ORA_SID} "/>
</exec>
使用 tstamp target可以非常方便的定义日期属性:
<tstamp>
<format property="DB_BACKUP_TIME" pattern="yyyy-MM-dd"/>
</tstamp>
附录:参考脚本
<!-- 2005 by guipei. -->
<project name="TestAntOracle" default="demo" basedir="./">
<!-- set global properties for this build -->
<property name="DB_BACKUP_DIR" value="." />
<property name="ORA_SID" value="my_oracle"/>
<property name="ORA_USER" value="scott"/>
<property name="ORA_PWD" value="tiger"/>
<property name="ORA_RESTORE_FILE" value="db_back.dmp"/>
<target name="demo" >
<echo> ant db_backup to backup db </echo>
<echo> ant db_restore to restore db </echo>
<echo> ant db_start to start db </echo>
<echo> ant db_stop to stop db </echo>
</target>
<target name="db_backup">
<tstamp>
<format property="DB_BACKUP_TIME" pattern="yyyy-MM-dd"/>
</tstamp>
<property name="DB_BACKUP_FILE" value="MYDB_${DB_BACKUP_TIME}.DMP" />
<echo>will backup db at ${DB_BACKUP_FILE}</echo>
<exec dir="${DB_BACKUP_DIR}" executable="exp" >
<arg line=" ${ORA_USER}/${ORA_PWD}@${ORA_SID} file=MYDB_${DB_BACKUP_TIME}.DMP "/>
</exec>
</target>
<target name="db_restore">
<exec dir="${DB_BACKUP_DIR}" executable="imp" >
<arg line=" ${ORA_USER}/${ORA_PWD}@${ORA_SID} file=${ORA_RESTORE_FILE} full=y"/>
</exec>
</target>
<target name="db_start">
<exec executable="lsnrctl" >
<arg line=" start "/>
</exec>
<exec executable='cmd' >
<arg line="/c net start oracleservice${ORA_SID} "/>
</exec>
</target>
<target name="db_stop">
<exec executable="lsnrctl" >
<arg line=" stop "/>
</exec>
<exec executable='cmd' >
<arg line="/c net stop oracleservice${ORA_SID} "/>
</exec>
</target>
</project>