Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include XML Documentation from Nuget Package in csproj build output

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?

like image 203
Tim Avatar asked Aug 08 '19 12:08

Tim


People also ask

How do I create an XML document in Visual Studio?

From the menu bar, choose Tools > Options to open the Options dialog box. Then, navigate to Text Editor > C# (or Visual Basic) > Advanced. In the Editor Help section, look for the Generate XML documentation comments option.

What is package reference in Csproj?

Package references, using <PackageReference> MSBuild items, specify NuGet package dependencies directly within project files, as opposed to having a separate packages. config file. Use of PackageReference doesn't affect other aspects of NuGet; for example, settings in NuGet.

Where are NuGet packages references stored?

The location of the default global packages folder. The default is %userprofile%\. nuget\packages (Windows) or ~/. nuget/packages (Mac/Linux).


2 Answers

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>
like image 196
user1106925 Avatar answered Oct 21 '22 15:10

user1106925


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.

like image 2
yayayahei Avatar answered Oct 21 '22 14:10

yayayahei