Msbuild: NuGet package with build .targets and .props not working with TargetFrameworks

Created on 14 Mar 2017  路  5Comments  路  Source: dotnet/msbuild

I have nuget package which contains .props and .targets files in build directory.

When i install this package to another project .targets and .props are called as they should if i specify only one <targetFramework>, but if i want my project to be build for multiple frameworks and i use let say <TargetFrameworks>netcoreapp1.0;net46</TargetFrameworks> .targets and .props from my package are never used.

Here is the reason why (generated project.csproj.nuget.g.targets generated by Restore):

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
  </PropertyGroup>
  <ImportGroup Condition=" '$(TargetFramework)' == 'net46' AND '$(ExcludeRestorePackageImports)' != 'true' ">
    <Import Project="$(NuGetPackageRoot)asseco.nuget.deployment\2.0.0\build\Asseco.NuGet.Deployment.targets" Condition="Exists('$(NuGetPackageRoot)asseco.nuget.deployment\2.0.0\build\Asseco.NuGet.Deployment.targets')" />
  </ImportGroup>
  <ImportGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.0' AND '$(ExcludeRestorePackageImports)' != 'true' ">
    <Import Project="$(NuGetPackageRoot)asseco.nuget.deployment\2.0.0\build\Asseco.NuGet.Deployment.targets" Condition="Exists('$(NuGetPackageRoot)asseco.nuget.deployment\2.0.0\build\Asseco.NuGet.Deployment.targets')" />
  </ImportGroup>
</Project>

My project file is simple as this:

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

  <PropertyGroup>
    <TargetFrameworks>netcoreapp1.0;net46</TargetFrameworks>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Asseco.NuGet.Deployment" Version="2.0.0" />
  </ItemGroup>

</Project>

If you look at Condition="'$(TargetFramework)' == 'netcoreapp1.0'" this condition is source of problem. Since i have set TargetFrameworks and not TargetFramework this condition is alwasy false because $(TargetFramework) is always empty and import of .targets or .props never happen.

Generated nupkg is ok, contains all frameworks, but .targets and .props from other packages can not be used with TargetFrameworks.

Thank you for help

Most helpful comment

The conditions will evaluate to true on the inner builds of the referenced projects. This may or may not be what you want. If you want to build props/targets that are imported for the outer build in multi-targeting projects, you can put the files into a buildCrossTargeting folder inside the nuget (instead of or complementing build).

All 5 comments

The conditions will evaluate to true on the inner builds of the referenced projects. This may or may not be what you want. If you want to build props/targets that are imported for the outer build in multi-targeting projects, you can put the files into a buildCrossTargeting folder inside the nuget (instead of or complementing build).

Hi, thank you for answer.

What i want is to have multitarget project (classlibrary) and i want to create nuget package.

I have my custom target AfterTargets="Pack". So when package is created it will contain all target frameworks libraries (.dll) and my target is called with this nupkg.

I want to use dotnet pack or msbuild /t:Pack.

What do you mean by buildCrossTargeting folder? Rename folder build to buildCrossTargeting?

Thank you

Thank you i have tried it.

It works like charm, but i have to have both directories buildCrossTargeting and build since i want it working for multitarget projects and also single target projects.

But thanks anyway for help, you saved my day :).

I just hit this in my nuget package, I was getting crazy! Any plan to fix this since no one knows about that buildCrossTargeting folder?

I also hit this. Trying to setup metrics gathering for CI.
Everything seemed to build fine however wasn't getting the metrics target for most of the projects.
As i was only targeting one framework was no hard fix to change, however now limits multitargeting.

The 'Metrics' Target was basically nonexistent for TargetFrameworks projects.

Hope this helps someone else

Was this page helpful?
0 / 5 - 0 ratings