通过ftp MsBuild任务部署生成

通过ftp MsBuild任务部署生成,ftp,msbuild,msbuild-task,msbuildextensionpack,Ftp,Msbuild,Msbuild Task,Msbuildextensionpack,我正在尝试在本地发布和文件转换后自动上传ftp版本。我已经查看了ftp msbuild任务,但它仅在文件深度为1级且不支持创建子目录时才显示为上载文件。我已经搜索了很多年,但找不到任何让它创建目录树的示例: <MSBuild.ExtensionPack.Communication.Ftp TaskAction="UploadFiles" Host="$(FtpHost)" UserName="$(FtpUserName)" UserPassword="$(FtpPassword)" Fil

我正在尝试在本地发布和文件转换后自动上传ftp版本。我已经查看了ftp msbuild任务,但它仅在文件深度为1级且不支持创建子目录时才显示为上载文件。我已经搜索了很多年,但找不到任何让它创建目录树的示例:

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


我不能使用Msdeploy进行ftp,因为我必须先构建然后转换一些文件。我知道不推荐使用msbuild社区任务来代替扩展包(如上所述),所以我不想使用它。在最坏的情况下,我会考虑通过CLI使用FTP客户端,但如果可能的话,更喜欢使用标准MSBug库。还有什么方法可以让我这样做吗?

这个任务似乎有一个递归标记


尝试使用类似()的命令行工具,并使用TFS Build workflow中的Invoke Process activity来运行该命令行

为什么您认为“msbuild社区任务已被弃用”?上次活动是25天前。我从你说的话中推断,现在不是。我问了一个问题,因为我试图理解为什么有两个,为什么功能上有重叠,以及它们之间的关系。只有两种不同的努力来填补msbuild功能上的一些空白。微软做了他们的事,其他人开始了一个开源库。。。。。。。。。它们有些重叠,有些具有独特的功能。它们之间没有关系,只是每个库都试图提供有用且可重用的公共msbuild例程。微软一开始规模很小,多年来一直在增长。但我仍然每天使用一个社区。
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();
                }
            }
        }
    }
}