Nuget package generation Exclude lib folder

15,362

Solution 1

I wanted to share my experience. What I needed was to use csproj as Nuget.exe target (since I wanted NuGet to resolve dependencies automatically) and no lib folder in a result package. To omit that folder I used the following command:

nuget pack <projectPath> -Exclude bin/**/*.*

Solution 2

Use the NuGet Pack command option -Tool. See https://docs.microsoft.com/en-us/nuget/tools/cli-ref-pack.

"Determines if the output files of the project should be in the tool folder."

Solution 3

I solved this problem by firstly excluding all files from my project's output folder

<file src="bin\**\*.*" target="lib\net451" exclude="bin\**\*.*" />

And then when actually packaging via NuGet.exe I targeted my .nuspec and not my csproj. The resulting package did not contain my project's output dll under a /lib folder.

Solution 4

This is expected behavior when you pack by a project, as the whole point is that nuget will read the project and generate a package accordingly. And any .nuspec that is found will only be able to add content, not remove any previously added.

So what you note is your second update is what you are supposed to do - pack by the .nuspec instead.

Share:
15,362

Related videos on Youtube

TorontoKid
Author by

TorontoKid

Updated on January 19, 2020

Comments

  • TorontoKid
    TorontoKid over 4 years

    I am trying to generate nuget package with .nuspec file. We have several projects under one roof and trying to create nLog.config (And transform files) and distribute it via nuget package. For any version of .Net Framework I am looking for same set of config files (only configs no dll). So I really don't require \lib\net45\myproject.dll or \lib\net40\myproject.dll. Though when I generate nuget package it always create lib folder and include dll. Which sort of create dependency for any project related to .net framework version.

    Below is my nuspec file in case if someone wants to refer if I am doing something wrong. I tried "" and few other things but no luck.

    <?xml version="1.0"?>
    <package >
      <metadata>
        <id>NLogConfig</id>
        <version>1.0.3</version>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <copyright>Copyright 2013</copyright>
        <dependencies>
          <dependency id="NLog" version="2.1.0" />
          <dependency id="NLog.Schema" version="2.1.0" />
        </dependencies>
      </metadata>
      <files>    
        <file src="NLog.config" target="content" />
        <file src="NLog.Debug.config" target="content" />
        <file src="NLog.UAT.config" target="content" />
        <file src="NLog.Release.config" target="content" />
        <file src="tools\*.*" target="tools"/>          
      </files>
    </package>
    

    How can I exclude lib folder completely using nuspec file (Preferred) or other mechanism? Thanks !!

    enter image description here

    Update 1:

    I tried to sneak but was not successful. I put post build event and tried to delete DLL. But somehow system was smart it gave me error "Error 79 Unable to find 'C:\GITRepo\NLogConfig\NLogConfig\bin\Release\NLogConfig.dll'. Make sure the project has been built."

    Update 2

    I didn't found way around using .csproj to build nuget package but using external command and specifying nuspec file I was able to achieve same results. Question still remains this is my workaround only.

  • TorontoKid
    TorontoKid over 10 years
    Thanks for reply Dan !! Unfortunately it didn't work. I think I might not be clear in my question but I am trying to create package as part of the Visual Studio Build process using <BuildPackage>true</BuildPackage> for release and using <Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />. Excluding dll like such still includes lib folder and dll generated with same. So far I found only option is using nuspec and create package externally or putting same command in post build event.
  • TorontoKid
    TorontoKid over 10 years
    Thanks for reply Giammin !! Unfortunately that didn't work for me.
  • giammin
    giammin over 10 years
    @TorontoKid this is really strange...how are you generating that package? how do you call nuget.exe?
  • TorontoKid
    TorontoKid over 10 years
    If you modify .csproj file with "<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />" and for given configuration (In my case for Release) if you set <BuildPackage>true</BuildPackage>, each time you build release you will get nuget package generated in your output folder. You need to have .nuget\nuget.config, .nuget\nuget.exe and .nuget\nuget.targets in root folder to make it work. Please let me know if this is confusing and I can send you detailed email with setup screen shots.
  • giammin
    giammin over 10 years
    @TorontoKid i think you have to detach package creation from the project to make it works
  • TorontoKid
    TorontoKid over 10 years
    Thanks for taking a look @ it !! That's what I exactly did for workaround. I updated my question with edit on same "I didn't found way around using .csproj to build nuget package but using external command and specifying nuspec file I was able to achieve same results. Question still remains this is my workaround only." Again appreciate your suggestion.
  • Peter Ivan
    Peter Ivan over 6 years
    This actually worked, should be marked as an answer.
  • CJBS
    CJBS over 6 years
    This is good, and as mentioned, it's necessary to target the .nuspec. Targeting the .csproj results in the lib folder being included. I like this approach, because it doesn't require modifying syntax to the nuget command, instead including it with the .nuspec file.
  • CJBS
    CJBS over 6 years
    This certainly keeps the output from being installed as dependencies in the target project (the one to which the NuGet package is added), but does still result in the output being installed into the tools folder... However, I like "Dav Evans"'s approach more, as it includes the config in the .nuget package, rather than modifying nuget.exe pack syntax. stackoverflow.com/a/30883992/3063884
  • CJBS
    CJBS over 6 years
    Possibly this doesn't work because the target param isn't included.
  • Sam Rueby
    Sam Rueby about 6 years
    "nuget.exe : Cannot create a package that has no dependencies nor content." Which is wrong, because <files> is being used to populate the tools folder.
  • AndreyCh
    AndreyCh about 6 years
    Make sure you set correct paths for those files, looks like NuGet could not access them. If you use backslash as a path separator, try to replace it with slash.
  • Long Do Thanh
    Long Do Thanh over 3 years
    This actually ended up with creating tools folder. It does not help
  • JGV
    JGV over 3 years
    Works fine. +1 from me.