【问题标题】:NuGet packages: Place dll and config files under a sub-directoryNuGet 包:将 dll 和配置文件放在子目录下
【发布时间】:2020-09-23 12:19:21
【问题描述】:

发现了许多过时和/或相关但不够充分的解决方案,因此我会试一试,希望我不会问重复的问题。

上下文

  • 我使用插件系统加载位于“bin/Plugin”文件夹下的插件 dll。
  • 每个插件都附带一个配置文件(即 MyPlugin.dll + MyPlugin.config)
  • 每个插件都使用私有 NuGet 服务器分发。
  • 我的项目中没有“.nuspec”文件。 “.nupkg”包是使用“.csproj”生成的,“nuspec”是由 nuget pack 命令自动生成的。
  • NuGet 项目和使用 NuGet 的项目都在使用 .NetFrameworkpackages.config(而不是 packageReferences)。

目标

由于插件是使用 NuGet 分发的,因此需要注意两点:

  1. 安装 NuGet 包时,不应将 dll 放在“bin”文件夹中,而应将其放在“bin/Plugin”子文件夹中。
  2. 在 NuGet 包中包含“.config”并将其放在同一位置。

对如何完成(1)和(2)有什么建议吗?


从问题中删除了之前添加的不成功步骤并发布了答案

【问题讨论】:

  • 嗨,关于这个问题的任何更新?
  • 越来越近了。我将在当天更新帖子(非常感谢您的帮助)

标签: .net visual-studio nuget visual-studio-2019 packages.config


【解决方案1】:

为了实现这一点,您应该创建一个<package_id>.props 文件,该文件可以根据需要将这些文件复制到目标路径中。

1) 在您的 MyPlugin 库项目中,您应该在 Build 文件夹下创建一个名为 &lt;package_id&gt;.props 的文件。

然后将这些内容添加到该新文件中:

<Project>
    <Target Name="CopyToFolder" BeforeTargets="Build">
        <ItemGroup>
            <Files Include="$(MSBuildThisFileDirectory)..\File\*.*"></Files>
        </ItemGroup>
        <Copy SourceFiles="@(Files)" DestinationFolder="$(ProjectDir)bin\Plugin"></Copy>
        
    </Target>

</Project>

2) 在您的MyPlugin.csproj 文件中添加这些节点:

<ItemGroup>
    <None Include="xxx\MyPlugin.dll(the path of MyPlugin.dll in your project)" Pack="true" PackagePath="File"></None>
    <None Include="xxx\MyPlugin.config(the path of MyPlugin.dll in your project)" Pack="true" PackagePath="File"></None>
    <None Include="Build\MyPlugin.props" Pack="true" PackagePath="build"></None>
</ItemGroup>
    

3)点击Pack菜单重新打包MyPlugin项目。而且在安装这个新版本的nuget包之前,应该先clean nuget caches或者删除C:\Users\xxx(current user)\.nuget\packages下的所有缓存文件。

除了,还有a similar thread you can refer to

================================================ ====

更新 1

关于第 1 步的说明:

.props 文件必须命名为您的package_id,这样它才能在 nuget nupkg 文件上工作。请查看我在上面提供的链接。

举个例子,如果你的nuget包命名为MyPlugin.1.0.0.nupkg,那么props文件应该命名为MyPlugin.props文件。目标的建议是将nupkg的File文件夹中的文件复制到目标主项目的bin\Plugin文件夹中。

关于第 2 步的说明:

这些步骤将您的 MyPlugin 项目文件的内容打包到 nupkg 中。

因此,如果您的 MyPlugin.config 位于 MyPlugin 项目的 bin\Release 文件夹下,您应该使用:

<None Include="bin\Release\netstandard2.0\MyPlugin.config(the path of MyPlugin.dll in your project)" Pack="true" PackagePath="File"></None>

您应该确保bin\Release\netstandard2.0\MyPlugin.config是正确的,并且由于它的相对路径(与csproj文件的路径比较),您可以找到该文件。

PackagePath="File" 表示应该将MyPlugin.config 文件打包到nupkg 的File 文件夹中。

<None Include="Build\MyPlugin.props" Pack="true" PackagePath="build"></None>

这一步是将MyPlugin.props打包到nupkg文件的build文件夹中。 Please see this document,

Build\MyPlugin.props 表示您的 MyPlugin 项目中 MyPlugin.props 的文件物理路径,以便 nuget 找到该文件。

The build folder of nupkg (3.x+) MSBuild .targets and .props files  Automatically inserted into the project.

原来是nupkg的功能。

