Very confused around creating a nuspec file with specific targets - nuget

I have a nuspec file with the following structure
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>XLabs.Forms</id>
<version>2.2.0-pre05</version>
<title>XLabs - Forms</title>
<authors>XLabs Team</authors>
<owners>XLabs Team</owners>
<licenseUrl>https://github.com/XLabs/Xamarin-Forms-Labs/blob/master/LICENSE</licenseUrl>
<projectUrl>https://github.com/XLabs/Xamarin-Forms-Labs</projectUrl>
<iconUrl>https://raw.githubusercontent.com/XLabs/Xamarin-Forms-Labs/master/Design/Icons/nuget/icon_nuget.png</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>This package contains the cross-platform XLabs Forms framework and controls.</description>
<summary>XLabs is a open source project that aims to provide a powerfull and cross platform set of controls tailored to work with Xamarin Forms.</summary>
<releaseNotes>Updated for Xamarin Forms 2.2</releaseNotes>
<copyright>Copyright © 2016 XLabs Team</copyright>
<tags>Xamarin XLabs Forms Controls GridView ListView</tags>
<dependencies>
<group>
<dependency id="XLabs.Platform" version="[2.2.0-pre05]" />
<dependency id="Xamarin.Forms" version="2.2.0.31" />
</group>
</dependencies>
</metadata>
<files>
<!-- Assemblies: Android -->
<file src="..\..\source\Forms\XLabs.Forms\bin\$configuration$\XLabs.Forms.dll" target="lib\monoandroid" />
<file src="..\..\source\Forms\XLabs.Forms\bin\$configuration$\XLabs.Forms.pdb" target="lib\monoandroid" />
<file src="..\..\source\Forms\XLabs.Forms.Droid\bin\$configuration$\XLabs.Forms.Droid.dll" target="lib\monoandroid" />
<file src="..\..\source\Forms\XLabs.Forms.Droid\bin\$configuration$\XLabs.Forms.Droid.pdb" target="lib\monoandroid" />
<!-- Assemblies: iOS -->
<file src="..\..\source\Forms\XLabs.Forms\bin\$configuration$\XLabs.Forms.dll" target="lib\Xamarin.iOS10" />
<file src="..\..\source\Forms\XLabs.Forms\bin\$configuration$\XLabs.Forms.pdb" target="lib\Xamarin.iOS10" />
<file src="..\..\source\Forms\XLabs.Forms.iOS\bin\$configuration$\XLabs.Forms.iOS.pdb" target="lib\Xamarin.iOS10" />
<file src="..\..\source\Forms\XLabs.Forms.iOS\bin\$configuration$\XLabs.Forms.iOS.dll" target="lib\Xamarin.iOS10" />
</files>
</package>
and it is driving me a little crazy :) The nupkg file that is created with this nuspec should ONLY install in a Xamarin iOS or base Xamarin Android project right? Not true, it is installing in any type of project (PCL, Windows 10, Windows 8, etc). I am at the point where I am not sure what else to do to limit the package so that it can only be installed in the targets that I need.
Can anyone see what I might be doing wrong?
UPDATE: Here is the latest version of the nuspec file that I have tried that still DOES NOT work. The following will create a package that installed in a Windows 8, Windows Mobile, and PCL project even though it only contains Android bases assemblies
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>XLabs.Forms</id>
<version>2.2.0-pre05</version>
<title>XLabs - Forms</title>
<authors>XLabs Team</authors>
<owners>XLabs Team</owners>
<licenseUrl>https://github.com/XLabs/Xamarin-Forms-Labs/blob/master/LICENSE</licenseUrl>
<projectUrl>https://github.com/XLabs/Xamarin-Forms-Labs</projectUrl>
<iconUrl>https://raw.githubusercontent.com/XLabs/Xamarin-Forms-Labs/master/Design/Icons/nuget/icon_nuget.png</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>This package contains the cross-platform XLabs Forms framework and controls.</description>
<summary>XLabs is a open source project that aims to provide a powerfull and cross platform set of controls tailored to work with Xamarin Forms.</summary>
<releaseNotes>Updated for Xamarin Forms 2.2</releaseNotes>
<copyright>Copyright © 2016 XLabs Team</copyright>
<tags>Xamarin XLabs Forms Controls GridView ListView</tags>
<dependencies>
<group targetFramework="monoandroid">
<dependency id="XLabs.Core" version="[2.2.0-pre05]" />
<dependency id="XLabs.IoC" version="[2.2.0-pre05]" />
<dependency id="XLabs.Platform" version="[2.2.0-pre05]" />
<dependency id="XLabs.Serialization" version="[2.2.0-pre05]" />
<dependency id="Xamarin.Forms" version="2.2.0.31" />
</group>
</dependencies>
</metadata>
<files>
<!-- Content Files -->
<file src="..\..\docs\XLabs.Forms.Readme.md" target="content\XLabs.Forms.Readme.md" />
<!-- Assemblies: Android -->
<file src="..\..\source\Forms\XLabs.Forms\bin\$configuration$\XLabs.Forms.dll" target="lib\monoandroid" />
<file src="..\..\source\Forms\XLabs.Forms\bin\$configuration$\XLabs.Forms.pdb" target="lib\monoandroid" />
<file src="..\..\source\Forms\XLabs.Forms.Droid\bin\$configuration$\XLabs.Forms.Droid.dll" target="lib\monoandroid" />
<file src="..\..\source\Forms\XLabs.Forms.Droid\bin\$configuration$\XLabs.Forms.Droid.pdb" target="lib\monoandroid" />
</files>
</package>

