Nuget package for ILMerged dll - nuget

I would like to package up an ILMerge'd dll using Nuget.
Currently I can do this however the package also includes the dlls that I've just merged and also installs some other nuget package dependencies that I have.
What I would like would be a way of specifying in the nuspec file that it should only include my merged dll, and that it should not add references to the nuget dependencies so effectively when a user installs this package they will get a single dll reference in their project and no additional nuget dependencies.
Is this possible to define within the nuspec? or is there a better approach to achieve this?

Well, while packaging the DLL(s), make sure you do not have the dependent references in the lib. Also, make sure in the nuspec file, there are no references added.
Directory Structure:
My_Package
| - lib
| - [Required Dll's]
| - My_Package.nuspec
Edit the nuspec file and remove all references:
<dependencies>
<dependency id="DLLDependency" version="1.0" />
</dependencies>

Like so:
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<dependencies>
</dependencies>
</metadata>
<files>
<file src="bin\Release\mydll.dll" target="lib" />
<file src="bin\Release\mydll.pdb" target="lib" />
</files>
</package>

Related

Custom NuGet package not installing in wix project

I generate a NuGet that is is just a number of redist files that I want to use in one of my projects. If I install it in a C# or C++ projects, it works. But when I try to install it in a wixproj project and I get the following message:
Could not install package 'package-1.0.0'. You are trying to install this package into a project that targets 'Unsupported,Version=v0.0', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
I generate the package through a TeamCity task (using NuGet 5.6.0). When trying to generate the package with a NuGet CLI 5.8.1, I get the following warning:
*WARNING: NU5128: Some target frameworks declared in the dependencies group of the nuspec and the lib/ref folder do not have exact matches in the other location. Consult the list of actions below:
Add a dependency group for native0.0 to the nuspec*
Looked at https://learn.microsoft.com/en-us/nuget/reference/errors-and-warnings/nu5128, one of the solutions was trying a dependencies group targetFramework, (I used "native0.0") with no success. My nuspec is as follows:
<?xml version="1.0"?>
<package>
<metadata>
<id>package</id>
<version>1.0.0</version>
<authors>package</authors>
<owners>owner</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>my package</description>
<copyright>© 2021 company, Inc</copyright>
<tags>native</tags>
</metadata>
<files>
<file src="downloads\Folder\win32.vs2017\file1.lib" target="lib\native\lib\win32.vs2017\" />
<file src="downloads\Folder\win32.vs2017\file1-debug.lib" target="lib\native\lib\win32.vs2017\" />
<file src="downloads\Folder\Include\**" target="lib\native\include\" />
<file src="build\package.props" target="build\native" />
</files>
</package>
And my props file
<Project>
<PropertyGroup>
<MyVersion>1.0.0</MyVersion>
</PropertyGroup>
</Project>
I can install other NuGet packages into wixprojects, so how I configure mine to work? Thanks.
OK I found it, the issue lies at the line
<file src="build\package.props" target="build\native" />
changing target to "build\" allows the NuGet to be loaded to any project type, included WixProj. Note that the NU5128 warning still exists though, but not an issue for me.

Excluding the library being built from nuget package in .NET Standard

Bear with me - this is an unusual scenario.
I have 4 projects in my solution. The top most project references the 3 other projects. None of the 3 other projects reference each other. So the architecture is like this:
Now, when I build project A I want it to produce a nuget package containing projects B, C and D but not project A. As this is in .NET standard I can configure the packages tab of project A to produce a nuget package automatically when it builds by checking the 'Generate NuGet package on build option.' Then, I can get it to include B, C and D by making the following changes to A's csproj file:
<ItemGroup>
<ProjectReference Include="..\B.csproj">
<PrivateAssets>all</PrivateAssets>
</ProjectReference>
<ProjectReference Include="..\C.csproj">
<PrivateAssets>all</PrivateAssets>
</ProjectReference>
<ProjectReference Include="..\D.csproj">
<PrivateAssets>all</PrivateAssets>
</ProjectReference>
</ItemGroup>
<PropertyGroup>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
<Version>1.0.0-beta</Version>
<PackageId>A</PackageId>
<Company></Company>
<Product>A</Product>
<Description></Description>
<Authors></Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
<Target Name="CopyProjectReferencesToPackage" DependsOnTargets="ResolveReferences">
<ItemGroup>
<BuildOutputInPackage Include="#(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')->WithMetadataValue('PrivateAssets', 'all'))" />
</ItemGroup>
</Target>
Ideally I would like to add a line to remove A.dll from the nuget package. Is this possible? A is a wrapper project which consuming code will never need to use. It is not possible for B, C and D to reference each other.
UPDATE
This is how I solved it (thanks #tom redfern)
I created a nuspec file manually:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>A</id>
<version>1.0.0-beta</version>
<authors>Foo</authors>
<owners>Bar</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>A package</description>
<dependencies>
<group targetFramework=".NETStandard2.0">
</group>
</dependencies>
</metadata>
<files>
<file src="bin\Release\netstandard2.0\B.dll" target="lib\netstandard2.0\B.dll" />
<file src="bin\Release\netstandard2.0\C.dll" target="lib\netstandard2.0\C.dll" />
<file src="bin\Release\netstandard2.0\D.dll" target="lib\netstandard2.0\D.dll" />
</files>
</package>
Then in my .csproj file for A I put the following to automatically pack it after a build:
<Target Name="__PackNuGetPackage" AfterTargets="Build">
<Exec Command="$(NugetPackage)nuget.exe pack "A.nuspec"" />
</Target>
Using patented(1) elite(2) debugging skills, we can figure out if it's possible without manually creating and maintaining a nuspec file.
First, let's start with NuGet's docs on creating a package with the dotnet CLI. It says "msbuild -t:pack is functionality equivalent to dotnet pack". So, first hint, it's just running MSBuild targets.
So, run dotnet msbuild my.csproj -pp:pp.txt. This "pre-processes" (evaluates all MSBuild import statements and writes the result into a single file) the csproj (just a standard MSBuild file). We then search for the pack target, and scroll up until we find the filename of the file that was imported. We see it's NuGet.Build.Tasks.Pack.targets, and since NuGet is open source on GitHub, I can point you to the source.
Searching NuGet.Build.Tasks.Pack.targets for Condition, to see what extensibility options the NuGet team has provided, I see <IncludeBuildOutput Condition="'$(IncludeBuildOutput)'==''">true</IncludeBuildOutput>. So, settings <IncludeBuildOutput Condition="'$(IncludeBuildOutput)'==''">false</IncludeBuildOutput> in your csproj, might work.
(1) not patented
(2) standard, but since people don't modify MSBuild files anywhere near as often as C#, the skills and tools aren't as well known
You can achieve this by using a nuspec file. Use nuspec when you need absolute control over the nuget pack process. A simple nuspec file:
<package >
<metadata>
<id>MyPackage</id>
<version>1.0</version>
<authors>Something</authors>
<owners>Something</owners>
<description>Somthing</description>
<copyright></copyright>
<dependencies>
<!-- any nuget package dependencies -->
<dependency id="AnotherPackage" version="2019.2.4.1" />
</dependencies>
</metadata>
<files>
<!-- this is where you can have complete control over which assemblies get added to your package. You can add them individually pr using wildcards. -->
<file src="..\obj\**\*.dll" target="lib" />
</files>
</package>
When you have created your .nuspec file, add it into your solution, and then make your "Nuget Pack" build step read the nuspec file rather than the project file.

How can I keep NuGet from including dependent binaries?

I have a project that depends on another nuget package that drops native binaries into the bin directory of my project.
I need to create a Nuget spec to package up my binaries. The simple method
<package>
<metadata>
<id>MY ID</id>
<version>$version$</version>
<title>MY TITLE</title>
<authors>ME</authors>
<owners>ME</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>MY DESC</description>
<releaseNotes>Internal Version</releaseNotes>
<copyright>Copyright 2018</copyright>
<dependencies>
<dependency id="foo" version="3.0.6" exclude="all"/>
<dependency id="bar" version="3.0.6" exclude="all"/>
<dependency id="baz" version="3.0.6.101" exclude="all"/>
<dependency id="gronk" version="11.0.2" exclude="all" />
</dependencies>
</metadata>
</package>
results in a huge nupkg with lots of dependent binaries present, along with a pile of warnings:
WARNING: Issue: Assembly outside lib folder.
An empty files element suppresses the bins, but I really need some files (namely, a readme.md and some samples) in the package as well.
When I add the following to the nuspec:
<files>
<file src=".\Content\EventsHelpers.cs" target="content\Helpers" />
<file src=".\Readme.md" target="content" />
</files>
I get the content I need, but I then get the (large) files from the dependent binaries.
How can I get the files I want while suppressing the dependent binaries?
EDIT: I can do this via the command line, however, the CI pipeline I'm using doesn't let me set the command line. So I need to do it via the nuspec.

Nuspec buildAction not applying to contentFiles

I've been recently trying to create a .nuspec file that attaches a .dll file as an Embedded Resource. To do so, I've used the contentFiles tag on metadata, setting the buildAction="EmbeddedResource", as described in the section Example contentFiles section on the official documentation.
Below you can see my .nuspec file content:
<package>
<metadata>
<id>MyPackage</id>
<version>1.0.0.0</version>
<title>MyPackage</title>
<authors>Matias G Henschel</authors>
<owners>Matias G Henschel</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>My package description</description>
<copyright>2017</copyright>
<contentFiles>
<files include="myDllFile.dll" buildAction="EmbeddedResource" />
</contentFiles>
</metadata>
<files>
<file src="content\myDllFile.dll" target="contentFiles" />
</files>
</package>
This package correctly copies the file inside the target project, but it doesn't apply the Build Action to it, which is crucial for me.
I've also tried using a .targets file, with no success.
If you want to see more, I've also created an Issue on the GitHub page.
PS: IMHO, both documentation on contentFiles and .targets files require some rework, they aren't clear enough and .targets' lacks examples.
You also need a file entry to actually copy the file to the correct contentFiles folder:
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
…
<contentFiles>
<files include="any/any/MyEmbeddedFile.txt" buildAction="EmbeddedResource" />
</contentFiles>
</metadata>
<files>
<file src="path/to/MyEmbeddedFile.txt" target="contentFiles/any/any/MyEmbeddedFile.txt" />
</files>
</package>
Note that this will only work in NuGet 3.3+ with project.json based projects and NuGet 4+ for PackageReference based projects. For projects using this package via packages.config, you will still need to add the file to the content folder and add a custom target to make it the right item type.

Nuget Package won't install in UAP

I'm trying to create a custom nuget package out of one of my projects. When I try to install it into the test project that references it, I receive this error:
Package "package" 1.0.5 is not compatible with uap10.0.15063 (UAP,Version=v10.0.15063). Package "package" 1.0.5 supports: net (.NETFramework,Version=v0.0)
This obviously isn't the case because the test project can run fine referencing the Project itself.
I'm building the nuget package based on my nuspec file:
<?xml version="1.0"?>
<package >
<metadata>
<id>Project</id>
<version>1.0.0</version>
<title>Project</title>
<authors>company</authors>
<owners>company</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>desc</description>
<copyright>cp</copyright>
<dependencies>
<group targetFramework="uap ">
<dependency id="Logging" version="1.0.0" exclude="Build,Analyzers" />
<dependency id="Microsoft.NETCore" version="5.0.0" exclude="Build,Analyzers" />
<dependency id="Microsoft.NETCore.Portable.Compatibility" version="1.0.0" exclude="Build,Analyzers" />
<dependency id="NuGet.Build" version="2.12.0" exclude="Build,Analyzers" />
<dependency id="NuGet.CommandLine" version="4.1.0" exclude="Build,Analyzers" />
</group>
</dependencies>
</metadata>
<files>
<file src="bin\Release\*.dll" target="lib" />
</files>
"Logging" is also a custom nuget package I created from the .csproj file of another project.
I've tried removing the tag, and also tried renaming the targetFramework="uap", none of that works.
What could be wrong and what can I try to get this working?
The target doesn't just rely on the NuSpec file, it would depend on the C# project and the dll itself. Go to:
Project> Rightclick> Properties> TargetFramework>
Set it to .Net or whatever. Also, change your output type to a console application rather than a windows application, if that is doable.
Also, you should create the nuspec file using:
Nuget spec < ProjectPath >
This gives us the advantage to just change the metadata for .csproj files, and copies that into the package metadata automatically.
I'm not a 100% sure if this will fix it, but this should be what needs to be done. Best of luck!