Nuget包仅用于内容文件

问题描述 投票:1回答:1

我正在尝试创建一个.NET标准nuget包内容文件(没有托管程序集),供.NET Framework程序集使用。我的目标是将此文件夹输出到消耗组件的bin文件夹。

这个问题类似于Add native files from NuGet package to project output directory(并使用这个nuget包http://www.nuget.org/packages/Baseclass.Contrib.Nuget.Output/),但我要求我的nuget包是.NET Standard

我已在contentfiles \ any \ any \ myfiles下的程序集中添加了文件,并将以下内容添加到nuspec文件的元数据部分:

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

当我尝试将其添加到.NET框架程序集时,我收到错误

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.

我还尝试将文件添加到项目的根目录,并在nuspec文件中的metadata元素下添加以下内容:

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

完整的nuspec文件:

<?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>

另外,在csproj中我还将TargetFramework元素更改为

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

解:

最后这是解决方案。 “myFiles”是一个位于项目根目录中的文件夹,包含许多不同的文件和子目录。

nuspec文件:

<?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>

引用nuget包的项目似乎有一个名为“myfiles”的文件夹(在所有文件上都有一个链接图标),它将被复制到构建时的bin文件夹中。

c# .net nuget .net-standard
1个回答
1
投票

有一些推断的方法可以做到这一点,但添加可以添加targetFramework作为依赖项的设置。

<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>

我还建议明确地将.dll文件与nuspec文件的files部分中的内容文件分开,并引用lib\<targetName>\约定。(这实际上与我上面提到的推断情绪有关,但我没有太多目前提供的详细信息)

所以你的nuspec文件看起来像这样:

<?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>

从引用您的nuget包的项目中看到的内容的屏幕截图。 (我的内容是一个文本文件,目标是"lib\<target>\any"恭敬)

enter image description here


值得注意的是,可能会对程序集中引用您的程序包的内容进行一些行为考虑。

enter image description here

在上图中,来自nuget包的内容默认引用为No Copy to Output和C#Compile作为BuildAction。

© www.soinside.com 2019 - 2024. All rights reserved.