nuget not installing file in project\bin directory - nuget

Working in VS 2019 .NET framework 4.8
For my C# class library project, I ran nuget spec to create a nuspec file for the dll and its dependencies:
<?xml version="1.0" encoding="utf-8"?>
<package >
<metadata>
<id>MyId</id>
<version>1.0.0</version>
<title>MyTitle</title>
<authors>Me</authors>
....
<dependencies>
<dependency id="dll1" version="1.0.0" />
<dependency id="dll2" version="2.0.0" />
</dependencies>
</metadata>
</package>
No problems building the nuget package. In my consuming project (MyProject), I installed that package, and after building MyProject, the class library dll and the dependency files are placed in the MyProject\bin directory. Great.
However I need to get another file into directory MyProject\bin, let's call that file MyFile.txt. I first tried using the element:
.....
<dependency id="dll2" version="2.0.0" />
</dependencies>
</metadata>
<files>
<file src="MyFile.txt" target="lib" />
</files>
</package>
No luck. While the file is in the packages\lib folder, it does not get placed in MyProject\bin.
I then removed the element and tried the element within the tag:
.....
<contentFiles>
<files include="MyFile.txt" buildAction="None" copyToOutput="true" flatten="true" />
</contentFiles>
</metadata>
</package>
No luck that way either. Is there a way to accomplish what I need to do? It looks like it may involve having some folders in my class library root folder, but not sure what folders (content?) or how to properly reference them in the nuspec file. Thanks.

Neglected to update this post with the answer I found.
Using the <files><file /></files> logic is what solved it. It involved the use of the target attribute and using the content keyword in the value:
<files>
<file src="content\bin\MyFile.txt" target="content\bin" />
</files>
It's in the documentation, I unfortunately missed that even upon multiple readings. See https://learn.microsoft.com/en-us/nuget/reference/nuspec#dependencies-element

Related

How to get csproj to add the reference file for a dll for a nuget package?

I'm able to add a dll and make sure it's in lib and ref folders so I can avoid the MSB3246, NU5128, and NU5131 warnings in a .nuspec file.
<?xml version="1.0" encoding="utf-8"?>
<package >
<metadata>
<id>NewDll</id>
<version>1.0.0</version>
<title>NewDll</title>
<authors>Dll Dev Team</authors>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<license type="expression">MIT</license>
<projectUrl>https://new.dll.com</projectUrl>
<description>Allow NewDll dll to be used in .NET Framework 4.8 as a nuget package.</description>
<releaseNotes>Remove missing reference assembly warning.</releaseNotes>
<copyright>2021</copyright>
<dependencies>
<group targetFramework=".NETFramework4.8">
</group>
</dependencies>
<references>
<group targetFramework="net48">
<reference file="newdll.dll" />
</group>
</references>
</metadata>
<files>
<file src="C:\Users\me\source\repos\Project1\dlls\newdll.dll" target="lib\net48\newdll.dll" />
<file src="C:\Users\me\source\repos\Project1\dlls\newdll.dll" target="ref\net48\newdll.dll" />
</files>
</package>
But I don't know how to do that in a .csproj file. I've gotten only part of the way there.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<Description>Put NewDll.dll to be used in .NET Framework 4.8 as a nuget package.</Description>
<PackageReleaseNotes>Remove missing reference assembly warning.</PackageReleaseNotes>
<Version>1.0.0</Version>
</PropertyGroup>
<!-- How do I add a reference? -->
<ItemGroup>
<Content Include="NewDll.dll">
<Pack>true</Pack>
<PackagePath>lib\$(TargetFramework)</PackagePath>
</Content>
</ItemGroup>
</Project>
How do I make sure that the dll also creates the ref folder in the nuget package in the .csproj?
You can add multiple paths to PackagePath, e.g.:
<ItemGroup>
<None Include="NewDll.dll">
<Pack>true</Pack>
<PackagePath>lib\$(TargetFramework);ref\$(TargetFramework)</PackagePath>
</None>
</ItemGroup>

How does NuGet contentFiles work?

I'm trying to create a nuget package with content which work with different kinds of projects.
TestSource.nuspec look like this:
<contentFiles>
<files include="cs\any\TestSource.cs" buildAction="Compile" />
</contentFiles>
</metadata>
<files>
<file src="TestFolder\TestSource.cs" target="content/TestFolder" />
</files>
(the file is available both as contentFiles\cs\any\TestSource.cs and TestFolder\TestSource.cs)
The created package works fine in a "normal" project but nothing happens in a packagereference project.
I assume I'm missing something simple
Found this out myself.
You need to add the file twice in the files section
<contentFiles>
<files include="cs\any\TestSource.cs" buildAction="Compile" />
</contentFiles>
</metadata>
<files>
<file src="TestFolder\TestSource.cs" target="content/TestFolder" />
<file src="TestFolder\TestSource.cs" target="contentFiles/cs/any" />
</files>
The package will then work for both normal and packagereference projects.

Reference nuspec id elsewhere in the nuspec file?