I believe it is because you have a group dependency that does not have a target framework. The group dependency will cause NuGet to install the Xamarin.Forms and XLabs.Platform NuGet packages whilst the assemblies in your lib directories would not be used unless you are installing into an iOS or Android project.
I would try adding a target framework for iOS and Android.
<group targetFramework="Xamarin.iOS10">
<dependency id="XLabs.Platform" version="[2.2.0-pre05]" />
<dependency id="Xamarin.Forms" version="2.2.0.31" />
</group>
<group targetFramework="MonoAndroid">
<dependency id="XLabs.Platform" version="[2.2.0-pre05]" />
<dependency id="Xamarin.Forms" version="2.2.0.31" />
</group>

Turns out the issue was with this line
<!-- Content Files -->
<file src="..\..\docs\XLabs.Forms.Readme.md" target="content\XLabs.Forms.Readme.md" />
Since that doesn't have a target moniker in it, it is valid for ALL target frameworks. Once I removed that, everything started working. I think the nuspec file format needs a LOT more details in its documentation :)

Related

nuget not installing file in project\bin directory

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

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 to define dependencies correctly in nuspec

I created a library for Xamarin forms and I want to publish it as Nuget package. There are several dependencies and in nuspec I used the following notation
<dependencies>
<group targetFramework="netstandard1.1">
<dependency id="NETStandard.Library" version="1.6.1" />
<dependency id="Xamarin.Forms" version="2.5.0.91635" />
<dependency id="Newtonsoft.Json" version="10.0.3" />
</group>
<group targetFramework="MonoAndroid">
<dependency id="Xamarin.GooglePlayServices.Location" version="42.1021.1" />
<dependency id="Xamarin.GooglePlayServices.Places" version="42.1021.1" />
<dependency id="Xamarin.Android.Maps.Utils" version="0.5.0" />
<dependency id="Xamarin.Forms" version="2.5.0.91635" />
<dependency id="Newtonsoft.Json" version="10.0.3" />
</group>
</dependencies>
Now when I reference the nuget package in a project that uses the latest Xamarin Google libraries, I get an error
Detected package version outside of dependency constraint: Xamarin.GooglePlayServices.Location 42.1021.1 requires Xamarin.GooglePlayServices.Basement (= 42.1021.1) but version Xamarin.GooglePlayServices.Basement 71.1620.0 was resolved.
Referencing the Nuget Docs my notation sais: accept all versions greater than or equal to 42.1021.1. So what's the correct notation to include 42.1021.1 and 71.1620.0 as well?
Try adding square brackets to the version to fully restrict the version you are using.
<!-- Accept only version 6.1. -->
<dependency id="ExamplePackage" version="[6.1]" />
<!-- Accepts any 6.x.y version. -->
<dependency id="ExamplePackage" version="[6,7)" />
For more documentation please look here:
https://learn.microsoft.com/en-gb/nuget/concepts/package-versioning#Constraining_Upgrades_To_Allowed_Versions

Nuget not copying content

I am having a problem including a file i need in order for the project to work, here is the nuspec file that i have:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>package_name</id>
<version>1.0.1</version>
<title>x Wrapper</title>
<authors>me</authors>
<owners>me</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>package desc.</description>
<releaseNotes>Bugfix to exe not being copied.</releaseNotes>
<copyright>Copyright 2017</copyright>
<tags></tags>
<dependencies>
<dependency id="x.Common.Interfaces" version="1.0.0" />
<dependency id="x.Common.Logging" version="1.0.0" />
<dependency id="x.Wrapper.Common" version="1.0.0" />
</dependencies>
</metadata>
<files>
<file src="build\package_name.props" target="build\package_name.props" />
<file src="build\package_name.targets" target="build\package_name.targets" />
<file src="x.Wrapper\x.exe" target="content\x.exe" />
<file src="bin\Debug\package_name.dll" target="lib\net45\package_name.dll" />
</files>
</package>
When package is pushed and installed to nuget server there is only project dll + dependencies dll's but no other files are being copied into the project. Anyone with some more experience with nuget packages has idea on what could be going on ?

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.