Ant: scp create directory if not exist
我用这个从A复制到B。但是,例如,如果文件夹/配置不存在,它会给我一个异常。我怎么解决?我试过mkdirs="true",但也会出错。
1 2 3 4 5 | <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> |
在应对之前,可以使用sshexec在远程计算机上创建目录树。mkdir-p如果不存在,则创建目录。(仅当目录不存在时,如何使用mkdir?)
1 2 3 4 5 6 | <sshexec host="${host}" username="${remote_user}" password="${remote_password}" command="mkdir -p ${remote_dir_path}" trust="true" /> |
在SCP之前将其添加到目标中
不能用scp任务创建远程目录。这是scptransfer的macrodef,它可以完成传输所需的所有工作。从给定的StageDirectory=>修复换行符、CreateMemote、DeleteMemote,如果StageDir为空则失败…等。macrodef使用的是ant addon flaka,如果您不需要所有的铃声和口哨,只需在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | <project xmlns:fl="antlib:it.haefelinger.flaka"> <fl:install-property-handler/> <macrodef name="scptransfer"> <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,最近才在这里获得手册和一些示例。