【问题标题】:.Net Core msbuild build.proj equivilent of AssemblyInfo.Net Core msbuild build.proj 相当于 AssemblyInfo
【发布时间】:2019-10-17 19:28:07
【问题描述】:

我正在将一个项目从 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>

【问题讨论】:

    标签: c# asp.net-mvc asp.net-core msbuild assemblyinfo


    【解决方案1】:

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

    1. 目标的名称不能是“GenerateAssemblyInfo”,如果这是目标的名称,它会被忽略,没有错误、警告或消息,目标只是不运行...
    2. 在导入msbuild项目的项目的csproj文件中,需要添加false,我在下面添加了这个
    3. 如果将 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.2.0\lib\netstandard2.0\Microsoft.AspNetCore.Http.Abstractions.dll</HintPath>
        </Reference>
      </ItemGroup>
    
      <Import Project="../.build/build.proj" />
    </Project
    

    【讨论】:

      猜你喜欢
      • 2017-06-27
      • 2017-02-20
      • 1970-01-01
      • 2021-10-15
      • 1970-01-01
      • 1970-01-01
      • 2017-01-02
      相关资源
      最近更新 更多