How to always fulfill a goal in MSBuild

I have an MSBuild file that processes the AssemblyInfo file before compiling the application. At the end of the assembly, it restores the AssemblyInfo file. He does this by backing up the file, manipulating it, and then restoring the file after the build time.

This works pretty well, unless an error occurs during build. Then it does not restore the original file. Is there a way to tell MSBuild that the goal was completed at the end of the build, whether it succeeded or failed?

+4
source share
3 answers

How about changing the problem:

  • Add the AssemblyInfo.cs.template "template" to the version control that represents your "perfect" AssemblyInfo.cs with regex hooks there
  • Before assembling, copy the template into a real one and apply your regular expressions.
  • Add some kind of subversion oversight for AssemblyInfo.cs (I'm not an svn expert, but I'm sure you can tell it to ignore specific files)
  • In case your developers need to add some kind of customization that usually appears in AssemblyInfo.cs (for example, InternalsVisibleTo), then ask them to add it to another .cs file in which the checkbox is checked.

As an additional refinement, combine the Sayed solution with mine and remove the version information from the actual AssemblyInfo.cs and check the VersionInfo.cs.template checkbox that creates VersionInfo.cs in BeforeBuild.

+2
source

Based on your last comment on the original question, I would take a different approach and forget the approach you are currently using. You should be aware that your version information does not have to be in the AssemblyInfo.cs file. It can be in any code file if you have only the AssemblyVersion and AssemblyFileVersion attributes defined after each. With that said, what would I do, follow these steps:

  • Remove AssemblyVersion and AssemblyFileVersion from AssemblyInfo.cs
  • Create a new file, name it whatever you want, in my case I put it in Properties \ VersionInfo.cs. Do not add this file to the project.
  • Edit the project file to include this file in the list of file that should be compiled only if you want it

Set the bit to # 3. When you create a .NET project, the project itself is an MSBuild file. Inside this file you will find the element declared by compilation. This is a list of files that will be sent to the compiler for compilation. You can dynamically include / exclude files from this list. In this case, you want to include the VersionInfo.cs file only if you are building a build server (or some other condition that you define). In this example, I determined what condition should be if the project was built in Release mode. Thus, for Release mode, VersionInfo.cs will be sent to the compiler, but for other assemblies it will not. Here is the contents of VersionInfo.cs

VersionInfo.cs

[assembly: System.Reflection.AssemblyVersion("1.2.3.4")] [assembly: System.Reflection.AssemblyFileVersion("1.2.3.4")] 

To associate this with the build process, you must edit the project file. In this file you will find an element (possibly more than 1 depending on the type of project). You must add a goal similar to the following.

 <Target Name="BeforeCompile"> <ItemGroup Condition=" '$(Configuration)'=='Release' "> <Compile Include="Properties\VersionInfo.cs" /> </ItemGroup> </Target> 

Here is what I did here to define the goal, BeforeCompile, which is a well-known goal that you can override. See Article

Since we know that TeamFoundationServerUrl is only defined when created through TFS.

If you create a command line form, then something like this

 <Target Name="BeforeCompile"> <ItemGroup Condition=" '$(IncludeVersionInfo)'=='true' "> <Compile Include="Properties\VersionInfo.cs" /> </ItemGroup> </Target> 

And when you build the project, just do msbuild.exe YourProject.proj / p: IncludeVersion = true. Note: when building a solution, this will not work.

+5
source

I have never used it, but from the documentation it seems that the OnError element is useful for what you are trying to achieve.

Causes one or more goals to execute if the ContinueOnError attribute is false for the failed task.

0
source

All Articles