1

Is there a property or another way in MSBuild that would hold the start time of a build?

I have explored my builds with the MSBuild structured viewer but found nothing.

honzajscz
  • 2,850
  • 1
  • 27
  • 29

1 Answers1

2

First of all, project files (csproj) are actually MSBuild files, so we can use MSBuild variables.

The problem is when we compile the entire solution (sln), we don't have an MSBuild file to declare variables and change the default behavior of MSBuild Build target.

So in the first step, we will need to create an MSBuild file for the sln:

  • You can change this environment variable to generate the MSBuild file from the build process:

    Set MSBuildEmitSolution=1
    msbuild {yourSlnFile}.sln
    

    The root folder now should contain a file with .sln.metaproj, that you can compile the solution with him (msbuild {yourSlnFile}.sln.metaproj /t:Build)

    Link

  • You can create your own MSBuild file for compile the solution with him, and he will be cleaner from the first option:

    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
        <Target Name="Build">
            <Message Text="$([System.DateTime]::Now.ToString(`yyyy.MMdd`))"></Message>
            <MSBuild Projects="{yourSlnFile}.sln" Properties="Configuration=$(Configuration); Platform=$(Platform)" />
        </Target> 
    </Project>  
    

    Link

Now, in your custom MSBuild file to build the sln, you can declare a tag in PropertyGroup, and this tag will hold the DateTime in your file:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
    <PropertyGroup>
        <StartCommandTime>$([System.DateTime]::Now.ToString(`yyyy.MMdd`))</StartCommandTime>
    </PropertyGroup>
    <Target Name="Build">
        <Message Text="$(StartCommandTime)"></Message>
        <MSBuild Projects="{yourSlnFile}.sln" Properties="Configuration=$(Configuration); Platform=$(Platform)" />
        <Message Text="$(StartCommandTime)"></Message>
    </Target> 
</Project> 
baruchiro
  • 5,088
  • 5
  • 44
  • 66
  • What you suggest is the current time. I want the time of the build start - precisely the moment when the *'msbuild -t:rebuild mySol.sln'* is executed or Visual Studio starts the build. This should remain constant until the end of the build. – honzajscz Jan 20 '19 at 21:30
  • Thank you for answer and the suggested solution. I am, however, aware of that. I was more hoping for some implicit time stamp property. Something like your *StartCommandTime*. Since I need it to work in Visual Studio as well. – honzajscz Jan 21 '19 at 13:10
  • Please, please tell us what your end goal – baruchiro Jan 21 '19 at 16:23
  • Sure: I need to perform a certain target only once per a solution build. The very first project of the solution should execute this target and other projects should not. This all achieved preferably using *Directory.Build.targets* and not touching any project. My idea was to let the very first project to create a well-known file that would contain a time stamp of the build start. If other project were about to execute this target they would first check if the file is there and if it contains right time stamp. I actually do not need a time stamp. I just need some a sln build identifier. – honzajscz Jan 21 '19 at 18:55
  • 1
    I understand you don't know who the first project is. But if you work within a visual studio, you should know. And if you run a `msbuild` from outside, the solution is much simpler. – baruchiro Jan 21 '19 at 19:51
  • 1
    I think that for "hold the start time of a build", this can be an accepted result. For your real question, I think you should ask another question, explain as much as possible, and I will answer you. – baruchiro Jan 21 '19 at 19:59