2

I have a NuGet package with an xml documentation file (doc.xml).

I have installed the NuGet package in my project.

I know want to add the NuGet documentation file doc.xml to my solution.

I am running .net core 3.1 but I have no idea how this can be achieved.

Thank you!

1 Answers1

3

If your nuget project is net standard or net core, please check these steps:

1) create a file called <package_id>.props file under a folder called build on your project.

enter image description here

Note that, you should make sure that the your package_id of your nuget project is the same as the .props file, otherwise, it will not work. See this link's description.

In my side, my nuget package called test.1.0.0.nupkg, so I should rename the file as test.props file.

2) please add these content in test.props file.

<Project>

    <Target Name="CopyFilesToProject" BeforeTargets="Build">
        <ItemGroup>
            <SourceScripts Include="$(MSBuildThisFileDirectory)..\File\*.*"/> 
        </ItemGroup>
        <Copy
           SourceFiles="@(SourceScripts)"
           DestinationFolder="$(ProjectDir)"     
        />
    </Target>


</Project>

The propose of this target is to copy the xml file from the File folder of the nupkg into the target project's folder when you install this nuget into the main project.

3) add these under xxx.csproj file:

<ItemGroup>
    <None Include="bin\Debug\netcoreapp3.1\test.xml(the path of the xml file in your nuget project)" Pack="true" PackagePath="File"></None>
    <None Include="build\test.props(the path of the test.props file in your nuget project)" Pack="true" PackagePath="build"></None>
        
</ItemGroup>

4) Then, when you pack your project, the structure should be this:

enter image description here

enter image description here

enter image description here

Before you install this new version of nuget package, you should clean nuget caches first or just delete all cache files under C:\Users\xxx(current user)\.nuget\packages to remove the old ones in case you still install the old one.

After that, rebuild your main project to execute the target and you will see the xml document file exist under the main project folder.

enter image description here

In addition, there is a similar issue you can refer to and if you use the net framework project, the link also provide the method.

===========================

Update 1

If you want to copy the file into bin\Release or bin\Debug, you should modify step 2, change to use this in the .props file:

<Copy
     SourceFiles="@(SourceScripts)"
    DestinationFolder="$(ProjectDir)$(OutputPath)"     
/>

or just

<Copy
   SourceFiles="@(SourceScripts)"
   DestinationFolder="$(OutDir)"     
  />

as you want.

Before you install this new version, you should first delete nuget caches under C:\Users\xxx(current user)\.nuget\packages.

Mr Qian
  • 21,064
  • 1
  • 31
  • 41
  • WOW what a great answer! I will test this out ASAP and get back to you ASAP! Thanks –  Sep 25 '20 at 11:54
  • If I want to copy the xml file to the output directory (bin/Bebug, bin/Release etc.) how would I do that ? –  Sep 28 '20 at 07:17
  • Now I have DestinationFolder="$(ProjectDir)$(Configuration)" but the xml file is not showing up in either bin/Debug nor bin/Release. It is not showing anywhere actually. –  Sep 28 '20 at 07:37
  • Sorry for that. You should use `$(ProjectDir)$(OutputPath)`:) Before you install this new version, you should first delete nuget caches under `C:\Users\xxx(current user)\.nuget\packages`. – Mr Qian Sep 28 '20 at 07:45
  • 1
    Just one last thing, can I not use $(OutDir) instead ? –  Sep 28 '20 at 08:31
  • 1
    Sure. It is the same effect as you want. I have updated my answer and you can check it:) – Mr Qian Sep 28 '20 at 08:46
  • Hi again. When I run this from VS it works, BUT, when I do it from Azure DevOps no file is being copied. It's super frustrating! Any suggestions ? –  Oct 01 '20 at 09:33