首页 > 解决方案 > nuget 内容/内容文件在嵌套的 nuget 包中重复

问题描述

我有 3 个相互嵌套的项目。

  1. HtAlarms.Common

    • 有配置文件HtAlarms.config
    • 从这里创建一个 nuget 包:HtAlarms.Common.nuget
  2. HtOpc.Alarms

    • 包含HtAlarms.Common.nuget
    • 从这里创建一个 nuget 包:HtOpc.Alarms.nuget
      • HtAlarms.config 错误地包含在 nuget 包中!
  3. HtServer.ServerTemplate
    • 包含HtOpc.Alarms.nuget

      - HtAlarms.config 错误地包含在根目录中!

HtAlarms.Common具有以下目录结构:

+-- Config
|   +-- NLog
|   |   +-- HtAlarms.config

在此处输入图像描述

在此处输入图像描述

HtAlarms.Common.csproj

...
  <ItemGroup>
    <Content Include="Config\NLog\HtAlarms.config">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
...

HtOPC.Alarms具有以下目录结构:

+-- Config
|   +-- NLog
|   |   +-- HtAlarms.config

在此处输入图像描述

在此处输入图像描述

HtOPC.Alarms.csproj

...
  <ItemGroup>
    <PackageReference Include="HtAlarms.Common" Version="6.0.0-fix-htalarms-config.68" />
  </ItemGroup>
...

但是nuget包裹已经有副本了!?

在此处输入图像描述


HtServer.ServerTemplate具有以下目录结构:

+-- Config
|   +-- NLog
|   |   +-- HtAlarms.config
+-- HtAlarms.config

在此处输入图像描述

在此处输入图像描述

HtAlarms.Common.nuget安装Config/NLog/HtAlarms.config-

HtOPC.Alarms.nuget安装HtAlarms.config-错误

也是。CopyToOutputDirectory_ false应该是true

有人可以告诉我这里有什么问题吗?谢谢!!

标签: .netvisual-studionugetnuget-packagevisual-studio-2019

解决方案


有人可以告诉我这里有什么问题吗?谢谢!!

据我所知,在Net Standard项目中(新的sdk格式项目,打包时只能通过PackageReference格式安装),当你把Build Action文件改成Content,然后点击打包,nuget会把这些文件放入nuget 包文件夹 ( HtAlarms.Commonxxxxx.nupkg) 中的 ContentFiles 文件夹。

当您将此软件包安装到新项目时,文件ContentFiles夹中的这些文件将自动复制到新项目中。

它与Build Action而不是有关CopyToOutputDirectory

有了您的进一步信息,您不希望此文件出现在第二个 nuget 包中HtOpc.Alarms,我建议您可以试试这个:

解决方案

1)将项目中文件的Build Action改为first然后在 item 下输入要添加的文件。HtAlarms.configHtAlarms.CommonNoneHtAlarms.Common.csproj<pack>true</pack>HtAlarms.config

在此处输入图像描述

在此处输入图像描述

然后单击打包并重新打包名为HtAlarms.Common.nuget.

2)然后进入第二个项目HtOpc.Alarms,首先卸载旧的nuget包HtAlarms.Common.nuget。其次,在将新的 nuget 包安装到项目之前清理 nuget 缓存。如果您安装了错误的 nuget 版本,这一点很重要。此步骤不添加,您可以直接重新打包这个新项目。HtAlarms.Common.nugetHtOpc.Alarms<pack>true</pack>HtOpc.Alarms.csprojHtOpc.Alarms.nuget

3)对于第三个项目,您可以尝试与第二步相同的步骤安装HtOpc.Alarms.nugetHtServer.ServerTemplate项目中。

更新 1

经过我的测试,我发现当我将nuget包安装到一个新项目中 Copy to Output Directory时,新导入的文件的默认HtAlarms.config设置为Never。我认为这是打包按钮的效果,使用默认设计的xxxx.nuspec文件来打包你的项目,当你使用xxx.nuspec文件时,它会忽略Copy to Output Directory文件的。

所以当你打包nuget包时HtAlarms.Common.nuget,打开它然后添加copyToOutput="true"HtAlarms.Common.nuspec文件中(使用zip工具打开HtAlarms.Common.1.0.0.0.nupkg

在此处输入图像描述

1)将此节点添加到文件中HtAlarms.Common.nuspec

copyToOutput="true"

在此处输入图像描述

另外,保存这个操作,清理nuget缓存,然后重新安装HtAlarms.Common.nugetHtOpc.Alarms项目中。

更新 2

由于您以后无法修改 nuget 包,因此我认为您使用nuget.exe cli将项目与xxxx.nuspec文件一起打包,而不是使用打包按钮。

首先,您应该下载nuget.exe,然后按照此指南将其配置到环境变量中,然后您可以在CMD中调用nuget。

1)打包项目时,不要使用Pack,打开CMD,先输入HtAlarms.Common.csproj存在的路径(如cd xxxxxxxx\HtAlarms.Common)。

在此处输入图像描述

在此处输入图像描述

2)键入 nuget spec 生成包描述文件HtAlarms.Common.nuspec。在此文件中,您可以根据需要命名包 id、版本、作者、描述等来打包包。

例如,就像这样:

请记住contentfiles节点和files节点添加到其中:

  <?xml version="1.0"?>
<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>$author$</authors>
    <owners>$author$</owners>
    <licenseUrl>http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE</licenseUrl>
    <projectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</projectUrl>
    <iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>1</description>
    <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
    <copyright>Copyright 2020</copyright>
    <tags>Tag1 Tag2</tags>

 <contentFiles>
      <files include="Config\NLog\HtAlarms.config" buildAction="None" copyToOutput="true" flatten="true" />
    </contentFiles>
  </metadata>
<files>
<file src="Config\NLog\HtAlarms.config" target="ContentFiles\any\Config\NLog" />
<file src="Config\NLog\HtAlarms.config" target="Content\Config\NLog" />
</files>
</package>

3)在输入nuget pack HtAlarms.Common.csprojCMD 生成新的 nuget 包之前HtAlarms.Common.nuget,你应该重建你的项目

当您在其他项目中重新安装它时,请记住先清理 nuget 缓存。

然后您也可以将此 nuget 包格式用于HtOpc.Alarms项目。

在此处输入图像描述

之后,您可以直接将这个 nuget 包安装到项目中。


推荐阅读