使用tfs中的多个targetframeworks构建项目作为nuget包

时间:2018-01-02 17:47:08

标签: tfs msbuild nuget csproj

我想在我的一个tfs构建步骤中构建我的nuget包。

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

当我只使用一个TargetFramework时它工作正常。我的程序集也得到了一个版本和我的Nuget-Package。 即使我在.Net-Standard2.0中使用AssemblyInfo.cs - 在this回答中解释。

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

所以最后这个工作正在生成一个版本化的nuget文件。

    <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文件是无版本的。为什么会这样,我该如何解决?

<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 个答案:

答案 0 :(得分:2)

可以调整目标以支持多目标定位:

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