How to execute msbuild (csproj) targets based on input files being out of date

Anurag Shekhar Sharma 21 Reputation points
2021-11-15T06:54:44.447+00:00

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.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
Not Monitored
Not Monitored
Tag not monitored by Microsoft.
35,960 questions
0 comments No comments
{count} votes

Accepted answer
  1. PengGe-MSFT 3,331 Reputation points
    2021-11-16T07:12:01.113+00:00

    Hi, @Anurag Shekhar Sharma

    Welcome to Microsoft Q&A!

    You can try use AfterTargets.
    This is my csproj file:

    <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">  
    <PropertyGroup>  
      <AssemblyName>MSBuildSample</AssemblyName>  
      <OutputPath>Bin\</OutputPath>  
    </PropertyGroup>  
      
    <ItemGroup>  
      <Compile Include="helloworld.cs" />  
    </ItemGroup>  
    <ItemGroup>  
        <ins Include="a.txt" />  
        <outs Include="b.txt" />  
    </ItemGroup>  
      
    <Target Name="Build" Inputs="@(Compile)" Outputs="$(OutputPath)$(AssemblyName).exe">  
    <MakeDir Directories="$(OutputPath)"      Condition="!Exists('$(OutputPath)')" />  
    <Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" />  
    </Target>  
      
    <Target Name="ExecuteTarget" Inputs="@(ins)" Outputs="@(outs)" AfterTargets="Build">  
        <Exec Command="echo Well Done" />  
    </Target>  
      
    </Project>  
    

    149620-image.png

    Sincerely,
    Peng
    *
    If you have extra questions about this answer, please click "Comment". If the answer is the right solution, please click "Accept Answer" and kindly upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anurag Shekhar Sharma 21 Reputation points
    2021-11-16T07:18:35.08+00:00

    Thanks @PengGe-MSFT . One follow-up query - how do I ensure that this targets always gets executed in VS IDE build regardless of FUTD (fast-up-to-date) being enabled or disabled?


  2. Anurag Shekhar Sharma 21 Reputation points
    2021-11-16T10:25:36.25+00:00

    No I meant incremental build.