Search code examples
c#asp.net-mvcasp.net-coremsbuildassemblyinfo

.Net Core msbuild build.proj equivilent of AssemblyInfo


I'm upgrading a project from AspNet Mvc 4 to AspNet Core Mvc 2.2; I am attempting to migrate the msbuild build.proj file to set the version and other attributes for the projects that create dll's; everything is working except the GenerateAssemblyInfo task. Is there a new way to do this in netcoreapp2.2?

    <?xml version="1.0" encoding="utf-8"?>
       <Project ToolsVersion="15.0"  xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="GetRevisionInfo" BeforeTargets="Build">
    <GitPendingChanges ContinueOnError="WarnAndContinue">
      <Output TaskParameter="HasPendingChanges" PropertyName="HasPendingChanges" />
    </GitPendingChanges>
    <!--This will throw git Error 128 if there are no Tags -->
    <GitDescribe SoftErrorMode="true" Lightweight="true" ContinueOnError="WarnAndContinue">
      <Output TaskParameter="Tag" PropertyName="Tag" />
      <Output TaskParameter="CommitCount" PropertyName="CommitCount" />
      <Output TaskParameter="CommitHash" PropertyName="CommitHash" />
    </GitDescribe>
    <GitBranch ContinueOnError="WarnAndContinue">
      <Output TaskParameter="Branch" PropertyName="Branch" />
    </GitBranch>
    <PropertyGroup>
      <ShortCommitHash Condition="'$(CommitHash)' != ''">$(CommitHash.Substring(0,6))</ShortCommitHash>
      <ReleaseType Condition="'$(CommitCount)' != '' AND '$(CommitCount)' != '0'">Beta</ReleaseType>
      <ReleaseType Condition="'$(Branch)' != '' AND '$(Branch)' != 'master'">Alpha</ReleaseType>
    </PropertyGroup>
  </Target>

  <!-- Error out if this is a release and our working copy has uncommitted changes -->
  <Target Name="CheckRelease" AfterTargets="GetRevisionInfo" Condition="'$(Configuration)' == 'Release'">
    <Error Text="Cannot build a Release Version when there are uncommitted changes, commit or revert all changes." Condition="'$(CommitHash)' != '' AND '$(HasPendingChanges)' == 'True'" />
  </Target>

  <!-- Generates AssemblyInfo file using Git Describe -->
  <Target Name="GenerateAssemblyInfo" AfterTargets="CheckRelease" Condition="'$(CommitHash)' != ''">
    <Time>
      <Output TaskParameter="Year" PropertyName="Year" />
    </Time>
    <AssemblyInfo
      CodeLanguage="CS"
      OutputFile="$(MSBuildProjectDirectory)\Properties\AssemblyInfo.cs"
      AssemblyProduct="$(MSBuildProjectName) $(ReleaseType)"
      AssemblyCompany="xxx xxx xxx, LLC"
      AssemblyCopyright="Copyright © $(Year) xxx xxx xxx, LLC. All rights reserved."
      AssemblyConfiguration="$(Configuration)-$(Platform)"
      AssemblyVersion="$(Tag).$(CommitCount)"
      AssemblyFileVersion="$(Tag).$(CommitCount)"
        AssemblyInformationalVersion ="$(Tag)-$(CommitCount)-$(ShortCommitHash) $(ReleaseType)"
        AssemblyTitle="$(Tag)-$(CommitCount)-$(CommitHash)"/>
  </Target>

  <!-- copy framework files to libraries -->
  <Target Name="CopyLibraries" Condition="'$(MSBuildProjectName)' == 'BaseApplication'" AfterTargets="Build">
    <CreateItem Include="$(TargetDir)xxx.*">
      <Output TaskParameter="Include" PropertyName="CopyFiles"  />
    </CreateItem>
    <Copy SourceFiles="$(CopyFiles)" DestinationFolder="$(MSBuildProjectDirectory)\..\libraries\xxx\$(Configuration)"/>
  </Target>
  <Target Name="CopyKendoUI" AfterTargets="AfterBuild">
    <ItemGroup>
      <KendoFiles Include="
                    $(TargetDir)kendo.mvc.*;
                    $(TargetDir)\**\Kendo.Mvc.resources.*;" />
    </ItemGroup>
    <Copy SourceFiles="@(KendoFiles)" DestinationFolder="$(MSBuildProjectDirectory)\..\libraries\kendoui\$(Configuration)\%(RecursiveDir)"/>
  </Target>
</Project>

