S-JIS[2008-09-23/2010-02-28] 変更履歴

Ant:exec

OS依存のコマンド(ファイル)を実行するコアタスク。
(org.apache.tools.ant.taskdefs.Exec extends Task

最新exec


属性

属性 説明 更新日
executable 実行するコマンド(引数を含まない)  
dir コマンドを実行するディレクトリー。(実行時のカレントディレクトリー。Javaのuser.dir  
spawn trueにすると、別プロセスで実行する。デフォルトはfalse  
outputproperty 標準出力の内容を、ここで指定されたプロパティーに設定する。 2010-02-28

ボディー部に入れられるタグ

タグ 説明 備考 更新日
<arg value="引数1つ" /> コマンドの引数を1つ指定する。    
<arg line="引数複数" /> コマンドの引数を複数指定する。 スペース区切り  
<env key="環境変数名" value="値"> 環境変数を指定する。 JBossでJAVA_OPTSを指定する例 2008-11-08

Windowsの例

JBossのrun.batを実行する例。

<?xml version="1.0" encoding="Shift_JIS"?>
<project name="jboss" basedir=".." default="run">
	<property name="jboss.home" location="C:/jboss/jboss-4.2.3.GA" />

	<target name="run">
		<exec executable="${jboss.home}/bin/run.bat" />
	</target>
</project>

Eclipseのantで実行すると、Eclipseのコンソール上にメッセージ類が表示される。
そのコンソール上のコマンドが終了するまで、ant実行自体も終了しない。


spawn属性をtrueにして別プロセスにしてみると、ant実行はすぐ終了するが、どこにもコンソールが表示されない(Eclipseのコンソール上にも表示されない…)。

	<target name="run">
		<exec executable="${jboss.home}/bin/run.bat" spawn="true" />
	</target>

WindowsXPでバッチファイルを別プロセスで実行するには、startコマンドを利用できる。ただしstartは内部コマンドなので、execタスクから直接実行する事は出来ない。cmd.exeを介してやる必要がある。
また、(ついでに)run.batに引数「-c default」を指定してみた。

	<target name="run">
		<exec executable="cmd" spawn="true">
			<arg value="/c" />
			<arg value="start" />
			<arg line="${jboss.home}/bin/run.bat -c default" />
		</exec>
	</target>

Cygwinの例

Cygwinのコマンドを実行する例。[2010-02-21]

実体がexeファイルのものは、そのまま実行することが出来る。
シェルスクリプトの場合は、/bin/bash.exeを経由して実行する。その際、環境変数等が必要であれば、--loginオプションを付ける。

  build.xml 実行結果の例
 
<property name="cygwin" location="C:\cygwin" />
 
exe
<target name="execute pwd">
	<exec executable="${cygwin}/bin/pwd.exe" />
</target>

 

execute pwd:
     [exec] /cygdrive/c/workspace/sample/bin
<target name="execute ls">
	<exec executable="${cygwin}/bin/ls.exe">
		<arg value="-l" />
		<arg value="/usr/local" />
	</exec>
</target>
execute ls:
     [exec] total 2
     [exec] drwxrwx---+ 1 hishidama Users 0 2008-10-17 00:34 bin
     [exec] drwxrwx---+ 1 hishidama Users 0 2008-10-17 00:34 etc
     [exec] drwxrwx---+ 1 hishidama Users 0 2008-10-17 00:34 lib
<target name="execute ls2">
	<exec executable="${cygwin}/bin/ls.exe" dir="${cygwin}/usr/local">
		<arg value="-l" />
	</exec>
</target>
execute ls2:
     [exec] total 2
     [exec] drwxrwx---+ 1 hishidama Users 0 2008-10-17 00:34 bin
     [exec] drwxrwx---+ 1 hishidama Users 0 2008-10-17 00:34 etc
     [exec] drwxrwx---+ 1 hishidama Users 0 2008-10-17 00:34 lib
<target name="cygpath"> [2010-02-28]
	<exec executable="${cygwin}/bin/cygpath.exe" outputproperty="zzz">
		<arg value="-u" />
		<arg value="C:\temp" />
	</exec>
	<echo message="${zzz}" />
</target>
cygpath:
     [echo] /cygdrive/c/temp
sh
<target name="execute test.sh">
	<exec executable="${cygwin}/bin/bash.exe">
		<arg value="/tmp/test.sh" />
		<arg value="abc" />
	</exec>
</target>
/tmp/test.sh:
#!/bin/bash
echo zzzz $1
execute test.sh:
     [exec] zzzz abc
<target name="execute user.sh">
	<exec executable="${cygwin}/bin/bash.exe">
		<arg value="/tmp/user.sh" />
	</exec>
</target>

/tmp/user.sh:

#!/bin/bash
echo aaa $USER zzz
execute user.sh:
     [exec] aaa zzz
<target name="execute user.sh login">
	<exec executable="${cygwin}/bin/bash.exe">
		<arg value="--login" />
		<arg value="/tmp/user.sh" />
	</exec>
</target>
execute user.sh login:
     [exec] aaa hishidama zzz

Ant目次へ戻る / 技術メモへ戻る
メールの送信先:ひしだま