无论如何,这一步可以自己检查,请检查文件路径是否正确,然后确保文件文件夹中存在目标文件nupkg。

这是我的 nupkg 结构:

================================================ =========================

另外,在安装这个新版本的 nuget 包之前,您应该先清理 nuget 缓存以删除旧缓存,以防您仍然安装旧缓存。

之后,首先构建你的主项目执行目标,然后你会看到我在这里展示的效果。

【讨论】:

  • 不幸的是——尽管配置文件已添加到我的“发布”文件夹中——但“.nupkg”文件中仍然缺少它(仅供参考——“.prop”文件仅在重命名后才执行到'Directory.Build.props')。难道我做错了什么?是否可以对每个步骤的含义添加一些描述?
  • 感谢您的反馈,请查看我的更新。并希望它可以帮助您理解和处理问题:)
  • 另外,你应该先构建你的主项目执行目标看看效果。
【解决方案2】:

为我的问题找到了一个几乎全自动的解决方案 (特别感谢@Perry-Qian-MSFT 的指导,以便正确处理)。

从新手的角度来看,以下是实现这一目标的步骤:

问题1:如何将文件包含到'.nupkg'中

为了将“MyPlugin.config”文件包含到“.nupkg”中,我编辑了“.csproj”文件并添加了以下内容:

<ItemGroup>
  <Content Include="MyPlugin.config">
    <Pack>true</Pack>
    <PackagePath>MyPlugin.config</PackagePath>
  </Content>
</ItemGroup>

此步骤将在 nuget 包内的“content”文件夹下添加“MyPlugin.config”文件。 请注意,“MyPlugin.config”文件应放在 NuGet 项目的根级别。

问题2:如何将文件复制到使用nuget的项目中

这是需要手动编辑的步骤。也将尝试自动执行此操作并更新答案。

在打包 nuget 之后(在将其推送到 NuGet 服务器之前),打开“nupkg”包(使用 winzip/winrar/7zip 等)并将以下内容添加到“.nuspec”文件中:

<metadata>
  <contentFiles>
    <files include="MyPlugin.config" copyToOutput="true" />
  </contentFiles>
</metadata>
<files>
  <file src="MyPlugin.config" target=""/>
</files>

这里不需要添加'MyPlugin.dll',因为它是一个lib文件。

保存并推送包到服务器。现在,当“消费者”项目安装 NuGet 时,配置文件将添加到项目中。配置文件在构建项目时不会添加到 bin 目录中

问题 3:如何将文件复制到消费者的 bin 文件夹中,在 'Plugins' 子目录下

在此步骤中使用了“props”文件。 props 文件旨在打包在 NuGet 包中,复制到消费者的根目录中,并在构建时由消费者项目执行。

在 NuGet 的项目中添加“Directory.build.props”文件并编辑“csproj”以添加以下内容:

  <Content Include="Directory.build.props">
    <Pack>true</Pack>
    <PackagePath>Directory.build.props</PackagePath>
  </Content>

编辑“Directory.build.props”文件的内容:

<Project>
  <Target Name="CopyToFolder" AfterTargets="Build">
    <ItemGroup>
      <ConfigFile Include="$(MSBuildThisFileDirectory)MyPlugin.config"></ConfigFile>
    </ItemGroup>
    <ItemGroup>
      <DllFile Include="$(MSBuildThisFileDirectory)bin\MyPlugin.dll"></DllFile>
    </ItemGroup>
    <Copy SourceFiles="@(ConfigFile)" DestinationFolder="$(ProjectDir)bin\Plugins"></Copy>
    <Move SourceFiles="@(DllFile)" DestinationFolder="$(ProjectDir)bin\Plugins" Condition="Exists('%(FullPath)')"></Move>
  </Target>
</Project>

在“nuspec”文件中添加“Directory.build.props”条目。因此,第 2 步将被增强为:

  <metadata>
    <contentFiles>
      <files include="MyPlugin.config" copyToOutput="true" />
    </contentFiles>
    <contentFiles>
      <files include="Directory.build.props" copyToOutput="true" />
    </contentFiles>
  </metadata>
  <files>
    <file src="MyPlugin.config" target="Plugins"/>
    <file src="Directory.build.props" target="Plugins"/>
  </files>

现在,props 文件将在消费者项目的根目录中结束。按照惯例,在构建项目 (MSBuild) 时会自动执行“Directory.build.props”文件。使用“复制”和“移动”任务,“dll”和“配置”都放在“bin/Plugins”文件夹下。

从第 0 方开始,我花了很多时间来实现这一点,希望它也能帮助其他人:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-24
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    相关资源
    最近更新 更多