Ant:如果不存在,则创建目录
Posted
技术标签:
【中文标题】Ant:如果不存在,则创建目录【英文标题】:Ant: scp create directory if not exist 【发布时间】:2016-02-02 07:23:32 【问题描述】:我正在使用它从 A 复制到 B。 但是,例如,如果文件夹 /config 不存在,它会给我一个例外。我该如何解决?我试过 mkdirs="true" 但它也会出错
<scp todir="$ftplogin@$ftpserver:$ftp-remote-dir/config" verbose="false" trust="true" failonerror="No">
<fileset dir="$stuff/$stuff-version/config$config-files">
<include name="*.*"/>
</fileset>
</scp>
【问题讨论】:
【参考方案1】:无法使用 scp 任务创建远程目录。
这是 scptransfer 的宏定义,它完成了传输所需的所有工作
从给定的 stagedirectory => 修复换行,createremote,deleteremote,如果 stagedir 为空则失败 ... 等等。
Macrodef 正在使用 ant 插件 Flaka,如果您不需要所有的花里胡哨,只需使用 sshexec task 和 mkdir -p
在 scp 之前创建您的远程目标目录。
<project xmlns:fl="antlib:it.haefelinger.flaka">
<fl:install-property-handler/>
<macrodef name="scptransfer">
<attribute name="host" default="$aix.host"/>
<attribute name="userid" default="$aix.userid"/>
<attribute name="knownhosts" default="$aix.knownhosts"/>
<attribute name="ppk" default="$aix.ppk"/>
<attribute name="createremote" default="false"/>
<attribute name="remotedir" default="$eartarget/$project/$env/$module/$job.id"/>
<attribute name="deleteremote" default="false"/>
<attribute name="deleteincludes" default="*"/>
<attribute name="stagedir" default="$artifactdir/$module"/>
<attribute name="stageincludes" default="**/*.*"/>
<attribute name="stageexcludes" default=""/>
<attribute name="failstageempty" default="true"/>
<attribute name="fixLF" default="false"/>
<attribute name="timeout" default="900000"/>
<attribute name="verbose" default="true"/>
<sequential>
<echo>
=============== SCP Transfer ===============
Project = $project
Environment = $env
UserID = @userid
Targetserver = @host
Targetpath = @remotedir
fixLF ? = @fixLF
createremote ? = @createremote
deleteremote ? = @deleteremote
#@deleteremote ? 'deleteincludes = @deletecincludes' : ''
Stagedir = @stagedir
stageincludes = @stageincludes
stageexcludes = @stageexcludes
=============== SCP Transfer ===============
</echo>
<!-- contents in stagedir ? -->
<resourcecount property="stagecount">
<fileset dir="@stagedir" includes="@stageincludes" excludes="@stageexcludes" id="stagecontents"/>
</resourcecount>
<fl:choose>
<fl:when test=" $stagecount > 0 ">
<!-- Fix Linefeeds for ASCII Files -->
<fl:when test=" @fixLF ">
<fixcrlf
excludes="**/*.jar **/*.tar **/*.zip **/*.ear **/*.class"
srcdir="@stagedir"
eol="lf"
eof="remove"
fixlast="true"
/>
</fl:when>
<!-- // T i m e o u t -->
<parallel threadcount="1" timeout="@timeout">
<!-- create remotedir ? -->
<fl:when test=" @createremote ">
<sshexec host="@host"
username="@userid"
knownhosts="@knownhosts"
keyfile="@ppk"
command="mkdir -p @remotedir"
/>
</fl:when>
<!-- delete contents in remotedir ?
will throw error if remotedir doesn't exist ! -->
<fl:when test=" @deleteremote ">
<sshexec host="@host"
username="@userid"
knownhosts="@knownhosts"
keyfile="@ppk"
command="cd @remotedir;rm -rfe @delfilepattern"
/>
</fl:when>
<!-- Filetransfer from stagedir -->
<scp todir="@userid@@@host:@remotedir"
keyfile="@ppk"
knownhosts="@knownhosts"
sftp="true"
verbose="@verbose"
>
<fileset refid="stagecontents"/>
</scp>
</parallel>
<!-- T i m e o u t // -->
</fl:when>
<fl:otherwise>
<echo>
=============== SCP Transfer =============
Skip => NO StageDirContents !!
=============== SCP Transfer =============
</echo>
<fl:fail message="Stagedir [@stagedir] empty !!" test=" @failstageempty "/>
</fl:otherwise>
</fl:choose>
</sequential>
</macrodef>
</project>
由于我们最近才切换到 Github,因此获得了手册 here 和一些示例 here。
【讨论】:
【参考方案2】:您可以在应对之前使用sshexec 在远程机器上创建目录树。 mkdir -p 如果不存在则创建目录。 (How to mkdir only if a dir does not already exist?)
<sshexec
host="$host"
username="$remote_user"
password="$remote_password"
command="mkdir -p $remote_dir_path"
trust="true" />
在 scp 之前将其添加到您的目标
【讨论】:
以上是关于Ant:如果不存在,则创建目录的主要内容,如果未能解决你的问题,请参考以下文章
MATLAB。写入文本文件或创建它,如果它不存在。将图形保存在目录中,如果不存在则创建它