2

I'm required to add a target to my csproj file which specifies input files as well as output files. Going by this link, I tried to define my target like so:

<ItemGroup>
    <ins Include="A.txt" />
    <outs Include="B.txt" />
</ItemGroup>

<Target Name="ExecuteTarget" Inputs="@(ins)" Outputs="@(outs)">
    <Exec Command="echo hello world" />
</Target>

The intention here is to echo hello world in the Visual Studio output window every time the csproj is built in VS IDE if and only if the file A.txt is newer than B.txt OR B.txt does not exist. Both A.txt and B.txt exist in the same directory as the csproj file.

However, this does not seem to work as I'm not able to get the string "hello world" to print in the output window when I build this csproj. I'm using VS 16.11.5 along with .NET 5.0 enabled csproj (WinUI).

Any help/ideas/suggestions will be greatly appreciated.

Anurag S Sharma
  • 400
  • 2
  • 12
  • A couple of things to check: - [Crank up the output verbosity](https://stackoverflow.com/a/1211886/21567) of MSBuild inside the IDE. - Then check the output window if your target is actually called or not (or even considered: MSBuild will write a message like inputs/outputs out of date, etc.). Running your project file from the command line, w/o providing either ins nor outs prints the message, which would be expected behavior (in absence of all ins/outs). – Christian.K Nov 15 '21 at 04:59

1 Answers1

2

A target has to be called somewhere, just having inputs and outputs isn't sufficient. Couple of ways to do that: call it explicitly with CallTarget, or make it a dependency of another target (so another target which does get called has Dependencies="ExecuteTarget"), or use AfterTargets, etc.

E.g. <Target Name="ExecuteTarget" Inputs="@(ins)" Outputs="@(outs)" AfterTargets="CoreCompile"> should do it, and the target will be skipped if B.txt exists and is newer than A.txt. I'd suggest using a more meaningful name for the target though.

stijn
  • 34,664
  • 13
  • 111
  • 163