25
votes

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.

8

8 Answers

14
votes

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/**/*.*
7
votes

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."

5
votes

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.

1
votes

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.

0
votes

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

0
votes

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>
0
votes

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.

0
votes

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