简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM