Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use my .targets file in Visual Studio with custom build actions?

I am a beginner with MSBuild. So far I have been able to create a custom task called 'MakeTextFile' which creates a text file in C:\ based on the contents property you pass it. This works running from a command line prompt.

I have also included this in my .targets file (under the project tag):

<ItemGroup>
    <AvailableItemName Include="CreateTextFileAction" />
</ItemGroup>

When I use the Import tag on my client applications .csproj I can now set items build actions to 'CreateTextFileAction', however the action never triggers (as no text file on C:\ is created)

How do I get all the file paths of items that were marked with my build action 'CreateTextFileAction' and pass them onto my custom task?

For reference, my .targets file:

<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <AvailableItemName Include="CreateTextFileAction" />
    </ItemGroup>

    <UsingTask AssemblyFile="CustomMSBuildTask.dll" TaskName="CustomMSBuildTask.MakeTextFile" />


   <Target Name="MyTarget">
       <MakeTextFile Contents="TODO HOW DO I GRAB MARKED FILES?" />
   </Target>
</Project>
like image 428
James Avatar asked Oct 08 '22 10:10

James


1 Answers

A csproj file has a defined set of targets. The three main entry points are Build, Rebuild and Clean. These targets each have a set of dependencies. If you write your own targets to be part of the standard csproj build you need to find a suitable injection point within these dependencies.

For ease of use there are two standard targets for you to override called BeforeBuild and AfterBuild. If you define this in the csproj file (after the import of the csharp targets file) and call your custom task in there then it should work (or at least move further along).

   <Target Name="BeforeBuild">
       <MakeTextFile Contents="TODO HOW DO I GRAB MARKED FILES?" />
   </Target>
like image 131
daughey Avatar answered Oct 11 '22 00:10

daughey