0

Im using Visual Studio 2022 and created a Nuget Package using this article

https://arsenshnurkov.github.io/gentoo-mono-handbook/building-nupkg.htm

I run nuget pack and i see the nupg file and upload it to our Azure Artifacts. Below is the spec file

<package >
  <metadata>
    <id>myProject.csproj</id>
    <version>1.0.0</version>
    <authors>user</authors>
    <owners>user</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Test package 1</description>
    <releaseNotes>Summary of changes.</releaseNotes>
    <copyright>Copyright 2023</copyright>
    <tags>Tag test</tags>
  </metadata>
</package>

I noticed when i install this pack into a test application it includes some dlls that are not required for the end project to run.

After some research there are suggestions to change the .csproj to exclude it from Nuget when packing following this article https://www.jocheojeda.com/2019/07/22/how-to-exclude-package-dependencies-in-a-nuget-package/

but he writes the exact question i have in mind but no example of how to do this. In the article he quotes

the answer depend on how now you create your NuGet package, in this case, I’m going to focus my answer on excluding the dependency in a package created by the info in the csproj file (there is a different approach if you use the nuspec file).

I dont want to amend the csproj file but just the nuspec file. How could i leave out files that i dont want to have bundled with my package by amending the nuspec file?

5
  • 1
    Do you actually need a nuspec file at all? If your csproj file is an "SDK-style" one, I'd expect you to be able to package it without a nuspec file.
    – Jon Skeet
    Commented Jan 9, 2023 at 9:37
  • Im pretty new with packaging files using Nuget so im not so sure and just following the articles. The project being packed is a class library targeting .Net 4.7. I dont want to touch the .csproj file but more curious how to do this in the nuspec file but now confused as youre suggesting it could be done without it?
    – He-Man
    Commented Jan 9, 2023 at 9:45
  • Yes, if you use an SDK-style csproj file (which is perfectly capable of targeting .NET 4.7) then you probably don't need a separate nuspec file. I find that's significantly easier to manage - and SDK-style project files are also generally much easier to work with (as they don't list every individual source file etc)
    – Jon Skeet
    Commented Jan 9, 2023 at 9:46
  • I would say im not using an SDK style csproj (learn.microsoft.com/en-us/nuget/resources/check-project-format) its just .Net Framework. If you are suggesting to change/convert it (i would need to research if this is possible) i just need to ensure it doesnt break existing functionality if i was to install this package and overwrite existing files on current applications if i convert to an SDK style?
    – He-Man
    Commented Jan 9, 2023 at 9:53
  • Not sure what you mean by "install this package" but you should be able to convert the project to an SDK-style project without breaking it, yes. I suspect there are various documents around for how to do that.
    – Jon Skeet
    Commented Jan 9, 2023 at 11:36

1 Answer 1

0

In order to include/exclude files from nuget packaging, you can add the section to nuspec config file as described here.

Following your example, the files would exclude log files from your package. Using wildcards, you can exclude any other file/dll:

<package>
    <metadata>
    <id>myProject.csproj</id>
    <version>1.0.0</version>
    <authors>user</authors>
    <owners>user</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Test package 1</description>
    <releaseNotes>Summary of changes.</releaseNotes>
    <copyright>Copyright 2023</copyright>
    <tags>Tag test</tags>
    </metadata>
    <files>     
        <file src="*.*" exclude="*.log" />
    </files>
</package>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.