28

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
  • 733
  • 1
  • 7
  • 15

10 Answers10

15

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/**/*.*
AndreyCh
  • 1,298
  • 1
  • 14
  • 16
7

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

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

CJBS
  • 15,147
  • 6
  • 86
  • 135
  • 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. https://stackoverflow.com/a/30883992/3063884 – CJBS Jan 18 '18 at 19:56
  • This actually ended up with creating tools folder. It does not help – Long Do Thanh Sep 21 '20 at 13:16
5

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.

Dav Evans
  • 4,031
  • 7
  • 41
  • 60
  • 1
    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 Jan 18 '18 at 19:53
1

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.

Alex
  • 14,104
  • 11
  • 54
  • 77
1

Often you don't need/want a .nuspec file. In that case, you can control this from within the project file:

<PropertyGroup>
  <IncludeBuildOutput>false</IncludeBuildOutput>
  <IncludeSymbols>false</IncludeSymbols>
</PropertyGroup>
l33t
  • 18,692
  • 16
  • 103
  • 180
0

You can keep the lib files being included in the package, by using the exclude syntax. Something like below:

<files>
<file src="bin\Debug\*.*" exclude="bin\Debug\*.dll" />
</files>

For more information, refer to http://docs.nuget.org/docs/reference/nuspec-reference

Dan Liu
  • 812
  • 5
  • 1
  • 1
    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 true for release and using . 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 Dec 04 '13 at 16:28
  • Possibly this doesn't work because the `target` param isn't included. – CJBS Jan 18 '18 at 19:58
0

you have to remove the dependencies element and until you will use a project to build a package nuget will add a reference to those dlls

<?xml version="1.0"?>
<package >
  <metadata>
    <id>NLogConfig</id>
    <version>1.0.3</version>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <copyright>Copyright 2013</copyright>
  </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>
giammin
  • 18,620
  • 8
  • 71
  • 89
  • Thanks for reply Giammin !! Unfortunately that didn't work for me. – TorontoKid Dec 04 '13 at 18:13
  • @TorontoKid this is really strange...how are you generating that package? how do you call nuget.exe? – giammin Dec 05 '13 at 08:45
  • If you modify .csproj file with "" and for given configuration (In my case for Release) if you set true, 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. – TorontoKid Dec 06 '13 at 15:28
  • @TorontoKid i think you have to detach package creation from the project to make it works – giammin Dec 06 '13 at 15:38
  • 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. – TorontoKid Dec 06 '13 at 15:55
0

I am also having trouble with this and my issue with your workaround, which is to detach the package creation from the project, is that I am unable to use wildcards in the nuspec file. My workaround is to use the install.ps1 PowerShell script which will run after adding the package to a project. You will however need to set the ExecutionPolicy first in the package manager console first so that PowerShell scripts can run.

param($installPath, $toolsPath, $package, $project)
$project.Object.References | Where-Object { $_.Name -eq 'NLogConfig' } | ForEach-Object { $_.Remove() }

I really don't like this workaround because it will fail to run in machines where the ExecutionPolicy is restricted which is the default.

Donn R
  • 118
  • 8
0

So I was having the same issue, I didnt really care if the .dll was included but was having issued with .cs in my content as then the class would exists in both the content that was added to the target project and the dll that was added when the package was installed (guessing you were doing something along the same lines).

I ended up changing the build action to the .cs files that were in the content folder to NONE so that they would not end up in the DLL.

NOTE: when you do this none of the code in the .cs file is verified so you will want to do this as the last step when you know everything is right

It would be nice to exclude the DLL altogether but this was my workaround using the project as the build.

Hope this helps someone

workabyte
  • 3,496
  • 2
  • 27
  • 35
0

Try to add this line to your project file, works for me:

<None Remove="bin\**\*.*"/>
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 19 '23 at 20:34