How to stop NuGet packaging from including unwanted files - visual-studio

I'm building a NuGet package with a folder hierarchy that looks like this:
ProjectDir
Infrastructure
class1.cs
class2.cs
Task
class3.cs
class3.cs
StuffToInclude
SampleCode
sampleclass1.cs.pp
sampleclass2.cs.pp
Images
image1.gif
image2.gif
Doc
text1.txt
text2.txt
Project.nuspec
Note the nuspec file is generated simply with the "nuget spec" command, and then the following file section is added:
<files>
<file src="StuffToInclude\**\*.*" target="content" />
</files>
What I would expect (and want) is a content folder in the package that looks like this:
content
SampleCode
sampleclass1.cs.pp
sampleclass2.cs.pp
Images
image1.gif
image2.gif
Doc
text1.txt
text2.txt
What I get however is a slightly larger content folder that includes a second copy of the "StuffToInclude" folder, looking like this:
content
StuffToInclude
Images
image1.gif
image2.gif
Doc
text1.txt
text2.txt
SampleCode
sampleclass1.cs.pp
sampleclass2.cs.pp
Images
image1.gif
image2.gif
Doc
text1.txt
text2.txt
Notice that the undesired StuffToInclude folder does not have the SampleCode subfolder in it -- somehow the nuget packer figured out that ".pp" files should not be placed in the content unless explicitly asked for. But all those other files (in all the other folders) are unnecessarily duplicated and it is not desirable to have them there - because when the package is consumed, that folder is also duplicated in the target project.
I thought perhaps something like this in the nuspec would help:
<files>
<file src="StuffToInclude\**\*.*" target="content" exclude="StuffToInclude\**\*.*"/>
</files>
but all variants I have tried for the "exclude" attribute don't seem to help.
How do I exclude the "StuffToInclude" file from being included in the content folder?
Many thanks.

How to stop NuGet packaging from including unwanted files
I assume the command you use when creating the package is something like nuget pack xx.csproj.
If so, what's the special reason you created a package based on project file after creating the .nuspec file?
According to this document, we can use nuget pack command based on the specified .nuspec or project file.
So usually, if we create a package based on .csproj, there's no need to create a .nuspec file. Also, if we want to create a package based on .nuspec, I suggest use command nuget pack xx.nuspec instead of nuget pack xx.csproj.
Cause of the issue:
I've done some tests and found when we put Project.nuspec file in project dir, and use a command like nuget pack xx.csproj,then the nuget.exe will read data from both xx.nuspec and xx.csproj, that's why you have a second copy.
1# Structure for nuget pack xx.csproj if no xx.nuspec in same directory:
content
StuffToInclude
Images
image1.gif
image2.gif
Doc
text1.txt
text2.txt
2# Structure for nuget pack xx.nuspec:
content
SampleCode
sampleclass1.cs.pp
sampleclass2.cs.pp
Images
image1.gif
image2.gif
Doc
text1.txt
text2.txt
And when you use nuget pack command based on project file(.csproj) where exists a ProjectName.nuspec, it reads data from both .csproj and .nuspec. so it results in the second copy like what you got.
To resolve it:
Keep the xx.nuspec file and use command like nuget pack xx.nuspec.(suggested)
Delete the xx.nuspec file(Or delete the <Files> section) and use command like nuget pack xx.csproj, to include those xx.cs.pp files into content folder, you may need to set their build action to content.
Keep both the xx.nuspec file and the xx.csproj file (see below) and everything you want in the xx.nuspec file, but to avoid the unwanted second copies of things, make sure that every single item you don't want copied twice is set to a Build Action of None.

Related

Prevent duplicating files in NuGet content and contentFiles folders

