2015-11-02 92 views
1

我正在使用它從A複製到B. 但是,如果例如/ config文件夾不存在,它會給我一個例外。我該如何解決它?我試圖mkdirs =「真」,但它運行到錯誤以及Ant:如果不存在scp創建目錄

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

回答

0

這是不可能建立與SCP任務遠程目錄。 這裏是一個scptransfer的宏指令,它可以完成從給定的stagedirectory傳輸 所需的所有工作=>修復換行,createremote,deleteremote,ifdist如果stagedir爲空......等等 Macrodef正在使用ant插件Flaka, t需要所有的花裏胡哨的東西,只需在scp之前使用sshexec taskmkdir -p 來創建遠程目標。

<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

1

在應對之前,您可以使用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

之前添加到您的目標