How to always execute a target in MSBuild?

What about changing the problem: Add a "template" AssemblyInfo.cs. Template to version control that represents your "ideal" AssemblyInfo. Cs with regex hooks in there Before build, copy the template to the real and apply your regexes Add some kind of subversion ignore for AssemblyInfo.Cs (I'm no svn expert, but I'm pretty sure there is a way you can tell it to ignore certain files) In the event that your devs need to add some kind of customization that would normally appear in an AssemblyInfo.

Cs (eg InternalsVisibleTo), then get them to add it to a different . Cs file that IS checked in.As a further refinement, combine Sayed's solution with mine and remove version info stuff from the actual AssemblyInfo. Cs and have a VersionInfo.cs.

Template that is checked in, that creates a VersionInfo.Cs in BeforeBuild.

Interesting idea... I'll need to think on that, but I think that may be closer to the kind of solution that would work. If it succeeds, then it compiles in the "template file". If it fails, the template file stays in there, but the file I want to protect isn't changed.

– Jason Thompson Sep 16 '10 at 13:09 I'd be interested to hear what you do come up with in the end. Good luck! – Peter McEvoy Sep 16 '10 at 13:10.

Based on your last comment to the original question I would take another approach, and forget the approach you are currently taking. You should know that your version info doesn't have to be in the AssemblyInfo. Cs file.It can be in any code file, just as long as you only have attributes AssemblyVersion and AssemblyFileVersion defined once each.

With that being said what I would do is follow these steps: Remove AssemblyVersion & AssemblyFileVersion from AssemblyInfo. Cs Create a new file, name it whatever you want want in my case I put it at Properties\VersionInfo.cs.Do not add this file to the project. Edit the project file to include that file into the list of file to be compiled only when you want it Let's expand a bit on #3.

When you build a . NET project, the project itself is an MSBuild file. Inside that file you will find an item declared Compile.

This is the list of files that will be sent to the compiler to be compiled. You can dynamically include/exclude files from that list. In you case you want to include the VersionInfo.

Cs file only if you are building on the build server (or whatever other condition you define). For this example I defined that condition to be if the project was building in Release mode. So for Release mode VersionInfo.

Cs would be sent to the compiler, and for other builds not. Here are the contents of VersionInfo. Cs VersionInfo.

Cs assembly: System.Reflection. AssemblyVersion("1.2.3.4") assembly: System.Reflection. AssemblyFileVersion("1.2.3.4") In order to hook this into the build process you have to edit the project file.

In that file you will find an element (maybe more than 1 depending on project type). You should add a target similar to the following there. Here what I've done here is to define a target, BeforeCompile, which is a well-known target that you can override.

See this MSDN article about other similar targets. Basically this is a target which will always be called before the compiler is invoked. In this target I add the VersionInfo.

Cs to the Compile item only if the Configuration property is set to release. You could define that property to be whatever you wanted. For instance if you have TFS as your build server then it could be, Because we know that TeamFoundationServerUrl is only defined when building through TFS.

If you are building form the command line then something like this And when you build the project just do msbuild. Exe YourProject. Proj /p:IncludeVersion=true.

Note: this will not work when building a solution.

Interesting, but I'm not sure how this solves the issue. Let me expand on what I'm doing. When I wish to send a release to test, I create a branch in subversion that specifies the version number.My custom MSBuild task then reads in what branch the working copy is in (as well as other information such as revision number, etc...).

It then modifies assemblyinfo. Cs to embed such information. On failure, it restores the original version of the file.

The reason for this is so that my regex's "know" where to modify the information. – Jason Thompson Sep 15 '10 at 13:49 More importantly, I don't create an endless loop of uncommitted changes this way. If I commit version 1.0.0.0, I don't have a changed file due to simply building the project.

Instead, the file is only changed when I need it to be (during build). I don't want my developers to have to remember to change this information by hand. The idea is that I can look at any file and know exactly where it came from and how it was built.

So if someone releases junk on the prod server, I know who to blame and exactly which version of the code I need to modify. – Jason Thompson Sep 15 '10 at 13:52 +1 I like the solution. Too bad cruisecontrol doesn't auto-define a property like this.

– Maslow Sep 16 '10 at 15:05.

I never used it, but from the documentation it seems that the OnError Element is useful to what you're trying to achieve. Causes one or more targets to execute, if the ContinueOnError attribute is false for a failed task.

I'm using the OnError element for my custom targets/tasks already. This works fabulously. The problem is when the error occurs outside a target that is under my control.So I'd like a super catch all that works as an OnError for any task.

– Jason Thompson Sep 13 '10 at 15:05 @Jason, in that case I'm sorry to say that I can't provide any further help. – João Angelo Sep 13 '10 at 15:17.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions