How can I make TeamCity take advantage of MSBuild's incremental build support?

前端 未结 3 1229
南笙
南笙 2021-02-14 00:00

I\'m setting up TeamCity (migrating from CruiseControl.NET) and I\'m struggling to get it to perform incremental builds through MSBuild.

I\'ve got a small .proj file whi

相关标签:
3条回答
  • 2021-02-14 00:07

    A workaround for this problem is to customize the MSBuild process to set the path at which the "Target Framework Moniker Assembly Attributes" file (the proper name for the file mentioned in the question) will be created.

    The TargetFrameworkMonikerAssemblyAttributesPath property is defined in Microsoft.Common.targets determines where the file should be created. By overriding this property, the location can be changed to use a different location.

    Here's a script that can be used to achieve a suitable replacement:

    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    
    <PropertyGroup>
        <PrepareForBuildDependsOn>
            $(PrepareForBuildDependsOn);
            _SetTargetFrameworkMonikerAssemblyAttributesPath
        </PrepareForBuildDependsOn>
    </PropertyGroup>
    
    <Target 
        Name="_SetTargetFrameworkMonikerAssemblyAttributesPath"
        Condition="'$(TEAMCITY_VERSION)' != ''">
    
        <PropertyGroup>
            <TargetFrameworkMonikerAssemblyAttributesDir
                Condition="'$(TargetFrameworkMonikerAssemblyAttributesDir)' == ''">
                $([MSBuild]::GetRegistryValue("HKEY_CURRENT_USER\Environment", "TMP"))
            </TargetFrameworkMonikerAssemblyAttributesDir>
            <TargetFrameworkMonikerAssemblyAttributesDir
                Condition="'$(TargetFrameworkMonikerAssemblyAttributesDir)' == ''">
                $([MSBuild]::GetRegistryValue("HKEY_CURRENT_USER\Environment", "TEMP"))
            </TargetFrameworkMonikerAssemblyAttributesDir>
            <TargetFrameworkMonikerAssemblyAttributesDir 
                Condition="'$(TargetFrameworkMonikerAssemblyAttributesDir)' == ''">
                $(USERPROFILE)
            </TargetFrameworkMonikerAssemblyAttributesDir>
            <TargetFrameworkMonikerAssemblyAttributesDir
                Condition="'$(TargetFrameworkMonikerAssemblyAttributesDir)' == ''">
                $([System.IO.Path]::Combine('$(WINDIR)', 'Temp'))
            </TargetFrameworkMonikerAssemblyAttributesDir>
            <TargetFrameworkMonikerAssemblyAttributesPath>
                $([System.IO.Path]::Combine('$(TargetFrameworkMonikerAssemblyAttributesDir)','$(TargetFrameworkMoniker).AssemblyAttributes$(DefaultLanguageSourceExtension)'))
            </TargetFrameworkMonikerAssemblyAttributesPath>
        </PropertyGroup>
    
        <Message Text="Target Framework Moniker Assembly Attributes path is &quot;$(TargetFrameworkMonikerAssemblyAttributesPath)&quot;" Importance="low" />
    
    </Target>
    

    The target is only executed when TEAMCITY_VERSION is specified as a property, which should be when the build is being executed by the TeamCity agent.

    NOTE: The child elements of the PropertyGroup should each be on a single line. They have been spread over multiple lines to increase readability here, but the additional line-breaks cause the script to fail.

    When the target runs, it tries to build a suitable path based on the user's environment variables as defined in the registry, first looking for TMP and TEMP, before falling back to the user's profile folder and finally the C:\Windows\Temp directory. This matches the order documented by System.Path.GetTempPath(), and should result in behaviour matching MSBuild execution outside of TeamCity.

    This should be saved as a .targets file somewhere on the system and imported to the .csproj file of projects being built by the TeamCity server, using an <Import> element. I added the script under my MSBuild extensions directory (C:\Program Files\MSBuild\) and referenced it by adding the following import element:

    <Import Project="$(MSBuildExtensionsPath)\TeamCity\TeamCity.Incremental.targets" />
    

    The location/ordering of Import elements doesn't matter, but I suggest including it after the <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> which should appear in every .csproj file.

    0 讨论(0)
  • 2021-02-14 00:19

    This issue is still present in TeamCity 2017.3.

    I wanted to find an easier to track work-around for this than the one detailed by the accepted answer so I did the following:

    1. I checked in a copy of my .NETFramework,Version=v4.7.AssemblyAttributes.cs file to my VCS
    2. I added a new build step to the build configurations that are using MSBuild with the following properties:
      • Runner Type: Command Line
      • Step Name: CopyAssemblyAttributesFile
      • Run: Custom script
      • Custom Script: copy "%system.teamcity.build.workingDir%\<path_to_AssemblyAttributes.cs Dir>\." "%env.TEMP%\."

    This will copy my version of the AssemblyAttributes file with the timestamp from the initial VCS checkout the first time,

    Subsequently MSBuild seems to think it is the same file since the timestamp will remain consistent and will now properly perform incremental builds, which may be verified from the build log on the agent.

    0 讨论(0)
  • 2021-02-14 00:26

    Adjusting TargetFrameworkMonikerAssemblyAttributesPath works around this issue, as Paul Turner mentions. Rather than battle the High Magick in Microsoft's build system scripts, I added an environment variable to set TargetFrameworkMonikerAssemblyAttributesPath in TeamCity project parameters.

    In TeamCity's project settings, I set env.TargetFrameworkMonikerAssemblyAttributesDir to %env.windir%\Temp.

    0 讨论(0)
提交回复
热议问题