Always run post build event commands in visual studio 2017

无人久伴 提交于 2020-07-06 10:27:14

问题


Currently, when I run my project, it will execute my post-build commands that I have set up. However, this is only true if there was a change to the project. My ultimate goal here is to have my project run ng build each time I build it. However, what I have noticed is that if I were to change an HTML file in angular, the project does not detect any changes so it does not build again and thus it does not run my ng build command.

Is there a way to force it to always run post-build commands or maybe make it always rebuild, even if no changes are detected? Or maybe there is another way to accomplish this?

This is a .NET Core WebApp and the code to run my post build event is located inside my .csproj file

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="echo Building Angular App..." />
    <Exec Command="cd ClientApp &amp;&amp; ng build" />
</Target>

回答1:


Is there a way to force it to always run post-build commands or maybe make it always rebuild, even if no changes are detected?

The easiest way is set the property DisableFastUpToDateCheck to true in the project file to disable FastUpToDateCheck for Visual Studio build manager:

<PropertyGroup>
    <DisableFastUpToDateCheck>True</DisableFastUpToDateCheck>
</PropertyGroup>

Check MSDN about DisableFastUpToDateCheck:

A boolean value that applies to Visual Studio only. The Visual Studio build manager uses a process called FastUpToDateCheck to determine whether a project must be rebuilt to be up to date. This process is faster than using MSBuild to determine this. Setting the DisableFastUpToDateCheck property to true lets you bypass the Visual Studio build manager and force it to use MSBuild to determine whether the project is up to date.

Besides, if you want a way to separate build from post-build commands, you can use MSBuild command line build this project directly without adding above settings.

Hope this helps.




回答2:


You could configure your post build event slightly differently in your .csproj file and set RunPostBuildEvent to Always as per the below:

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    ...
    <PostBuildEvent>cd ClientApp &amp;&amp; ng build</PostBuildEvent>
    <RunPostBuildEvent>Always</RunPostBuildEvent>
  </PropertyGroup>

EDIT: As I discovered after a bit more testing, the RunPostBuildEvent does not behave as I expected it to. Therefore, a 'workaround' is to add DisableFastUpToDateCheck as per the below:

<PropertyGroup>
  <DisableFastUpToDateCheck>true</DisableFastUpToDateCheck>
  <PostBuildEvent>cd ClientApp &amp;&amp; ng build</PostBuildEvent>
</PropertyGroup>

From MSDN:

A boolean value that applies to Visual Studio only. The Visual Studio build manager uses a process called FastUpToDateCheck to determine whether a project must be rebuilt to be up to date. This process is faster than using MSBuild to determine this. Setting the DisableFastUpToDateCheck property to true lets you bypass the Visual Studio build manager and force it to use MSBuild to determine whether the project is up to date.

Clearly the downside to this is that the project will always be rebuilt, so this is not an ideal solution.




回答3:


I have solved this by adding the following properties to my project file

<RunPostBuildEvent>Always</RunPostBuildEvent> 
<DisableFastUpToDateCheck>true</DisableFastUpToDateCheck>

Somebody else had this posted as an answer but deleted for some reason so I am re-posting it so anyone that comes along in future can see how I solved it.



来源:https://stackoverflow.com/questions/51228443/always-run-post-build-event-commands-in-visual-studio-2017

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!