jump to content
Snippets and tips
  1. Home
  2. Holiday
  3. Posts
  4. Tools
  5. Tags
  6. Categories

Msbuild stuff

useful additions to build files

The project files for MsBuild can be extended to do useful things during the build process.

I collected a few ideas to make my projects more useful.

Getting the build time into code #

One can add a build target that gets executed before compiling. This one adds a new file which gets recreated each time. Then, a BuildVersion.Version is available in CSharp.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<Target Name="BuildVersion" BeforeTargets="BeforeCompile">
    <Message Text="Configuration is $(Configuration)" />
    <PropertyGroup>
      <BuildCs>
        namespace $(RootNamespace){
        public class BuildVersion {
        public static string Version="$([System.DateTime]::Now.ToString(yyyy.MM.dd.HHmm))" %3B
        } }
      </BuildCs>
    </PropertyGroup>
    <WriteLinesToFile File="$(ProjectDir)BuildVersion.cs" Overwrite="true" Lines="$(BuildCs)" />
</Target>

Copying after creating a publish version #

One can add a build target after the publishing, f.e. to copy the binary files to a specific location.

1
2
3
4
  <Target Name="CopyToBin" AfterTargets="Publish">
    <Message Text="Inside AfterPublish" Importance="high" />
    <Exec Command="C:\bin\bmc.exe bin\publish -t C:\bin" ConsoleToMsBuild="true" />
  </Target>