.Net Core msbuild build.proj 相当于 AssemblyInfo

.Net Core msbuild build.proj equivilent of AssemblyInfo

我正在将项目从 AspNet Mvc 4 升级到 AspNet Core Mvc 2.2;我正在尝试迁移 msbuild build.proj 文件来为创建 dll 的项目设置版本和其他属性;除 GenerateAssemblyInfo 任务外,一切正常。 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>

在 Visual Studio 2019 年使用 .Net Core 时,有一些事情阻止了它的工作;

  1. 目标的名称不能是"GenerateAssemblyInfo",如果这是目标的名称,它就会被忽略,没有错误、警告或消息,目标就不会运行...
  2. 在导入msbuild项目的csproj文件中,需要添加false,我是在
  3. 下面添加的
  4. 如果将 OutputFile 设置在该文件夹中,则必须手动创建 Properties 文件夹。

下面是工作 build.proj 文件

    <?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>

和 MyProject.csproj 文件

<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.0\lib\netstandard2.0\Microsoft.AspNetCore.Http.Abstractions.dll</HintPath>
    </Reference>
  </ItemGroup>

  <Import Project="../.build/build.proj" />
</Project