8

This is the opposite problem from what I usually read about, which is people trying to get files included in the NuGet package. I'm trying to stop file from being included automatically.

I'm creating a library of pre-compiled MVC view templates (using RazorGenerator.Mvc), .less files, and JavaScript to support these as needed. The idea is that these are basic things that go into all of our sites.

By default, nuget pack includes any files marked Build Action: Content in your project, which any newly-created view, .less file, or script would be. There seems to be no option to override this behavior. A related question here presented a solution of an empty <files> node in the .nuspec to stop all includes, but I want to include some files, just only the ones I choose.

Now, as a stopgap, I can go through, explicitly set everything not to be Content (using None instead), then explicitly setup which files I do and don't want copied and where I want them copied to using the .nuspec file. However, if another developer comes along, such as my future self, and forgets this step, then the newly-added files will be copied into the package, and then into the destination project during the next install or update.

I found OctoPack, and that will including nothing by default as soon as it detects a <files> node in the .nuspec, but I'm not using anything else from TeamCity, and rewriting my build server scripts to understand how to invoke OctoPack instead of nuget pack doesn't strike me as a great option. OctoPack also has limitations; the dependencies have to be listed manually, the replacement tokens for the .nuspec file have to be passed in on command line (instead of read from AssemblyInfo.cs), and it just doesn't seem like the tool was intended for this use--they even say "OctoPack should only be installed on projects that you are going to deploy - that means the console application projects, Windows Service projects, and ASP.NET web applications. Unit tests, class libraries, and other supporting projects wouldn't be selected." (OctoPack Documentation)

The next alternative I tried was using msbuild to simply wipe out the Content group during the BeforeBuild event:

<Target Name="BeforeBuild">
    <ItemGroup>
      <Content Remove="**\*.*" />
      <None Include="**\*.*" />
    </ItemGroup>
  <Message Text="Content files: @(Content)" Importance="High" />
</Target>

The message verifies that the Content group is empty, but nuget pack doesn't seem to care; it still includes all the files anyway.

Has anyone successfully created a package like this, without having to manually mark everything Build Action: None and without using OctoPack?

2 Answers 2

6

It seems that if you invoke nuget pack MyProject.nuspec (instead of just nuget pack, which will look for both the .csproj and .nuspec), you can get the desired behavior, although you still (like with OctoPack) lose token substitution and automatic dependencies. So it's not a perfect solution, which would be something like --no-default-content, but it gets the job done well enough.

nuget pack MyProject.nuspec -Properties version={XYZ} (where XYZ is filled in by the build server) will at least let me make sure the version number is incremented correctly too. For example, I could set in the .nuspec

<version>1.1.0.$version$</version> and just use SVN revision as the last part.

3
  • “So it's not a perfect solution, which would be something like --no-default-content”—a more perfect solution would let you write something in the .nuspec file to control this. Who’s going to go to the work of remembering what parameters to pass to nuget each time?
    – binki
    Jan 19, 2015 at 6:15
  • Anyone using a continuous integration server doesn't have to remember. Now I have to remember that if I add a new dependency or change a dependency version, I have to manually add it to the nuspec, instead of being able to read that data from packages.config. Jan 20, 2015 at 12:31
  • It doesn't work. Specifying only .nuspec still includes everything by default. Trying it at solution level. ¯\_(ツ)_/¯ Mar 20, 2019 at 10:31
0

Another solution (still not ideal, but at least you can keep passing the .csproj file and not lose its advantages) is to delete the contents folder of the resulting .nupkg file (by treating it as a .zip).

An easy way to do this is via a script like this:

Remove files from .zip file with Powershell

Note that System.IO.Compression - used by this script - works only with PowerShell 3.0 and .NET 4.5.

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.