Rand Random
Rand Random

Reputation: 7440

msbuild target - AfterTargets="Build" for the solution?

I am looking for a way to run my defined Target only once per build process and not for every project that gets build.

I have defined the following in Directory.Build.props

<Target Name="MyTarget" AfterTargets="AfterBuild" >

    <Message Text="Hello World!!!" Importance="High" />

</Target>

Should only run once no matter how many projects the msbuild process is building, currently it happens for each project.

It shouldn't matter if I hit (Re-)Build Soltution or (Re-)Build [ProjectName] or hit F5 in Visual Studio, as long any build happens I want to exectue MyTarget only once.

Upvotes: 0

Views: 3262

Answers (1)

Mr Qian
Mr Qian

Reputation: 23715

Just answer this situation:

If my guess is right, pure msbuild function is not enough and you have to use a external file to help it work.

create a file called test.txt on the solution folder and write 0 in the txt file.

Then, modify your Directory.Build.props like this:

<Project>
<PropertyGroup>
  <Record></Record>
</PropertyGroup>
<ItemGroup>
    <File Include="..\test.txt"></File>
</ItemGroup>
    <Target Name="GetConditionValue" BeforeTargets="PrepareForBuild">
  
    <ReadLinesFromFile File="@(File)">
        <Output TaskParameter="Lines" PropertyName="Record"/>
      </ReadLinesFromFile>
    </Target>
    
    
<Target Name="MyTarget" AfterTargets="Build" Condition="'$(Record)'=='0'">

 <WriteLinesToFile File="@(File)" Lines="2" Overwrite="true"></WriteLinesToFile>

  <Message Text="Hello World!!!" Importance="High" />
  </Target>

</Project>

When you start a new build process, you should clear the test.txt file to 0 to make a new start.

Upvotes: 1

Related Questions