啟動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>