【问题标题】:MS Build configuration to copy additional files when deploying console appMS Build 配置以在部署控制台应用程序时复制其他文件
【发布时间】:2026-01-30 17:25:02
【问题描述】:

我有一个 .net 控制台应用程序,我将它部署到 Team City CI 环境中的服务器。构建过程使用 MSBuild 配置,应用程序使用 MSDeploy 部署。

应用程序本身部署正常,但我现在想将一组模板(文件)部署到同一个目标目录。我有一个 WebApi 应用程序,它使用 MSBuild WebApplication.targets 成功部署了相同的组件以及模板,并按照 this post 锁定到 CopyAllFilesToSingleFolderForMsdeploy 目标。但是,我无法让这种方法适用于控制台应用程序。

我还在AfterBuild 目标中尝试了简单的文件副本。

.csproj 项目文件的相关部分如下所示:

<Target Name="AfterBuild" Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <PropertyGroup>
        <TemplatePath>$([System.IO.Path]::Combine($([System.IO.Path]::GetDirectoryName($(MSBuildProjectDirectory))), `MyApp.MyComponent\Messages\Views`))</TemplatePath>
    </PropertyGroup>
    <ItemGroup>
        <Templates Include="$(TemplatePath)\**\*.cshtml" />
    </ItemGroup>
    <Message Text="Template Path = $(TemplatePath)" Importance="high" />
    <Copy SourceFiles="@(Templates)" DestinationFolder="$(OutputPath)\Templates\%(Templates.RecursiveDir)" />
</Target>

我做错了吗?这似乎是一件很简单的事情,但我找不到让它工作的方法。

【问题讨论】:

    标签: .net msbuild teamcity msdeploy


    【解决方案1】:

    这最终奏效了:

    <PropertyGroup>
        <GetCopyToOutputDirectoryItemsDependsOn>
            $(GetCopyToOutputDirectoryItemsDependsOn);
            CustomCollectFiles
        </GetCopyToOutputDirectoryItemsDependsOn>       
    </PropertyGroup>
    <Target Name="CustomCollectFiles">
        <PropertyGroup>
            <TemplatePath>$([System.IO.Path]::Combine($([System.IO.Path]::GetDirectoryName($(MSBuildProjectDirectory))), `MyApp.MyComponent\Messages\Views`))</TemplatePath>
        </PropertyGroup>
        <Message Text="Template Path = $(TemplatePath)" Importance="high" />
        <ItemGroup>
            <CustomFilesToInclude Include="$(TemplatePath)\**\*.cshtml" />
        </ItemGroup>
        <Message Text="CustomCollectFiles = @(CustomFilesToInclude)" Importance="high" />
        <Copy SourceFiles="@(CustomFilesToInclude)" DestinationFolder="$(OutputPath)\Templates\%(CustomFilesToInclude.RecursiveDir)" />
    </Target>
    

    【讨论】:

      最近更新 更多