Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy linked content files using .NET Core csproj?

I'm referencing this article in an attempt to copy linked files before each build:

<Target Name="CopyLinkedContentFiles" BeforeTargets="Build">
    <Copy SourceFiles="%(Content.Identity)" 
          DestinationFiles="%(Content.Link)" 
          SkipUnchangedFiles='true' 
          OverwriteReadOnlyFiles='true' 
          Condition="'%(Content.Link)' != ''" />
 </Target>

This doesn't appear to work for the new .NET Core csproj tooling. What would be an equivalent target that does?

EDIT: Example csproj content

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <Features>IOperation</Features>
    <Configurations>Debug;Release;Template</Configurations>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="..\MyRefProject\ChangeTypes.cs" Link="ChangeTypes.cs" />
    <Compile Include="..\MyRefProject\Parser.cs" Link="Parser.cs" />
    <Compile Include="..\MyRefProject\Calculator.cs" Link="Calculator.cs" />
    <Compile Include="..\MyRefProject\Converter.cs" Link="Converter.cs" />
  </ItemGroup>
</Project>
like image 565
ket Avatar asked Jan 22 '26 04:01

ket


1 Answers

A Condition should be specified at the Target level but this won't help in this scenario because you can't use an elements metadata as a condition.

You also need to provide an output path. Your example will only copy linked content into the project's directory, not it's output directory.

<Target Name="CopyLinkedContentFiles" BeforeTargets="Build">
<Copy SourceFiles="%(Content.Identity)" 
        DestinationFiles="$(OutputPath)\%(Content.Link)" 
        SkipUnchangedFiles="true" 
        OverwriteReadOnlyFiles="true" />
</Target>

The documentation for the Copy task can be found here.

I tested this using Visual Studio 2017 with two simple console projects. The second project included a couple of text files. The first project also references these files via content links. This is the .csproj file from the first project:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp2.0</TargetFramework>
    </PropertyGroup>
    <ItemGroup>
        <Content Include="..\Project2\ReadMeFirst.txt" Link="ReadMeFirst.txt" />
        <Content Include="..\Project2\ReadMeSecond.txt" Link="ReadMeSecond.txt" />
    </ItemGroup>
    <Target Name="CopyLinkedContentFiles" BeforeTargets="Build">
        <Copy SourceFiles="%(Content.Identity)" 
            DestinationFiles="$(OutputPath)\%(Content.Link)" 
            SkipUnchangedFiles="true" 
            OverwriteReadOnlyFiles="true" />
    </Target>
</Project>
like image 196
Marc LaFleur Avatar answered Jan 23 '26 18:01

Marc LaFleur