NuGet 将文件复制到项目目录

问题描述

我创建了一个 .nuspec 文件,该文件打包了一堆 .proto 文件以便在项目之间共享。这很棒。不幸的是,要构建 .proto 文件,它们需要实际复制到项目目录中,而不仅仅是引用。请注意,这是一个 .NET Core 项目。

现在我的定义创建了一个包,在使用时会引用项目中的文件,但这些文件仍位于原始 .Nuget 文件夹中,而这并不是我真正需要的。

这是我现在得到的 .nuspec 定义:

<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <Metadata>
        <id>GRPCProtoFiles</id>
        <version>1.0.0</version>
        <authors>Author</authors>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>.proto files NuGet package.</description>
        <dependencies>
          <dependency id="Google.Protobuf" version="3.14.0"/>
          <dependency id="Grpc" version="2.35.0"/>
        </dependencies>
        <contentFiles>
          <files include="any/any/Protos/**/*.proto" buildAction="None" copyToOutput="false" />
        </contentFiles>
    </Metadata>
    <files>
        <file src="**\*.proto" target="contentFiles\any\any\Protos" exclude="google\**" />
    </files>
</package>

我知道“构建操作”当前设置为“无”,但是一旦文件在安装包时位于正确的文件夹中,我就可以修复该问题。照原样,如果我尝试将 proto 文件的构建操作设置为正确的操作(即 Protobuf 编译器),则会出现错误,因为这些文件不在它们应有的位置。

我做错了什么?

编辑:

好的,我已经阅读了一些关于可能发生的事情以及原因的信息。特别是 this 答案给了我很多见识。但是,我想知道如何才能真正做我需要做的事情。现在看来,我想要用 NuGet 做的事情是不可能的,而且我使用了错误的工具来完成这项工作。但除了这一限制之外,NuGet 似乎也很理想 - 它能够仅从源项目中选取 .proto 文件,然后发布该文件,而不是为各种目标系统预先构建的库。

我可以添加一个预构建操作来将文件复制到项目目录,但我不知道如何引用源 NuGet 包文件夹(尤其是在版本更改的情况下)。不知道这个问题是否有解决方案?

解决方法

我真的很了解你的想法。首先,使用 Build Action None 将这些 proto 文件打包到 nupkg 中。然后使用主项目下的预构建事件来处理来自 nupkg 的文件。问题是如何使用 msbuild 属性从 nupkg 获取文件,因为 Net Core 项目使用链接属性来引用主项目下的文件,而不是将真正的 nuget 文件复制到主项目文件夹中。

并且在链接属性下,您无法更改该文件的构建操作,因为它实际上不存在于主项目文件夹下,并且主项目没有义务处理该文件。

有两个函数可以解决它:

==============================================>

功能一

您可以直接在 nupkg 本身下执行此操作,之后,将在构建过程中使用 nuget 包自动复制 proto 文件。尝试以下步骤,您不应该从另一个安装了 nuget 包的主项目中添加任何复制任务。

你必须使用 <packages_id>.props/targets 文件进入 nupkg 才能得到你想要的。请参阅this official document

1) 创建一个名为 <packages_id>.props 的文件。在你这边,它应该被命名为 GRPCProtoFiles.props 以便它可以工作。

enter image description here

2) 将这些添加到 GRPCProtoFiles.props 文件中:

<Project>

    <Target Name="CopyFiles" BeforeTargets="Build">
        <ItemGroup>
            <File Include="$(MSBuildThisFileDirectory)..\contentFiles\**\*.*"></File>
        </ItemGroup>
        <Copy SourceFiles="@(File)" DestinationFolder="$(ProjectDir)\Protos"></Copy>
    </Target> 


</Project>

3)GRPCProtoFiles.nuspec 文件中添加这些以将 props 文件包含到 nupkg 中:

<file src="build\*.props" target="build" />

整个 GRPCProtoFiles.nuspec 文件应该是这样的:

<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <metadata>
        <id>GRPCProtoFiles</id>
        <version>1.0.0</version>
        <authors>Author</authors>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>.proto files NuGet package.</description>
        <dependencies>
          <dependency id="Google.Protobuf" version="3.14.0"/>
          <dependency id="Grpc" version="2.35.0"/>
        </dependencies>
        <contentFiles>
          <files include="any/any/Protos/**/*.proto" buildAction="None" copyToOutput="false" />
        </contentFiles>
    </metadata>
    <files>
        <file src="xxx.proto" target="contentFiles\any\any\Protos" exclude="google\**" />
        <file src="build\*.props" target="build" />
    </files>
</package>

4) 然后重新打包新版本的 nuget 项目。在您安装新发布版本之前,请clean nuget caches first 或删除 C:\Users\xxx\.nuget\packages

下的所有文件

安装nuget包后,请点击Build按钮执行目标。

更类似于this issue

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

功能二

您无法为 nupkg 本身执行任何内部步骤。您可以直接从外部主项目中复制文件。

1) 请勿在 功能一 下为您的 nuget 包执行任何步骤。使用旧的 nuget 包。

修改已安装 GRPCProtoFiles nuget 包的项目 A 的 csproj 文件。

PackageReference GRPCProtoFiles 添加 <GeneratePathProperty>true</GeneratePathProperty> 以生成专有属性作为 $(PkgGRPCProtoFiles) 以获取 nupkg 内容的路径。

整体是这样的:

<ItemGroup>
    <PackageReference Include="GRPCProtoFiles" Version="1.0.0">
      <GeneratePathProperty>true</GeneratePathProperty>
    </PackageReference>
 </ItemGroup>

然后,右键单击项目 A-->属性-->构建事件-->将这些添加到 >预构建事件命令行

xcopy  /s /e /y /i  $(PkgGRPCProtoFiles)\contentFiles\any\any\Protos  $(ProjectDir)\Protos

enter image description here

,将此目标添加到 ProjectA.csproj 文件中:

<Target Name="CopyFiles" BeforeTargets="PrepareForBuild">
        <ItemGroup>
            <File Include="$(PkgGRPCProtoFiles)\contentFiles\**\*.*"></File>
        </ItemGroup>
        <Copy SourceFiles="@(File)" DestinationFolder="$(ProjectDir)\Protos"></Copy>
</Target>

enter image description here