本文介绍了使用nuget包在tfs中使用多个targetframeworks构建项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的tfs构建步骤之一中构建nuget软件包.

I want to build my nuget package in one of my tfs build steps.

因为TFS-NuGet-Step当前不支持.Net-Standard2.0,所以我在msbuild中使用/t:pack作为构建参数.

Because .Net-Standard2.0 is currently not supported by the TFS-NuGet-Step I am using /t:pack as build argument in my msbuild.

当我只使用一个TargetFramework时,它工作正常.我的装配体也获得了版本和我的Nuget包.即使我在.Net-Standard2.0中使用AssemblyInfo.cs,也在答案.

When I only use one TargetFramework it is working fine. My Assemblies get a Version and my Nuget-Package too.Even if I am using an AssemblyInfo.cs in .Net-Standard2.0 - explained in this answer.

到目前为止没有问题,但是如果我现在想在像<TargetFramework>netstandard2.0;net45</TargetFramework>这样的两个不同的TargetFrameworks上构建,我的nuget-package将不会获得版本.

So far no problem, but If I want to build now on two different TargetFrameworks like <TargetFramework>netstandard2.0;net45</TargetFramework> my nuget-package won't get a Version.

所以最终这是可行的,并且正在生成版本化的nuget文件.

So in the end this is working and is generating a versioned nuget-file.

    <Project Sdk="Microsoft.NET.Sdk">

      <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
        <GeneratePackageOnBuild>false</GeneratePackageOnBuild>
        <GenerateNuspecDependsOn>$(GenerateNuspecDependsOn);ReadPackageVersionFromOutputAssembly</GenerateNuspecDependsOn>
        <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
      </PropertyGroup>

      <Target Name="ReadPackageVersionFromOutputAssembly" DependsOnTargets="Build">
        <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
          <Output TaskParameter="Assemblies" ItemName="PackAssembly" />
        </GetAssemblyIdentity>
        <PropertyGroup>
          <PackageVersion>%(PackAssembly.Version)</PackageVersion>
        </PropertyGroup>
      </Target>

      <ItemGroup>
        <Compile Remove="AccessControl\**" />
        <EmbeddedResource Remove="AccessControl\**" />
        <None Remove="AccessControl\**" />
      </ItemGroup>

      <ItemGroup>
        <Compile Include="..\common\Version.cs" Link="Properties\Version.cs" />
      </ItemGroup>

      <ItemGroup>
        <PackageReference Include="System.Threading.Tasks.Dataflow" Version="4.8.0" />
      </ItemGroup>

      <ItemGroup>
        <Reference Include="MyEngine">
          <HintPath>..\libs\NetStandard\MyEngine.dll</HintPath>
        </Reference>
      </ItemGroup>

      <ItemGroup>
        <Folder Include="Properties\" />
      </ItemGroup>

    </Project>

这是我当前的csproj文件.这也将生成并生成一个nuget文件.但是在这种情况下,我的nuget文件是未版本化的.为什么会这样,我该如何解决?

So here is my current csproj-file. This builds too and also generates a nuget-file. But in this case my nuget-file is unversioned. Why is it this way and how can I fix it?

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
    <GeneratePackageOnBuild>false</GeneratePackageOnBuild>
    <GenerateNuspecDependsOn>$(GenerateNuspecDependsOn);ReadPackageVersionFromOutputAssembly</GenerateNuspecDependsOn>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  </PropertyGroup>

  <Target Name="ReadPackageVersionFromOutputAssembly" DependsOnTargets="Build">
    <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
      <Output TaskParameter="Assemblies" ItemName="PackAssembly" />
    </GetAssemblyIdentity>
    <PropertyGroup>
      <PackageVersion>%(PackAssembly.Version)</PackageVersion>
    </PropertyGroup>
  </Target>

  <ItemGroup>
    <Compile Remove="AccessControl\**" />
    <EmbeddedResource Remove="AccessControl\**" />
    <None Remove="AccessControl\**" />
  </ItemGroup>

  <ItemGroup>
    <Compile Include="..\common\Version.cs" Link="Properties\Version.cs" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="System.Threading.Tasks.Dataflow" Version="4.8.0" />
  </ItemGroup>

  <ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
    <Reference Include="MyEngine">
      <HintPath>..\libs\NetStandard\MyEngine.dll</HintPath>
    </Reference>
  </ItemGroup>

  <ItemGroup Condition="'$(TargetFramework)' == 'net45'">
    <Reference Include="MyEngine">
      <HintPath>..\libs\NET45\MyEngine.dll</HintPath>
    </Reference>
  </ItemGroup>

  <ItemGroup>
    <Folder Include="Properties\" />
  </ItemGroup>

</Project>

推荐答案

可以调整目标以支持多目标,如下所示:

The targets can be adapted to support multi-targeting like this:

<Target Name="ReadPackageVersionFromOutputAssemblySingleTfm" Returns="@(PackAssembly)" Condition="'$(IsCrossTargetingBuild)' != 'true'">
  <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
    <Output TaskParameter="Assemblies" ItemName="PackAssembly" />
  </GetAssemblyIdentity>
  <PropertyGroup>
    <PackageVersion>%(PackAssembly.Version)</PackageVersion>
  </PropertyGroup>
</Target>

<Target Name="ReadPackageVersionFromOutputAssemblyMultipleTfms" Condition="'$(IsCrossTargetingBuild)' == 'true'">
  <PropertyGroup>
    <FirstTargetFramework>$([System.String]::Copy($(TargetFrameworks)).Split(';').GetValue(0))</FirstTargetFramework>
  </PropertyGroup>
  <MSBuild Projects="$(MSBuildProjectFullPath)" Targets="ReadPackageVersionFromOutputAssemblySingleTfm" Properties="TargetFramework=$(FirstTargetFramework)">
    <Output TaskParameter="TargetOutputs" ItemName="PackAssembly" />
  </MSBuild>
  <PropertyGroup>
    <PackageVersion>%(PackAssembly.Version)</PackageVersion>
  </PropertyGroup>
</Target>

<Target Name="ReadPackageVersionFromOutputAssembly" DependsOnTargets="Build;ReadPackageVersionFromOutputAssemblySingleTfm;ReadPackageVersionFromOutputAssemblyMultipleTfms" />

这篇关于使用nuget包在tfs中使用多个targetframeworks构建项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 23:06