0

我正在尝试在本地发布和文件转换后自动执行构建的 ftp 上传。我已经查看了http://www.msbuildextensionpack.com/help/4.0.7.0/Index.html ftp msbuild 任务,但它似乎只在文件深度为 1 级且不支持创建子目录时才会上传。我已经搜索了很长时间,但找不到任何使其创建目录树的示例:

<MSBuild.ExtensionPack.Communication.Ftp TaskAction="UploadFiles" Host="$(FtpHost)" UserName="$(FtpUserName)" UserPassword="$(FtpPassword)" FileNames="@(PublishFiles)" />

我不能使用 Msdeploy 来 ftp,因为我必须构建,然后转换一些文件。我知道 msbuild 社区任务已被弃用以代替扩展包(上图),所以我不想使用它。在最坏的情况下,我会考虑通过 CLI 使用 ftp 客户端,但如果可能的话,我更愿意使用标准的 msbuild 库。有没有办法让我这样做?

4

2 回答 2

0

这个任务似乎有一个递归标签。

https://github.com/loresoft/msbuildtasks/blob/master/Source/MSBuild.Community.Tasks/Ftp/FtpUploadDirectoryContent.cs

using System;
using System.IO;
using Microsoft.Build.Framework;

namespace MSBuild.Community.Tasks.Ftp
{
    /// <summary>
    /// Uploads a full directory content to a remote directory.
    /// </summary>
    /// <example>Uploads directory content, including all subdirectories and subdirectory content:
    /// <code><![CDATA[
    /// <Target Name="DeployWebsite">
    /// <FtpUploadDirectoryContent
    /// ServerHost="ftp.myserver.com"
    /// Port="42"
    /// Username="user"
    /// Password="p@ssw0rd"
    /// LocalDirectory="c:\build\mywebsite"
    /// RemoteDirectory="root\www\mywebsite"
    /// Recursive="true"
    /// />
    /// ]]></code>
    /// To go a little step further. If the local directory looked like this:
    /// <code>
    /// [mywebsite]
    /// [images]
    /// 1.gif
    /// 2.gif
    /// 3.gif
    /// [js]
    /// clientscript.js
    /// nofocus.js
    /// [css]
    /// print.css
    /// main.css
    /// index.htm
    /// contact.htm
    /// downloads.htm
    /// </code>
    /// All directories and there content will be uploaded and a excact copy of the content of <c>mywebsite</c> directory will be created remotely.
    /// <remarks>
    /// If <see cref="Recursive"/> is set the <c>false</c>; only index.htm, contact.htm and downloads.htm will be uploaded and no subdirectories will be created remotely.
    /// </remarks>
    /// </example>
    public class FtpUploadDirectoryContent : FtpClientTaskBase
    {
        private String _localDirectory;
        private String _remoteDirectory;
        private bool _recursive;

        /// <summary>
        /// Gets or sets the local directory that contains the content to upload.
        /// </summary>
        /// <value>The local directory.</value>
        public String LocalDirectory
        {
            get
            {
                return _localDirectory;
            }
            set
            {
                _localDirectory = value;
            }
        }

        /// <summary>
        /// Gets or sets the remote directory destination for the local files.
        /// </summary>
        /// <value>The remote directory.</value>
        public string RemoteDirectory
        {
            get
            {
                return _remoteDirectory;
            }
            set
            {
                _remoteDirectory = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the subdirectories of the local directory should be created remotely and the content of these should also be uploaded.
        /// </summary>
        /// <value><c>true</c> if recursive; otherwise, <c>false</c>.</value>
        public bool Recursive
        {
            get
            {
                return _recursive;
            }
            set
            {
                _recursive = value;
            }
        }

        /// <summary>
        /// When overridden in a derived class, executes the task.
        /// </summary>
        /// <returns>
        /// true if the task successfully executed; otherwise, false.
        /// </returns>
        public override bool Execute()
        {
            try
            {
                try
                {
                    Connect();
                    Log.LogMessage( MessageImportance.Low, "Connected to remote host." );
                }
                catch(Exception caught)
                {
                    Log.LogErrorFromException( caught, false );
                    Log.LogError( "Couldn't connect remote machine." );
                    return false;
                }

                try
                {
                    Login();
                    Log.LogMessage( MessageImportance.Low, "Login succeed." );
                }
                catch(Exception caught)
                {
                    Log.LogErrorFromException( caught, false );
                    Log.LogError( "Couldn't login." );
                    return false;
                }

                try
                {
                    if(!DirectoryExists( RemoteDirectory ))
                    {
                        Log.LogError( "Remote directory doesn't exist." );
                        return false;
                    }

                    ChangeWorkingDirectory( RemoteDirectory );
                }
                catch(FtpException caught)
                {
                    Log.LogErrorFromException( caught, false );
                    Log.LogError( "Couldn't change remote working directory." );
                    return false;
                }

                try
                {
                    UploadDirectory( LocalDirectory, "*.*", Recursive );
                }
                catch(FtpException caught)
                {
                    Log.LogErrorFromException( caught, false );
                    Log.LogError( "Couldn't upload directory." );
                    return false;
                }
            }
            finally
            {
                Close();
            }

            return true;
        }

        /// <summary>
        /// Upload a directory and its file contents.
        /// </summary>
        /// <param name="localPath">The local path.</param>
        /// <param name="recurse">if set to <c>true</c> all subdurectiries will be included.</param>
        protected void UploadDirectory( String localPath, Boolean recurse )
        {
            UploadDirectory( localPath, "*.*", recurse );
        }

        /// <summary>
        /// Upload a directory and its file contents.
        /// </summary>
        /// <param name="localPath">The local path.</param>
        /// <param name="mask">Only upload files that compli to the mask.</param>
        /// <param name="recursive">if set to <c>true</c> all subdurectiries will be included.</param>
        protected void UploadDirectory( String localPath, String mask, Boolean recursive )
        {
            Log.LogMessage( MessageImportance.Low, "Uploading files of local directory {0}.", localPath );


            foreach(string file in Directory.GetFiles( localPath, mask ))
            {
                String filename = Path.GetFileName( file );
                Store( file, filename );

                Log.LogMessage( MessageImportance.Low, "{0} uploaded succesfully.", localPath );
            }

            if(recursive)
            {
                foreach(String directory in Directory.GetDirectories( localPath ))
                {
                    DirectoryInfo directoryInfo = new DirectoryInfo( directory );
                    bool existsRemotely = DirectoryExists( directoryInfo.Name );

                    if(!existsRemotely)
                    {
                        MakeDirectory( directoryInfo.Name );
                    }

                    ChangeWorkingDirectory( directoryInfo.Name );

                    UploadDirectory( directory, mask, Recursive );

                    CdUp();
                }
            }
        }
    }
}
于 2013-12-02T14:56:51.590 回答
-1

尝试使用(NCFTP)之类的命令行工具并使用 TFS Build 工作流中的 Invoke Process 活动来运行该命令行

于 2015-05-28T13:52:55.300 回答