nuget contentFiles: is a binary file instead of a text file - nuget

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>

Related

Copy files from the NuGet package to the destination project when installing the nuget package

I'm working on the packaging part of module development and I was able to successfully create the package and install it on the destination site.
But I have an issue with copying files to the module folder in the destination project when installing. It creates some references for folders, not real folders. There are no physical files created in the modules folder in the destination project. But we noticed that it was created in the global NuGet folder on my computer (C:\Users<User>.nuget\packages).
Check the following Screenshots
I'm using Package.nuspec file and .targets file as follows:
Package.nuspec
<?xml version="1.0"?>
<package >
<metadata xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<id>QBankModule6</id>
<version>6.0.74</version>
<title>QBank Module 6</title>
<authors>QBNK AB</authors>
<owners />
<iconUrl>http://nuget.episerver.com/Framework/Styles/images/icons/default_package_icon.png</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>QBank Module is a module for media support. The module includes a QBank tool for EPiServer.Cms.TinyMce 2.1.1.0 or higher versions and media references. IMPORTANT: After first install, go to Admin mode to initiate QBank.</description>
<tags>EPiServerModulePackage ThirdPartyAddOn Media Images DAM Mediabank Streaming QBank</tags>
<dependencies>
<group targetFramework="net5.0">
<dependency id="EPiServer.CMS.Core" version="[12.0.3, 13)" />
<dependency id="RestSharp" version="106.11.7" />
<dependency id="log4net" version="[2.0.0, 2.9999)" />
<dependency id="HtmlAgilityPack" version="[1.8.4.0, 1.8.4.9)" />
</group>
</dependencies>
<contentFiles>
<files include="any/any/modules/_protected/QBankModule/ClientResources/Scripts/Editors/QBankMediaPicker.js" buildAction="None" copyToOutput="true"/>
<files include="any/any/modules/_protected/QBankModule/ClientResources/Scripts/qbank-connector.js" buildAction="None" copyToOutput="true"/>
<files include="any/any/modules/_protected/QBankModule/ClientResources/Scripts/QBankResponsiveLight-1.0.js" buildAction="None" copyToOutput="true"/>
<files include="any/any/modules/_protected/QBankModule/ClientResources/Scripts/jquery.js" buildAction="None" copyToOutput="true"/>
<files include="any/any/modules/_protected/QBankModule/ClientResources/Styles/qbankplugin.css" buildAction="None" copyToOutput="true"/>
<files include="any/any/modules/_protected/QBankModule/TinyPlugin/editor_plugin.js" buildAction="None" copyToOutput="true"/>
<files include="any/any/modules/_protected/QBankModule/module.config" buildAction="None" copyToOutput="true"/>
<files include="any/any/modules/_protected/QBankModule/lang/QbankModule_lang.xml" buildAction="None" copyToOutput="true"/>
<files include="any/any/modules/_protected/QBankModule/QBankModule.zip" buildAction="None" copyToOutput="true"/>
<files include="any/any/web.config.install.xdt" buildAction="None" copyToOutput="true"/>
<files include="any/any/web.config.uninstall.xdt" buildAction="None" copyToOutput="true"/>
<files include="any/any/QbankApi.dll" buildAction="Compile" copyToOutput="true"/>
<files include="any/any/Qbank.Connector.Core.dll" buildAction="Compile" copyToOutput="true"/>
<files include="any/any/Qbank.Connector.EpiCore.dll" buildAction="Compile" copyToOutput="true"/>
<files include="any/any/Qbank.Connector.EpiCore11.dll" buildAction="Compile" copyToOutput="true"/>
<files include="any/any/Qbank.Connector.EpiServerModule5.dll" buildAction="Compile" copyToOutput="true"/>
</contentFiles>
</metadata>
<files>
<file src="Qbank.Connector.EpiServerModule5/build/QBankModule6.targets" target="lib/net5.0" />
<file src="Qbank.Connector.EpiServerModule5/bin/net5.0/QbankApi.dll" target="lib/net5.0" />
<file src="Qbank.Connector.EpiServerModule5/bin/net5.0/Qbank.Connector.Core.dll" target="lib/net5.0" />
<file src="Qbank.Connector.EpiServerModule5/bin/net5.0/Qbank.Connector.EpiCore.dll" target="lib/net5.0" />
<file src="Qbank.Connector.EpiServerModule5/bin/net5.0/Qbank.Connector.EpiCore11.dll" target="lib/net5.0" />
<file src="Qbank.Connector.EpiServerModule5/bin/net5.0/Qbank.Connector.EpiServerModule5.dll" target="lib/net5.0" />
<!--<file src="Qbank.Connector.EpiServerModule5/ClientResources/Images/qbank-loader.gif" target="contentFiles/any/any/modules/_protected/QBankModule/ClientResources/Images/qbank-loader.gif" />-->
<!--<file src="Qbank.Connector.EpiServerModule5/ClientResources/Images/qbankadd.png" target="contentFiles/any/any/modules/_protected/QBankModule/ClientResources/Images/qbankadd.png" />-->
<file src="Qbank.Connector.EpiServerModule5/ClientResources/Scripts/Editors/QBankMediaPicker.js" target="contentFiles/any/any/modules/_protected/QBankModule/ClientResources/Scripts/Editors/QBankMediaPicker.js" />
<file src="Qbank.Connector.EpiServerModule5/ClientResources/Scripts/qbank-connector.js" target="contentFiles/any/any/modules/_protected/QBankModule/ClientResources/Scripts/qbank-connector.js" />
<file src="Qbank.Connector.EpiServerModule5/ClientResources/Scripts/QBankResponsiveLight-1.0.js" target="contentFiles/any/any/modules/_protected/QBankModule/ClientResources/Scripts/QBankResponsiveLight-1.0.js" />
<file src="Qbank.Connector.EpiServerModule5/ClientResources/Scripts/jquery.js" target="contentFiles/any/any/modules/_protected/QBankModule/ClientResources/Scripts/jquery.js" />
<file src="Qbank.Connector.EpiServerModule5/ClientResources/Styles/qbankplugin.css" target="contentFiles/any/any/modules/_protected/QBankModule/ClientResources/Styles/qbankplugin.css" />
<file src="Qbank.Connector.EpiServerModule5/TinyPlugin/editor_plugin.js" target="contentFiles/any/any/modules/_protected/QBankModule/TinyPlugin/editor_plugin.js" />
<!--<file src="Qbank.Connector.EpiServerModule5/TinyPlugin/Img/qbank.png" target="contentFiles/any/any/modules/_protected/QBankModule/TinyPlugin/Img/qbank.png" />-->
<file src="Qbank.Connector.EpiServerModule5/module.config" target="contentFiles/any/any/modules/_protected/QBankModule/module.config" />
<file src="Qbank.Connector.EpiServerModule5/Lang/QbankModule_lang.xml" target="contentFiles/any/any/modules/_protected/QBankModule/lang/QbankModule_lang.xml" />
<file src="Qbank.Connector.EpiServerModule5/web.config.install.xdt" target="contentFiles/any/any"/>
<file src="Qbank.Connector.EpiServerModule5/web.config.uninstall.xdt" target="contentFiles/any/any"/>
<!--CDN Solution-->
<!--<file src="Qbank.Connector.EpiServerModule5/ClientResources/Images/qbankaudio.png" target="contentFiles/any/any/modules/_protected/QBankModule/ClientResources/Images/qbankaudio.png" />-->
<!--<file src="Qbank.Connector.EpiServerModule5/ClientResources/Images/qbankdocument.png" target="contentFiles/any/any/modules/_protected/QBankModule/ClientResources/Images/qbankdocument.png" />-->
<!--<file src="Qbank.Connector.EpiServerModule5/ClientResources/Images/qbankvideo.png" target="contentFiles/any/any/modules/_protected/QBankModule/ClientResources/Images/qbankvideo.png" />-->
<!--<file src="Qbank.Connector.EpiServerModule5/build/QBankModule6.props" target="build" />-->
<file src="Qbank.Connector.EpiServerModule5/build/QBankModule6.targets" target="build" />
<file src="Qbank.Connector.EpiServerModule5/QbankModule.zip" target="contentFiles/any/any/modules/_protected/QBankModule/QBankModule.zip" />
</files>
</package>
QBankModule6.targets
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<ItemGroup>
<SourceScripts Include="$(MSBuildThisFileDirectory)..\..\contentFiles\any\any\modules\_protected\**\*.*"/>
</ItemGroup>
<Target Name="CopyZipFiles" BeforeTargets="Build">
<Copy
SourceFiles="#(SourceScripts)"
DestinationFolder="$(MSBuildProjectDirectory)\modules\_protected\%(RecursiveDir)"
/>
</Target>
</Project>
Also, I found this post an implemented solutions mentioned in the post. But ended up with the same result.
Basically, I need to understand how to copy some files from the NuGet package to the destination project when installing the nuget package. And also the difference between <files> tag and <contentFiles> tag in nuspec file.
Any input from someone who has experience on this would be great!

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 add .exe file into nuget package

There is an .exe file I would like to add my nuget package but I can't seem to find the proper way of doing it..
I would like to add the .exe file into the bin\Debug and bin\Release folders..
<?xml version="1.0" encoding="utf-8"?>
<package >
<metadata>
<id>MyApi</id>
<version>1.6</version>
<title>MyApi</title>
<authors>ASIM GUNDUZ</authors>
<owners>ASIM GUNDUZ</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<license type="expression">MIT</license>
<description>.Net api</description>
<releaseNotes>Hello World</releaseNotes>
<copyright>Copyright 2020</copyright>
<tags>AApi</tags>
</metadata>
<files>
<file src ="bin\Release\MyApi.dll" target="lib"/>
<file src ="Sample_Usage.cs" target="content\AApi\Sample_Usage.cs"/>
</files>
</package>

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.

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 ?