Solution

  • There were a few things preventing this from working in Visual Studio 2019 using .Net Core;

    1. The name of the target cannot be "GenerateAssemblyInfo", if this is the name of the target, it just gets ignored, no error, warning, or message, the target just doesn't run...
    2. In the csproj file for the project that the msbuild project is imported in, you need to add false, I added this right below the
    3. You have to manually create the Properties folder if you set the OutputFile to be in that folder.

    Below is the working build.proj file

        <?xml version="1.0" encoding="utf-8"?>
       <Project ToolsVersion="15.0"  xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
         <Target Name="GetRevisionInfo" BeforeTargets="Build">
           <GitPendingChanges ContinueOnError="WarnAndContinue">
             <Output TaskParameter="HasPendingChanges" PropertyName="HasPendingChanges" />
           </GitPendingChanges>
           <!--This will throw git Error 128 if there are no Tags -->
           <GitDescribe SoftErrorMode="true" Lightweight="true" ContinueOnError="WarnAndContinue">
             <Output TaskParameter="Tag" PropertyName="Tag" />
             <Output TaskParameter="CommitCount" PropertyName="CommitCount" />
             <Output TaskParameter="CommitHash" PropertyName="CommitHash" />
           </GitDescribe>
           <GitBranch ContinueOnError="WarnAndContinue">
             <Output TaskParameter="Branch" PropertyName="Branch" />
           </GitBranch>
           <PropertyGroup>
             <ShortCommitHash Condition="'$(CommitHash)' != ''">$(CommitHash.Substring(0,6))</ShortCommitHash>
             <ReleaseType Condition="'$(CommitCount)' != '' AND '$(CommitCount)' != '0'">Beta</ReleaseType>
             <ReleaseType Condition="'$(Branch)' != '' AND '$(Branch)' != 'master'">Alpha</ReleaseType>
           </PropertyGroup>
         </Target>
    
      <!-- Error out if this is a release and our working copy has uncommitted changes -->
      <Target Name="CheckRelease" AfterTargets="GetRevisionInfo" Condition="'$(Configuration)' == 'Release'">
        <Error Text="Cannot build a Release Version when there are uncommitted changes, commit or revert all changes." Condition="'$(CommitHash)' != '' AND '$(HasPendingChanges)' == 'True'" />
      </Target>
    
         <!-- Generates AssemblyInfo file using Git Describe -->
         <Target Name="GenerateAssemblyInfo" AfterTargets="CheckRelease" Condition="'$(CommitHash)' != ''">
           <Time>
             <Output TaskParameter="Year" PropertyName="Year" />
           </Time>
           <AssemblyInfo
             CodeLanguage="CS"
             OutputFile="$(MSBuildProjectDirectory)\Properties\AssemblyInfo.cs"
             AssemblyProduct="$(MSBuildProjectName) $(ReleaseType)"
             AssemblyCompany="xxx xxx xxx, LLC"
             AssemblyCopyright="Copyright © $(Year) xxx xxx xxx, LLC. All rights reserved."
             AssemblyConfiguration="$(Configuration)-$(Platform)"
             AssemblyVersion="$(Tag).$(CommitCount)"
             AssemblyFileVersion="$(Tag).$(CommitCount)"
             AssemblyInformationalVersion ="$(Tag)-$(CommitCount)-$(ShortCommitHash) $(ReleaseType)"
             AssemblyTitle="$(Tag)-$(CommitCount)-$(CommitHash)"/>
          </Target>
    
         <!-- copy framework files to libraries -->
         <Target Name="CopyLibraries" Condition="'$(MSBuildProjectName)' == 'BaseApplication'" AfterTargets="Build">
           <CreateItem Include="$(TargetDir)xxx.*">
             <Output TaskParameter="Include" PropertyName="CopyFiles"  />
           </CreateItem>
           <Copy SourceFiles="$(CopyFiles)" DestinationFolder="$(MSBuildProjectDirectory)\..\libraries\xxx\$(Configuration)"/>
         </Target>
         <Target Name="CopyKendoUI" AfterTargets="AfterBuild">
           <ItemGroup>
             <KendoFiles Include="
                           $(TargetDir)kendo.mvc.*;
                           $(TargetDir)\**\Kendo.Mvc.resources.*;" />
           </ItemGroup>
           <Copy SourceFiles="@(KendoFiles)" DestinationFolder="$(MSBuildProjectDirectory)\..\libraries\kendoui\$(Configuration)\%(RecursiveDir)"/>
         </Target>
       </Project>
    

    And the MyProject.csproj file

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>netcoreapp2.2</TargetFramework>
        <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="MSBuildTasks" Version="1.5.0.235">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
      </ItemGroup>
    
      <ItemGroup>
        <Reference Include="Microsoft.AspNetCore.Http.Abstractions">
          <HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.http.abstractions\2.2.0\lib\netstandard2.0\Microsoft.AspNetCore.Http.Abstractions.dll</HintPath>
        </Reference>
      </ItemGroup>
    
      <Import Project="../.build/build.proj" />
    </Project