Nuget package for just content files

可紊 提交于 2020-07-06 12:41:06

问题


I am trying to create a .NET standard nuget package of content files (with no managed assemblies), to be consumed by .NET Framework assemblies. My aim is to get this folder of files copied to the bin folder of the consuming assembly.

This question is similar Add native files from NuGet package to project output directory (and uses this nuget package http://www.nuget.org/packages/Baseclass.Contrib.Nuget.Output/), but I require my nuget package to be .NET Standard

I have added the files in the assembly under contentfiles\any\any\myfiles and added the following to the metadata section of the nuspec file:

<contentFiles>
    <files include="any/any/myfiles/*" buildAction="None" copyToOutput="true" />
</contentFiles>

When I try to add this to a .NET framework assembly I get the error

Could not install package 'myPackage 1.0.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.6.2', 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 have also tried adding the files to the root of the project and adding the following beneath the metadata element in the nuspec file:

<files>
    <file src="myfiles\**\*" target="myfiles" />
</files>

Full nuspec file:

<?xml version="1.0"?>
<package>
    <metadata>
        <id>myPackage</id>
        <version>1.0.0</version>
        <title>myPackage</title>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>my files/description>
        <releaseNotes></releaseNotes>
        <tags></tags>
        <contentFiles>
            <files include="any/any/myfiles/*" buildAction="None" copyToOutput="true" />
        </contentFiles>
    </metadata>
    <files>
        <file src="myfiles\**\*" target="myfiles" />
    </files>
</package>

Also, in the csproj I've also changed the TargetFramework element to:

<TargetFrameworks>netstandard1.3;netstandard2.0;net40;net45;net46</TargetFrameworks>

SOLUTION:

In the end, this is the solution: "myFiles" is a folder that sits in the project root and contains many different files and sub-directories.

nuspec file:

<?xml version="1.0"?>
<package >
    <metadata>
        <id>myPackage</id>
        <version>1.0.0</version>
        <title>My Package</title>
        <tags></tags>
        <dependencies>
            <group targetFramework=".NETStandard2.0" />
            <group targetFramework=".NETFramework4.6.2" />
        </dependencies>
        <contentFiles>
            <!-- this sets "CopyIfNewer" on all files in the project that references this package -->
            <files include="**/*" buildAction="None" copyToOutput="true"/>
        </contentFiles>
    </metadata>
    <files>
        <file src="myFiles\**\*" target="contentFiles\any\any\myFiles"/>
    </files>
</package>

The project that references the nuget package will appear to have a folder called "myfiles" (with a link icon over all the files) which will get copied to the bin folder on build.


回答1:


There are some inferred ways to do this, but add can add the targetFramework as setting of dependencies here.

<dependencies>
  <group targetFramework=".NETStandard1.3" />
  <group targetFramework=".NETStandard2.0" />
  <group targetFramework=".NETFramework4.0" />
  <group targetFramework=".NETFramework4.5" />
  <group targetFramework=".NETFramework4.6" />
  <group targetFramework=".NETFramework4.6.2" />
</dependencies>

I would also recommend separating the .dll files explicitly from your content files in the files section of the nuspec file, and reference the lib\<targetName>\ convention.. (which actually relates to the inferred sentiment I reference to above, but I don't have much detail to provide on that at the moment)

So your nuspec file would look like this:

<?xml version="1.0"?>
<package>
<metadata>
    <id>myPackage</id>
    <version>1.0.0</version>
    <title>myPackage</title>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>my files/description>
    <releaseNotes></releaseNotes>
    <tags></tags>
    <contentFiles>
       <files include="any/netstandard1.3/myfiles/*" buildAction="None" copyToOutput="true" />
       <files include="any/netstandard2.0/myfiles/*" buildAction="None" copyToOutput="true" />
       <files include="any/net40/myfiles/*" buildAction="None" copyToOutput="true" />
       <files include="any/net45/myfiles/*" buildAction="None" copyToOutput="true" />
       <files include="any/net46/myfiles/*" buildAction="None" copyToOutput="true" />           
       <files include="any/net462/myfiles/*" buildAction="None" copyToOutput="true" />
    </contentFiles>
    <dependencies>
        <group targetFramework=".NETStandard1.3" />
        <group targetFramework=".NETStandard2.0" />
        <group targetFramework=".NETFramework4.0" />
        <group targetFramework=".NETFramework4.5" />
        <group targetFramework=".NETFramework4.6" />
        <group targetFramework=".NETFramework4.6.2" />
    </dependencies>
</metadata>
<files>
   <file src="<your-path>\<yourprojectassembly.dll>" target="lib\netstandard1.3\<yourprojectassembly.dll>" />
   <file src="<your-path>\<yourprojectassembly.dll>" target="lib\netstandard2.0\<yourprojectassembly.dll>" />
   <file src="<your-path>\<yourprojectassembly.dll>" target="lib\net40\<yourprojectassembly.dll>" />
   <file src="<your-path>\<yourprojectassembly.dll>" target="lib\net45\<yourprojectassembly.dll>" />
   <file src="<your-path>\<yourprojectassembly.dll>" target="lib\net46\<yourprojectassembly.dll>" />
   <file src="<your-path>\<yourprojectassembly.dll>" target="lib\net462\<yourprojectassembly.dll>" />
   <file src="<your-path>\myfiles\*" target="content\myfiles" />
   <file src="<your-path>\myfiles\*" target="contentFiles\any\netstandard1.3\myfiles" />
   <file src="<your-path>\myfiles\*" target="contentFiles\any\netstandard2.0\myfiles" />
   <file src="<your-path>\myfiles\*" target="contentFiles\any\net40\myfiles" />
   <file src="<your-path>\myfiles\*" target="contentFiles\any\net45\myfiles" />
   <file src="<your-path>\myfiles\*" target="contentFiles\any\net46\myfiles" />
   <file src="<your-path>\myfiles\*" target="contentFiles\any\net462\myfiles" />
</files>
</package>

Screenshot of what that will look like from a project that references your nuget package. (my content was a text file, and target was "lib\<target>\any" respectfully)


It is worth noting that there may be some behavior considerations to the content in the assembly that references to your package though.

in the above image, the content that came from the nuget package is by default referenced as No Copy to Output and C# Compile as the BuildAction.



来源:https://stackoverflow.com/questions/52110740/nuget-package-for-just-content-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!