Ant has no problem in executing system commands. In this actual system command, two problems are encountered: one is that the command for starting the service contains spaces, and the other is how to back up the database and add the date automatically.
First, we start the Oracle database with two operations:
1. Start the listener:
<Exec executable =/"LSNRCTL/">
<Arg line =/"Start/"/>
</Exec>
2. Open the database instance:
<Exec executable = 'cmd'>
<Arg line =/"/C Net start oracleservice $ {ora_sid}/"/>
</Exec>
There is no difference in the first operation here, that is, the standard usage of ant to call system commands. Let's take a look at the second command, Net start oracleservice $ {ora_sid }, if you directly put it in the executable attribute, the execution result will certainly be:
Build failed: e:/Java/testant/build. xml: 44: Execute failed: Java. Io. ioexception: CreateProcess:/"Net start oracleservicemy_oracle/" error = 2
Network Management ujia u.bitscn.com
The following method should be used for a command with spaces:
<Exec executable = 'cmd'>
<Arg line =/"/C Net start oracleservice $ {ora_sid}/"/>
</Exec>
If I add a time mark when backing up the database and use the tutorial to back up the Oracle database under Linux, I can use the data command to conveniently Add a date suffix to the backup file. Can ant?
I checked a lot of information and finally got it done:
<Tstamp>
<Format property =/"db_backup_time/" pattern =/"yyyy-mm-dd/"/>
</Tstamp>
You can use tstamp target to easily define the date attribute, and then generate the name of the backup file to solve your problem.
OK. You can refer to the complete ant script:
Appendix: Reference script
<! -- 2005 by guipei. -->
<Project name =/"testantoracle/" default =/"demo/" basedir =/". //">
<! -- Set global properties for this build -->
<Property name =/"db_backup_dir/" value =/"./"/> China Network Management Alliance bitscn.com
<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/"/>
Network Management Alliance bitscn @ com
</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>
[Transferred from www.bitscn.com]