I have a NuGet package that depends on another NuGet package but I want to replace one of the unmanaged DLLs with one of mine. This works well, but now I'm trying to add support for both x86 and x64 using a .targets file. When my target application builds, it pulls down the original version, not the replacement.
Here's my .nuspec:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>MyPackage</id>
<version>2.0.0</version>
<dependencies>
<group targetFramework=".NETFramework4.5.2">
<dependency id="YourPackage" version="1.0.0" />
</group>
</dependencies>
</metadata>
<files>
<file src="MyPackage.targets" target="build\net452" />
<file src="..\lib\x86\thepackage.dll" target="lib\net452\x86" />
<file src="..\lib\x64\thepackage.dll" target="lib\net452\x64" />
</files>
</package>
Here's my .targets:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="PlatformCheck" BeforeTargets="InjectReference"
Condition="(('$(Platform)' != 'x86') AND ('$(Platform)' != 'x64'))">
<Error Text="$(MSBuildThisFileName) does not work correctly on '$(Platform)' platform. You need to specify platform (x86 or x64)." />
</Target>
<Target Name="InjectReference" AfterTargets="ResolveAssemblyReferences">
<ItemGroup Condition="'$(Platform)' == 'x86' or '$(Platform)' == 'x64'">
<Reference Include="MyPackage">
<HintPath>$(MSBuildThisFileDirectory)$(Platform)\thepackage.dll</HintPath>
</Reference>
</ItemGroup>
</Target>
</Project>
What am I doing wrong?
EDIT: If I change the .nuspec...
<file src="..\lib\x86\thepackage.dll" target="lib\net452" />
<file src="..\lib\x64\thepackage.dll" target="lib\net452" />
... then it pulls down my version but uses x86 for both x86 and x64 builds.
I solved it by putting the files in the build folder and then simply copying the target file.
.nuspec:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>MyPackage</id>
<version>2.0.0</version>
<dependencies>
<group targetFramework=".NETFramework4.5.2">
<dependency id="YourPackage" version="1.0.0" />
</group>
</dependencies>
</metadata>
<files>
<file src="MyPackage.targets" target="build\net452" />
<file src="..\lib\x86\thepackage.dll" target="build\net452\x86" />
<file src="..\lib\x64\thepackage.dll" target="build\net452\x64" />
</files>
</package>
.targets:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="PlatformCheck" BeforeTargets="InjectReference"
Condition="(('$(Platform)' != 'x86') AND ('$(Platform)' != 'x64'))">
<Error Text="$(MSBuildThisFileName) does not work correctly on '$(Platform)' platform. You need to specify platform (x86 or x64)." />
</Target>
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)$(Platform)\thepackage.dll">
<Link>%(RecursiveDir)%(FileName)%(Extension)</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Related
Adding a binary file to the "contentFiles" of my nuget package leads to a build error in my C# project.
CSC : error CS2015: '...\0.0.14\contentFiles\any\any\archive.7z' is a binary file instead of a text file [...]
My goal is to have this binary file in my build output folder after dotnet build and dotnet publish.
details:
# .nuspec
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
...
<contentFiles>
<files include="archive.7z" flatten="true" buildAction="None" copyToOutput="true" />
</contentFiles>
</metadata>
<files>
<file src="content/archive.7z" target="contentFiles\any\any" />
</files>
</package>
# folder structure
├───content
│ └───archive.7z
└───.nuspec
# .csproj file
<ItemGroup>
<PackageReference Include="my-nuget-package-name" Version="0.0.14" />
</ItemGroup>
Be sure to add the path to contentFiles "any/any/archive.7z"
<files include="any/any/archive.7z" flatten="true" buildAction="None" copyToOutput="true" />
so the correct .nuspec file should look like this:
# .nuspec
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
...
<contentFiles>
<files include="any/any/archive.7z" flatten="true" buildAction="None" copyToOutput="true" />
</contentFiles>
</metadata>
<files>
<file src="content/archive.7z" target="contentFiles\any\any" />
</files>
</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>
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 ?
I'm using the following *.targets file to add build actions
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)\libeay32.dll">
<Link>libeay32.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="$(MSBuildThisFileDirectory)\COMAssinaDocs.dll">
<Link>COMAssinaDocs.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="$(MSBuildThisFileDirectory)\InterOps.ComAssinaDocs.dll">
<Link>InterOps.ComAssinaDocs.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="$(MSBuildThisFileDirectory)\ssleay32.dll">
<Link>ssleay32.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
and this is the nuspec:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>xxx.Providers.SignDocs</id>
</metadata>
<files>
<file src="bin\Release\xxx.Providers.SignDocs.targets" target="build" />
<file src="bin\Release\xxx.Providers.SignDocs.dll" target="lib\net45" />
<file src="bin\Release\xxx.Providers.SignDocs.Impl.dll" target="lib\net45" />
</files>
</package>
And I'm using this piece of nuget documentation to build this package:
Including MSBuild props and targets in a package
Basically what I want is to get a few unmanaged dlls copied to the output folder of the project using the package and even tho I am following the instructions the target project file targets setion iss not added and therefore the files are not copied to the output directory. What am I missing?
UPDATE
Was able to get it working using the configurations bellow.
xxx.Providers.SignDocs.targets
<None Include="$(MSBuildThisFileDirectory)\libeay32.dll">
<Link>libeay32.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="$(MSBuildThisFileDirectory)\COMAssinaDocs.dll">
<Link>COMAssinaDocs.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="$(MSBuildThisFileDirectory)\InterOps.ComAssinaDocs.dll">
<Link>InterOps.ComAssinaDocs.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="$(MSBuildThisFileDirectory)\ssleay32.dll">
<Link>ssleay32.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>xxx.Providers.SignDocs</id>
</metadata>
<files>
<file src="..\..\tools\libeay32.dll" target="build" />
<file src="..\..\tools\COMAssinaDocs.dll" target="build" />
<file src="..\..\tools\InterOps.ComAssinaDocs.dll" target="build" />
<file src="..\..\tools\ssleay32.dll" target="build" />
<file src="bin\Release\xxx.Providers.SignDocs.targets" target="build" />
<file src="bin\Release\xxx.Providers.SignDocs.dll" target="lib\net45" />
<file src="bin\Release\xxx.Providers.SignDocs.Impl.dll" target="lib\net45" />
</files>
</package>
The targets file needs to be included in the 'package/files' element of the nuspec file, otherwise it isn't included in the package. It needs a target of 'build'.
The targets file also needs to be named the same as the package.
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 :)