My NuGet package needs to deliver some rather large files to build output directory.
In an old NuGet model, such files have to be stored in content folder of the .nupkg. While in a new model introduced in NuGet 3.3, such files have to be stored in contentFiles folder.
To maintain a compatibility with older versions of NuGet and mainly with Package.config package management format, I need to duplicate the files into both folders. That unfortunately almost doubles a size of the package.
Is there a way to prevent that? Can I somehow link contentFiles to content folder?
Found updated documentation describing this in detail at
MSBuild targets for NuGet.
By default, everything gets added to the root of the content and contentFiles\any\<target_framework> folder within a package and preserves the relative folder structure, unless you specify a package path:
<Content Include="..\win7-x64\libuv.txt">
<Pack>true</Pack>
<PackagePath>content\myfiles\</PackagePath>
</Content>
If you want to copy all your content to only a specific root folder(s) (instead of content and contentFiles both), you can use the MSBuild property ContentTargetFolders, which defaults to "content;contentFiles" but can be set to any other folder names. Note that just specifying "contentFiles" in ContentTargetFolders puts files under contentFiles\any\<target_framework> or contentFiles\<language>\<target_framework> based on buildAction.
If you only want to output the file to the build output (content only copies the file to the output directory but does cause it to be set as copy to output directory item), you can use a completely different approach by creating an msbuild file that will be included in the project.
You can do this by putting both the file - say test.jpg into the tools folder (you could also use build) and add a Your.Package.Id.targets file to the build folder (the name being the package id of your package with .targets as extension) with the following content:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Content Include="$(MSBuildThisFileDirectory)..\tools\test.jpg">
<Link>test.jpg</Link>
<Visible>false</Visible>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>
</Project>
This target will be automatically imported into the project files regardless of which "style" of NuGet reference is used (packages.config, PackageReference) and should be backwards compatible to older versions of VS as long as they support NuGet and ToolsVersion 4.0.
The Link metadata denotes where in the output / publish directories the file will end up. You could set it to e.g. defaultContent\images\foo.jpg to create a nested structure and rename the file. (you could even use MSBulid variables to use some of the referencing project's configuration). The Visible metadata prevents the solution explorer from showing the full relative path to the file, which could end up in lots of nested .. nodes. The CopyToPublishDirectory applies to .NET Core / ASP.NET Core apps or SDK-based projects using the Publish target for publishing.
Note that you can set the Inclue-path to anything depending on where in your package the file is. You can also use wildcards (but then set Link to %(Filename)%(Extension))

Install not adding all Content files

I have some xml files I want to distribute with the project. The Build Action is set to Content and the Copy to Output Directory is set to Copy always. When I publish, I see all the xml files in my content folder; however, when I install using ClickOnce, these files are missing. What am I missing?
The problem was in the project settings->Publish->Application Files... the xml files under Publish Status must be changed from Data File(Auto) to Include.

Set content files to "copy local : always" in a nuget package

I generate a nuget package from a project with this command in the post-build event. the variable %conf% is set to the right configuration (debug or release) and %1 is the project name (e.g. "MyCompany.MyProject").
nuget pack -Prop Configuration=%conf% "%1.csproj" -exclude *.sql -IncludeReferencedProjects
This package is for our own usage only, it will never be published on nuget. It ends in our private repository.
In the project, there is a file that is set to generate action : content and copy local : always. (My Visual Studio is in French, so I'm not 100% sure of the translation). Let's name it importantfile.xml.
In the generated package, I end up with this structure :
- content
- importantfile.xml
- lib
-net45 (.NetFramework,Version=v4.5)
-MyCompany.MyProject.dll
Which is fine, I want importantfile.xml to be deployed in the package, because, well, this file is important!
When I install the package in another project, importantfile.xml is deployed at the root of the project. That's OK. But it is not set to copy local : always.
I need importantfile.xml to be copy local : always in this project where I install my package.
How can I achieve that?
Notes :
I can set copy local : always on the file just after installing the package, that's no big deal. I would live with it if later updates of the package would let this property as-is, which is not the case. When updating the package, copy local is reset to never (as stated here).
There's a nuspec file in the project's folder, here it is :
<?xml version="1.0"?>
<package >
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$title$</title>
<authors>$author$</authors>
<owners>$author$</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<copyright>Copyright 2014</copyright>
<tags>some random tags</tags>
</metadata>
</package>
Instead of using a PowerShell script another approach is to use an MSBuild targets or props file with the same name as the package id:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)importantfile.xml">
<Link>importantfile.xml</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
In the nuspec file then, instead of adding the required files to the Content directory, add them to the Build directory along with the targets file.
Build
importantfile.xml
MyPackage.targets
lib
net45
MyAssembly.dll
If you require different content for different architectures then you can add architecture folders under Build also each with their own targets file.
Benefits to using a targets file over the PowerShell script with NuGet Content directory:
required content files aren't shown in the project in Visual Studio
content files are linked to rather than copied into the directory of each project which references the NuGet package (preventing there being multiple copies and keeping behaviour the same as for assemblies / libraries from NuGet packages)
PowerShell scripts only work in Visual Studio and aren't run when NuGet is run from the commandline (build servers, other IDEs and other OS), this approach will work everywhere
PowerShell install scripts are not supported in NuGet 3.x project.json system.
I know you guys got a working solution to this but it didn't work for me so I'm going to share what I pulled out of the NLog.config NuGet package install.ps1 (github source here).
NOTE: this is not my code, this is the content of the install.ps1 from
the NLog.config nuget package just sharing the knowledge.
It seems a little more straight forward to me and just hoping to help others that will likely stumble upon this.
You can find the accepted int values for BuildAction here and the accepted values for CopyToOutputDirectory here.
if the link breaks again
Fields
prjBuildActionCompile 1
The file is compiled.
prjBuildActionContent 2
The file is included in the Content project output group (see Deploying Applications, Services, and Components)
prjBuildActionEmbeddedResource 3
The file is included in the main generated assembly or in a satellite assembly as a resource.
prjBuildActionNone 0
No action is taken.
param($installPath, $toolsPath, $package, $project)
$configItem = $project.ProjectItems.Item("NLog.config")
# set 'Copy To Output Directory' to 'Copy if newer'
$copyToOutput = $configItem.Properties.Item("CopyToOutputDirectory")
# Copy Always Always copyToOutput.Value = 1
# Copy if Newer copyToOutput.Value = 2
$copyToOutput.Value = 2
# set 'Build Action' to 'Content'
$buildAction = $configItem.Properties.Item("BuildAction")
$buildAction.Value = 2
I have made this which copies files from my build folder to the output folder (bin/debug or bin/release). Works like a charm for me.
Nuspec file:
<package>
<files>
<file src="\bin\Release\*.dll" target="lib" />
<file src="\bin\Release\x64\*.dll" target="build\x64" />
<file src="\bin\Release\x86\*.dll" target="build\x86" />
<file src="MyProject.targets" target="build\" />
</files>
</package>
MyProject.targets
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<NativeLibs Include="$(MSBuildThisFileDirectory)**\*.dll" />
<None Include="#(NativeLibs)">
<Link>%(RecursiveDir)%(FileName)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
You can use PowerShell and the Install.ps1 hook provided by NuGet.
See the documentation.
Via PowerShell you have to 'search' for the content element which includes your importantfile.xml in an attribute. When the script found it, it has to add <CopyToOutputDirectory>Always</CopyToOutputDirectory> as a child element.
<Content Include="importantfile.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
You can find some PowerShell snippets here. Just take a look at the .ps1 files.
You could try the following (not tested). The file has to be named Install.ps1 and copied into the tools folder:
param($installPath, $toolsPath, $package, $project)
# Load project XML.
$doc = New-Object System.Xml.XmlDocument
$doc.Load($project.FullName)
$namespace = 'http://schemas.microsoft.com/developer/msbuild/2003'
# Find the node containing the file. The tag "Content" may be replace by "None" depending of the case, check your .csproj file.
$xmlNode = Select-Xml "//msb:Project/msb:ItemGroup/msb:Content[#Include='importantfile.xml']" $doc -Namespace #{msb = $namespace}
#check if the node exists.
if($xmlNode -ne $null)
{
$nodeName = "CopyToOutputDirectory"
#Check if the property already exists, just in case.
$property = $xmlNode.Node.SelectSingleNode($nodeName)
if($property -eq $null)
{
$property = $doc.CreateElement($nodeName, $namespace)
$property.AppendChild($doc.CreateTextNode("Always"))
$xmlNode.Node.AppendChild($property)
# Save changes.
$doc.Save($project.FullName)
}
}
You should also check if everything is removed completely when uninstalling the package.
Note by Jonhhy5
When updating the package via update-package, Visual Studio warns that the project is modified "outside the environnment". That's caused by $doc.Save($project.FullName). If I click reload before the command is fully terminated, it sometimes causes errors. The trick is to leave the dialog there until the process finishes, and then reload the projects.
I have written a little tool called NuGetLib to automatically add files to the nuget package after build.
create a tools folder with your Install.ps1 script
build your nugetPackage
add the tools folder to the built nugetPackage
https://stackoverflow.com/a/47134733/6229375

Octopack: Additional files copied in a pre-build event

I am trying to get Octopack to include some additional dll's that are not part of my Visual Studio project.
Is there a way to get Octopack to use my csproj files (as normal) and then specify just the additional files I need, rather than using the section in the nuspec. (Because then I have to maintain a list of all project files in my nuspec)
I am currently copying some shared libraries to a folder "/providers" in a pre-build event. Is there a way to extend on this to copy them somewhere that Octopack will see and package them?
You don't need to maintain a list of all your project files in your nuspec file, you can do this:
<files>
<file src="MyProject.Website\**\*.*" target="" />
</files>
This will grab everything in your project (all files and sub directories). If that's too much for you, you can take advantage of exclusion rules:
<files>
<file src="MyProject.Website\**\*.*" target="" exclude="MyProject.Website\docs\admin.txt" />
</files>
Hope that helps.
The downside of the answer of grabbing everything inside your project directory via the nuspec is that this could include files you don't want (see the answer's subsequent exclusion example).
A probably better way to do this is to ensure in your project that you have your files defined as "Copy to bin" or "Copy as content" and then build via msbuild w/Octopack using an additional command line argument of /p:OctoPackEnforceAddingFiles=true which tells OctoPack to combine the csproj and the nuspec, so you don't have to call out ALL the possible paths with a wildcard, you can only include files in your nuspec that aren't picked up from the regular build process. This is supported as of version 2.1.3 of OctoPack.
You could also use regular Nuget.exe and MSBuild by just specifying your csproj file, and if a nuspec exists with specific paths it should join them together.
See these reference links for better examples.
https://getadigital.com/no/blogg/setting-up-your-project-and-teamcity-octopack-for-front-end-builds/
https://octopus.com/docs/packaging-applications/nuget-packages/using-octopack/octopack-to-include-buildevent-files

