本文介绍了Ant:scp创建目录(如果不存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用它从A复制到B.但是,例如,如果文件夹/config不存在,它将给我一个例外.我该如何解决?我尝试了mkdirs ="true",但是它也运行错误

I am using this to copy from A to B.But if for example the folder /config doesnt exist it gives me an exception. How can I solve it? I tried mkdirs="true" but it runs to error as well

<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>

推荐答案

无法使用scp任务创建远程目录.这是用于scptransfer的macrodef,它完成了转移所需的所有工作从给定的stagedirectory =>修复换行,createremote,deleteremote,如果stagedir为空...则失败...Macrodef正在使用ant插件 Flaka ,如果您不需要所有的花哨功能,只需使用 sshexec任务mkdir -p在scp之前创建您的远程targetdir.

It's not possible to create remote directories with scp task.Here's a macrodef for scptransfer, that does all the work needed for transferfrom a given stagedirectory => fix linefeeds, createremote, deleteremote, fail if stagedir empty ... etc.Macrodef is using ant addon Flaka, if you don't need all that bells and whistles, simply use sshexec task with mkdir -pbefore scp to create your remote targetdir.

<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,因此在此处和一些示例此处.

As we switched to Github only recently get the manual here and some examples here.

这篇关于Ant:scp创建目录(如果不存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 20:56