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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With