Build Project with multiple targetframeworks in tfs as nuget package

ぃ、小莉子 提交于 2019-12-11 03:03:32

问题


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

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

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.

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.

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>

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>

回答1:


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" />


来源:https://stackoverflow.com/questions/48065516/build-project-with-multiple-targetframeworks-in-tfs-as-nuget-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!