MSBuild Post-Build

I have an MSBuild script that just does everything I need to do without my stage after the build (see the previous question I asked: MSBuild conditional Exec? ).

What I want to do is build a lot of csproj files and possibly follow the post-build steps if and only if the project was built. I don’t want to constantly perform my post-construction step, otherwise the timestamp on my final output will be changed unnecessarily (and this makes the assembly process very time-consuming for no reason).

In my MSBuild script, I have something like the following for each of my csproj files:

<Target Name="ProjectName"> <MSBuild Projects="PathToProject" Properties="Configuration=$(buildtype)" /> </Target> 

Edit: I think I really want to determine when a CoreCompile task is executed for each project. If any way to verify this is able to?

Any ideas?

I am new to MSBuild, so maybe I'm completely wrong!

Thanks Alan

+4
c # msbuild csproj
source share
3 answers

After a long search for a simple solution to this problem, I did not find it, and in the end I came up with my own solution that works, but may not be the best solution. However, I would like to share it with anyone else with the same problem so that you can at least have a working solution and hopefully save a lot of noise.

Recall what I wanted to do was run the command line tool after my project, but only if the assembly was updated (i.e. the timestamp has changed). I did not want to put this in the section after the assembly of each project, because I only wanted the post-assembly to happen on our build server (not in the development machines).

I did not find any way to do this externally in my main .proj file and ended up changing the section after building each .csproj file. However, I prefix it with an if condition something like this:

 if '$(ExecuteCommand)' == 'true' command.exe 

This means that the command will never be executed on the development machine, but when I call the assembly from my .proj file, I can set this flag to true as follows:

 <!-- Define common properties --> <PropertyGroup> <ExecuteCommand>true</ExecuteCommand> </PropertyGroup> <Target Name="YourTarget"> <!-- Build project --> <MSBuild Projects="Path to project" Properties="ExecuteCommand=$(ExecuteCommand)" /> </Target> 

As I said, I do not think that this is the most elegant solution, but it certainly works and will be sufficient for me at the moment. However, I will still be interested to know what the right way to achieve this is so that I can improve my script.

Thanks Alan

+1
source share

You can also do this based on the configuration selected during the build process. For CI, you should always use "Release" or "Production" (you can define your own)

 <Exec Condition="'$(ConfigurationName)'=='Release'" Command="your command goes here ..."/> 
+2
source share

If you can add the following to each of your projects:

 <Target Name="DoStuffWithNewlyCompiledAssembly"> <Exec Command="command.exe" /> </Target> 

... then you only need to add the property:

 <Target Name="Name"> <MSBuild Projects="" Properties="TargetsTriggeredByCompilation=DoStuffWithNewlyCompiledAssembly" /> </Target> 

This works because someone smart at Microsoft added the following line at the end of CoreCompile target Microsoft.[CSharp|VisualBasic][.Core].targets (the file name depends on the language and version of MSBuild / Visual Studio).

 <CallTarget Targets="$(TargetsTriggeredByCompilation)" Condition="'$(TargetsTriggeredByCompilation)' != ''"/> 

So, if you specify the target name in the TargetsTriggeredByCompilation property, your target will be launched if CoreCompile running - and your target will not be launched if CoreCompile is skipped (for example, at the moment regarding the code).

0
source share

All Articles