Project file with just files and no built output

How can I make a project file (VS 2008) that just has some data files in and has no built output?
I can make an empty project and add my data files to it (which get copied to the output folder
), but it produces an EmptyProject.dll after I do a build. I want just my data files in the output directory and not some empty DLL or EXE.
I want the data files to be the only thing in this project as the project will be shared in a couple of solutions.
Our application is C#. All of our normal code projects are C#.
The data files are schemas (XSD). I want these schemas to be in the output folder, but I don't want them included with an existing project. I would like a project named "Schemas" that has nothing in except the XSD files and does nothing except copy the XSD files to the output folder. I would like this in a project file so that the same schemas project can be referenced in multiple solutions.
I don't know of a way to suppress the creation of the .dll file. BUT... here's an easy workaround. In the project properties, Build Events tab, write a Post-build event command line that will delete the file. Something like:
del path\filename.dll
Expanding on Scott's answer:
Create a new project of type Empty project
In Properties->Application, change Output type to Class Library
In Properties->Build->Advanced, change Debug Info to None
In Properties->Build Events, set the Post-build event command line to del $(TargetPath)
That way, the project creates only a DLL, which gets deleted. At the same time, the "copy to output directory" settings on your data files is respected.
Possibly another way is editing the csproj file by replacing this:
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
with this:
<Target Name="Build" />
<Target Name="Rebuild" />
Then builds don't create anything. It worked for me.
Same general idea should work for any xxproj file. Just replace the <Import Project...> tags with the <Target...> tags.
I'd be interested in knowing if this causes any issues or doesn't work for anyone.
What do you need a project for if you're not building it?
You can use solution folders to "store" files...
Why not just disable building this project for all configurations (use the Configuration Manager) - that way it won't build.
Great stuff. Expanding on Scott > Daniel's answer:
Safe to remove all References and Properties (AssemblyInfo.cs)
If it is a node/grunt/gulp project then you can invoke it in your Build Events > *Post-build event command line * eg: gulp build or gulp clean
Perhaps you can add removal or obj and bin output folders to your node/grunt/gulp clean scripts mitigating the need for del $(TargetPath)

Resources