2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Antで@を連続して書く方法

Posted at

現象

ftpからscpに書き換えるときに困ったことが起きました。
for文にてパラメータを指定する場合は@{param名}となるのですが、scpにてhostを指定する場合にも@が必要になります。
単純に@@と書くと1つの@が認識されずエラーになってしまいました。

解決方法

@を3つ書けば動きました。

ftp

    <target name="TEST-hoge" description="Test" depends="Test-hoge">
        <for list="${deploy.hosts}" param="deploy.host">
            <sequential>
                <ftp server="@{deploy.host}" userid="${user.id}" password="${password}" remotedir="${home}" verbose="yes">
                    <fileset refid="xxx_transfer" />
                </ftp>
            </sequential>
        </for>
    </target>

scp

    <target name="TEST-hoge" description="Test" depends="Test-hoge">
        <for list="${deploy.hosts}" param="deploy.host">
            <sequential>
                <scp todir="${user.id}@@@{deploy.host}:${home}" password="${password}" trust="yes">
                    <fileset refid="xxx_transfer" />
                </scp>
            </sequential>
        </for>
    </target>

補足説明

host名をパラメータにしたことで@が認識されなくて困ったというお話でした。
deploy先のサーバが複数あったためfor文を使いました。すっきり書けますね。
またサーバの増減があったときも設定ファイルを修正するだけでよくなります。
以下にfor文とscpの説明をかんたんに書いておきます。

for文の書き方

みたままですが、paramを使う場合は@{param名}で値を取ることができます。
(以下はあまり意味のないタスクとなってます)

    <target name="TEST-hoge" description="Test" depends="Test-hoge">
        <for list="${deploy.hosts}" param="deploy.host">
            <sequential>
                <param name="host" value="@{deploy.host}" />
            </sequential>
        </for>
    </target>

scpの書き方

todirには「ユーザ名@ホスト名:ディレクトリ」のフォーマットで書きます。

    <target name="TEST-hoge" description="Test" depends="Test-hoge">
        <scp todir="${user.id}@${host1}:${home}" password="${password}" trust="yes">
            <fileset refid="xxx_transfer" />
        </scp>
    </target>
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?