CorporateActionMan
CorporateActionMan

Reputation: 59

Project SDK (.Net Standard): Official way to create a nuget package that includes msbuild targets

Visual Studio 2019, .Net Standard 2.0

How do I include a custom msbuild targets file for the consuming project? What is the official supported way of doing this?

I've already tried:

Nuspec:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>TestingNugetContent</id>
    <version>1.0.10</version>
    <title>Blah</title>
    <authors>Me</authors>
    <owners>Me</owners>
    <licenseUrl>http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE</licenseUrl>
    <projectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</projectUrl>
    <iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Blah</description>
    <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
    <copyright>Copyright 2019</copyright>
    <tags>Tag1 Tag2</tags>
  </metadata>
  <files>
    <file src="Immutable\*.*" target="content/Immutable/" />
    <file src="Build\*.*" target="build/netstandard2.0/" />
  </files>
</package>
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>netstandard2.0</TargetFrameworks>
  </PropertyGroup>

  <PropertyGroup>
    <NoPackageAnalysis>true</NoPackageAnalysis>
    <NuspecFile>TestingNugetContent.nuspec</NuspecFile>
    <IntermediatePackDir>$(MSBuildProjectDirectory)/bin/$(Configuration)/publish/</IntermediatePackDir>
    <PublishDir>$(IntermediatePackDir)$(TargetFramework)/</PublishDir>
    <NuspecProperties>publishDir=$([MSBuild]::NormalizeDirectory($(IntermediatePackDir)))</NuspecProperties>
    <Version>1.0.10</Version>
  </PropertyGroup>

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

  <ItemGroup>
    <None Include="build\netstandard2.0\TestingNugetContent.targets" />
  </ItemGroup>

  <Target Name="PublishAll" BeforeTargets="GenerateNuspec">
    <ItemGroup>
      <_TargetFramework Include="$(TargetFrameworks)" />
    </ItemGroup>
    <MSBuild Projects="$(MSBuildProjectFullPath)" Targets="Publish" Properties="TargetFramework=%(_TargetFramework.Identity)" />
  </Target>


</Project>

Checking the consumer's <***>.csproj.nuget.g.targets file the import project tag for this custom target is missing after installing the Nuget package

Upvotes: 0

Views: 1651

Answers (1)

zivkan
zivkan

Reputation: 15071

As per the docs, the props and targets file names must match the package id exactly. Your nuspec lists the <id> as TestingNugetContent, so the files must be TestingNugetContent.props and TestingNugetContent.targets. They should be either directly in the build/ folder in the package, or the build/<tfm>/ folder (I prefer to be more explicit, so I appriciate you used the netstandard2.0 TFM). Now, your csproj appears to specify a build\netstandard2.0\TestingNugetContent.targets, which looks correct, so I can only guess that it wasn't packed into the correct location somehow.

I don't currently have time to show an example on how to pack it, but you can inspect the contents of your nupkg using NuGet package explorer, or just opening it up as a zip file, see what's "wrong", then adjust your project and try again.

FYI, you shouldn't need to use a nuspec at all, you can use the MSBuild PackagePath metadata on items to specify where MSBuild items are packed. It's unclear to me what the purpose of your PublishAll target is supposed to be. If you added it as part of trying to get your targets file included, you can remove it.

Upvotes: 1

Related Questions