【问题标题】:Prevent content files to be added on Nuget restore防止在 Nuget 还原时添加内容文件
【发布时间】:2018-12-11 17:47:26
【问题描述】:

我们需要一些可执行文件来创建我们的设置。所以我们打包了 将一些 .exe 文件放入 nuget 包中的外部依赖项。但在 NuGet 还原时,它们会被添加到项目根目录中。

我们怎样才能做到这一点?

四处搜索,但目前没有找到任何解决方案。

因为我们使用的是 nuspec 文件,所以这就是我所拥有的:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
 <metadata>
<id>VCRedistributable</id>
<version>$version$</version>
<title>VCRedistributable</title>
<authors>--</authors>
<owners>--</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>InstallVCRedistributable assemblies</description>
<contentFiles>
    <files include="**" exclude="**" buildAction="None" copyToOutput="false" 
 />
</contentFiles>
</metadata>

<files>
  <file src="VC\x86\*.*" target="content\x86" />
  <file src="VC\x64\*.*" target="content\x64" />
</files>

有什么想法吗?

【问题讨论】:

    标签: visual-studio nuget nuget-package-restore nuget-spec


    【解决方案1】:

    阻止内容文件添加到 Nuget 还原

    您应该定位到 tools 文件夹,而不是 content 文件夹。

    所以,您的 .nupsec 文件应该是:

    <?xml version="1.0" encoding="utf-8"?>
    <package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
     <metadata>
       <id>VCRedistributable</id>
       <version>$version$</version>
       <title>VCRedistributable</title>
       <authors>--</authors>
       <owners>--</owners>
       <requireLicenseAcceptance>false</requireLicenseAcceptance>
       <description>InstallVCRedistributable assemblies</description>
    
      </metadata>
    
      <files>
        <file src="VC\x86\*.*" target="tools\x86" />
        <file src="VC\x64\*.*" target="tools\x64" />
      </files>
    </package>
    

    那是因为内容目录是一个基于约定的工作目录,内容被复制到项目根目录:

    Convention-based working directory:

    另外,如果你的nuget包只包含外部一些.exe文件,你不必添加contentFiles标签,这个标签用于packagereference的内容文件。

    <contentFiles>
        <files include="**" exclude="**" buildAction="None" copyToOutput="false" 
     />
    </contentFiles>
    

    如果您有兴趣,可以查看this document了解更多详情。

    更新:

    创建我们自己的文件夹结构不是很好的约定吗? 根据上面的工具文件夹描述定义的 NuGet 似乎可以通过包管理器控制台访问它们。

    当然,您可以使用自己定义的 NuGet 以外的文件夹结构。但是您需要注意,这样做是有限制的。 你不能只包含你自己的文件夹结构,你还需要在你的.nuspec中添加一个 NuGet 定义的文件夹结构,否则,nuget 将安装失败并出现如下错误:

    无法安装包“MyCustomPackage 1.0.0”。你正试图 将此包安装到目标项目中 '.NETFramework,Version=v4.6.1',但包中不包含任何 与之兼容的程序集引用或内容文件 框架。

    因为 nuget 没有检测到您向项目中添加了程序集引用或内容文件。

    希望这会有所帮助。

    【讨论】:

    • 只是为了交叉检查。创建我们自己的文件夹结构而不是定义的 NuGet 是否是一个好的约定,因为基于上面的工具文件夹描述,它们似乎可以通过包管理器控制台访问。我们不希望对它们有任何引用,而只是在 NuGet 包中可用。
    • @Dave23Rave,当然可以。但是这样做是有限制的。详情请查看我的更新答案。
    • 感谢更新的答案。这解决了困惑并帮助我更好地理解事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    相关资源
    最近更新 更多