9

I have a nuget package that includes an XML documentation file.

packages/MyPackage.1.0.0/lib/net472/MyPackage.xml

However when I build my project, I want to include this xml file in the output.

So when I:

dotnet MyProj.csproj -c Release

I want to get:

> ls bin/Release/net472
MyProj.dll
MyPackage.dll
MyPackage.xml

However it never comes along. How can I get it?

geekley
  • 1,231
  • 10
  • 27
Tim
  • 2,968
  • 5
  • 29
  • 55
  • Why do you want XML documentation from dependent package to be in the build output of your project? XML documentation is located in the package, there is no need to create more copies of it. – Vlad DX Aug 21 '19 at 16:50
  • 2
    The project used to reference another project that's since become a nuget package. The XML doc is used for auto generated API pages. – Tim Sep 01 '19 at 13:10

2 Answers2

9

Posting Vlad's answer here.

If you want to copy xml from all packages, this is a better method than Snede's, since it works generically without requiring to specify .net version on path for each package.

<Project>
  <!-- ... -->
  <ItemGroup>
    <PackageReference Include="Example.SomePkg" Version="1.0.0" />
  </ItemGroup>
  <!-- ... -->
  <Import Project="$(MSBuildToolsPath)/Microsoft.CSharp.targets" />

  <!-- Add this -->
  <Target Name="_ResolveCopyLocalNuGetPkgXmls" AfterTargets="ResolveReferences">
    <ItemGroup><!-- Copy XML files from all PackageReferences to output dir -->
      <ReferenceCopyLocalPaths Include="@(ReferenceCopyLocalPaths->'%(RootDir)%(Directory)%(Filename).xml')"
      Condition="'%(ReferenceCopyLocalPaths.NuGetPackageId)'!='' and Exists('%(RootDir)%(Directory)%(Filename).xml')" />
    </ItemGroup>
  </Target>

</Project>
geekley
  • 1,231
  • 10
  • 27
  • 1
    This seems to work! Can you try to explain a bit how it works? :-) – osexpert Feb 17 '21 at 22:05
  • @osexpert I don't know to be honest lol. I'm total noob in msbuild. I just copied Vlad's answer here because I tested it and it seems to work well. Maybe he can add explaination if you @ him in the other answer? – geekley Feb 18 '21 at 02:21
  • Basically, it is copying all the xml files from the installed nuget packages to the build directory. When you install nuget package it will be stored in "C:\Users\your_login_name\.nuget\packages" – AlbertK May 04 '21 at 07:32
2

I find one solution: Please find it at https://snede.net/add-nuget-package-xml-documentation-to-swagger/?unapproved=168&moderation-hash=14dbe7e7ca3d8affb6ace2bfdb7ff581

This is actually a problem to copy files from nuget package to out dir or publish dir.

Just modify csproj file to copy file from package's path after run build or publish.

yayayahei
  • 401
  • 4
  • 9