I have a nuspec file defined like so:
<?xml version="1.0"?>
<package>
<metadata>
<id>PSP.Build.Vcs.Svn</id>
</metadata>
<files>
<file src="tools\**\**\**\PSP.Build.Vcs.Svn\Set-BuildNumber.psm1" target="tools" />
</files>
</package>
As you can see the id is "PSP.Build.Vcs.Svn" and that same literal is repeated in the file element. In the interests of DRY, is it possible to reference the id elsewhere in the nuspec file? I'm imagining something like the following (even though this didn't actually work):
<?xml version="1.0"?>
<package>
<metadata>
<id>PSP.Build.Vcs.Svn</id>
</metadata>
<files>
<file src="tools\**\**\**\$(id)\Set-BuildNumber.psm1" target="tools" />
</files>
</package>
I suspect the answer is no, but just thought I'd ask.
TIA
if you have setup your build process such as that the value of $id$ is PSP.Build.Vcs.Svn then you could replace that with $id$.
See http://docs.nuget.org/Create/Nuspec-Reference#replacement-tokens

Force nuget to always update a dependency

I wish to ensure my nuget package always updates to the latest version of another dependent package. Is there anyway I can do this?
My current nuspec looks like
<?xml version="1.0"?>
<package>
<metadata>
<id>Engine</id>
<version>$version$</version>
<title>Engine</title>
<authors>AgResearch</authors>
<owners>Overseer </owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>My budget model</description>
<releaseNotes>Release model</releaseNotes>
<copyright>Copyright 2014</copyright>
<tags>Overseer engine</tags>
<dependencies>
<dependency id="OverseerEngine.Schema" version="" />
<dependency id="Rezare.Lib" version="1.0.23.2" />
</dependencies>
</metadata>
<files>
<file src="OverseerEngine.dll" target="lib\net40\OverseerEngine.dll" />
<file src="OvrFileImport.dll" target="Content" />
</files>
</package>
It is the OverseerEngine.Schema package that I wish to always be updated (if an update is available) whenever this package is.
Currently there is not an automated way to do this. User has to manually update the package using the console or the update tab of manage nuget packages dialog.

Packaging SQL files only using Nuget.exe

Is there a way to package a folder of only .SQL files instead of having to add them to a project using Nuget.exe?
I know you can specify folders inside the nuspec, but what is the process of doing so? I've only been able to make packages from using .net project/applications.
For example, If I create a nuspec inside called Database.nuspec
F:\folder\trunk\Source\Database.nuspec
inside the folder
F:\folder\trunk\Source\Database
and I want to package up my patch scripts folder
F:\folder\trunk\Source\Database\Patch Scripts
and inside that folder I had
F:\folder\trunk\Source\Database\Patch Scripts\2.0
F:\folder\trunk\Source\Database\Patch Scripts\2.1
F:\folder\trunk\Source\Database\Patch Scripts\2.2
Would I need to include these folders inside of my nuspec or is nuget.exe smart enough to package them up for me?
E.G
Update:
Solution 1:
would be to include each folder to the nuspec
<files>
<file src="F:\folder\trunk\Source\Database\*.sql" target="Database" />
<file src="F:\folder\trunk\Source\Database\Patch Scripts\*.sql" target="Database\Patch Scripts\" />
<file src="F:\folder\trunk\Source\Database\Patch Scripts\2.0\*.sql" target="Database\Patch Scripts\2.0" />
</files>
But I have a lot of patch files and it will be quite tedious to write out about 40 into a nuspec, I will update if I find a work around.
Solution 2:
ufuk-haciogullari suggested using
*\*.sql
as the source since they are one level down, and this does what I want, but I need to keep the file structure intact, If i were to just write
<files>
<file src="F:\folder\trunk\Source\Database\*.sql" target="Database" />
<file src="F:\folder\trunk\Source\Database\Patch Scripts\*\*.sql" target="Database\Patch Scripts" />
</files>
it will store all the returns into the target Database\Patch Scripts\ and this isn't the structure I want.
Update:
<?xml version="1.0" encoding="utf-8" ?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>database</id>
<version>1.0.6</version>
<authors>me</authors>
<description>Description</description>
</metadata>
<files>
<file src="F:\folder\trunk\Source\Database\*.sql" target="Database\" />
<file src="F:\folder\trunk\Source\Database\Patch Scripts\**\*.sql" target="Database\Patch Scripts" />
</files>
This packages my solution and stores the files as they were in the previous format.
nuget.exe pack "F:\folder\trunk\Source\Database\database.nuspec" -OutputDirectory F:\NuGetStore
UPDATE:
Coming back to this, if you leave the target blank in it will re-create the same folder structure that is already in place, so you don't need to create directories.
<?xml version="1.0" encoding="utf-8" ?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>database</id>
<version>1.0.6</version>
<authors>me</authors>
<description>Description</description>
</metadata>
<files>
<file src="F:\folder\trunk\Source\Database\*.sql" target="" />
<file src="F:\folder\trunk\Source\Database\Patch Scripts\**\*.sql" target="" />
</files>
This packages my solution and stores the files.
nuget.exe pack "F:\folder\trunk\Source\Database\database.nuspec" -OutputDirectory F:\NuGetStore
You can create a nuspec file and state those files explicitly.
<?xml version="1.0" encoding="utf-8" ?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>MyPackage</id>
<version>1.0.0</version>
<authors>me</authors>
<description>Description</description>
</metadata>
<files>
<file src="*.sql" target="Content\SqlFiles" />
</files>
</package>
Then run pack command on the nuspec file.
NuGet.exe pack MyPackage.nuspec