Search code examples
.netmsbuildteamcitymsdeploy

MS Build configuration to copy additional files when deploying console app


I have a .net console application which I deploy to a server in a Team City CI environment. The build process is configured using MSBuild and the application is deployed using MSDeploy.

The application itself deploys ok, but I now want to deploy a collection of templates (files) to the same target directory. I have a WebApi application that successfully deploys the same component along with the templates using the MSBuild WebApplication.targets, latching on to the CopyAllFilesToSingleFolderForMsdeploy target as per this post. However, I can't get this approach to work for a console app.

I have also tried a straightforward file copy in the AfterBuild target.

The relevant section of the .csproj project file looks like this:

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

Am I doing this wrong? It seems a straightforward enough thing to want to do, but I can't find a way to get it to work.


Solution

  • This worked in the end:

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