gpt4 book ai didi

c# - 使用 .csproj 中的 MsBuild 和 dotnet pack 命令将文件从 Nuget 包复制到输出目录

转载 作者:行者123 更新时间:2023-12-02 01:36:04 27 4
gpt4 key购买 nike

上次我不得不找出如何从 Nuget 包中提取一些文件花了我至少 6 个月的时间,但我终于找到了 the solution .

问题是,此解决方案假设我有一个 .nupkg 文件,并手动添加一个 .targets 文件来执行提取过程。

现在,情况不同了:

  1. 我没有任何 .nupgk 文件,我们使用 dotnet pack 命令在 VSTS 服务器上自动生成一个文件。然后我们使用 Nuget 服务器中的包
  2. 我们不能再花 6 个月的时间来寻找解决方案

这是我的ProjectName.csproj

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
<Authors>Jérôme MEVEL</Authors>
<Version>1.0.3</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>

<!-- This answer got many votes on a Github thread so I tried just in case -->
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>

<!-- Just trying that too-->
<IncludeBuildOutput>true</IncludeBuildOutput>
<IncludeContentInPack>true</IncludeContentInPack>

<!-- I wanted to see the full generated Nuget package -->
<IncludeSource>true</IncludeSource>

<!-- desperate attempt -->
<TargetsForTfmSpecificBuildOutput>GetMyPackageFiles</TargetsForTfmSpecificBuildOutput>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="1.50.5" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.1.1" />
<PackageReference Include="NLog" Version="4.5.8">

<!-- Let's try this too just in case-->
<IncludeAssets>all</IncludeAssets>
</PackageReference>
<PackageReference Include="NLog.Extensions.Logging" Version="1.2.1">
<IncludeAssets>all</IncludeAssets>
</PackageReference>
<PackageReference Include="NLog.Web.AspNetCore" Version="4.6.0">
<IncludeAssets>all</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Data.Common" Version="4.3.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.5.1" />
</ItemGroup>
<ItemGroup>

<!-- Added the file to the root folder in case <IncludeAssets>all</IncludeAssets> is looking there -->
<Content Include="NLog.config">
<Pack>true</Pack>
<PackagePath>NLog;;</PackagePath>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>

<!-- My desired path to look for the file -->
<Content Include="NLog\NLog.config">
<Pack>true</Pack>
<PackagePath>NLog;;</PackagePath>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

<!-- This is the default setting, perfectly working when I reference the project instead of installing the Nuget package -->
<ItemGroup>
<None Update="NLog\NLog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<!-- desperate attempt -->
<Target Name="GetMyPackageFiles">
<ItemGroup>
<BuildOutputInPackage Include="NLog/Nlog.config">
<TargetPath>NLog</TargetPath>
</BuildOutputInPackage>
</ItemGroup>
</Target>
</Project>

正如你所看到的,我尝试了几种不同的设置。此 MsBuild 会生成一个 NLog.config 文件,该文件包含在 Nuget 包文件根目录的 NLog 文件夹中。

enter image description here

在我的不同尝试中,根据我设置的配置,我最终能够在 src/ProjectName.Logging/NLog/NLog.config 处获得 NLog.config 文件 甚至在 lib/netstandard2.0/Nlog.config

所以我的文件肯定包含在我的 Nuget 包文件中,但没有复制到使用该包的项目的输出目录中。

在使用 dotnet pack 生成包时,我尝试指定 .nuspec 文件,如 described here但我始终无法获得想要的结果(要么只有我的 NLog.config 包含在 Nuget 包中,要么包含在所有源文件中)。此外,这还有几个缺点,例如覆盖 .csproj 文件中的配置或增加无用的复杂性。我相信我想要实现的目标可以在不使用 .nuspec 文件的情况下完成(也许我错了)。

我注意到我的包中缺少 build/ProjectName.targets 文件,这可能就是缺少的部分。那么如何在不手动修改包的情况下添加这个.targets文件呢?

是否有另一种方法可以将我的配置文件从 Nuget 包复制到输出目录?

我真的希望有人能帮我解决这个问题。这是我第二次想要执行相同的操作,但略有不同,这又很难做到。

非常感谢

最佳答案

如果您按照所述从 .csproj 文件创建 nuget,则可以在没有 .nuspec 文件的情况下复制文件 here .

要将文件从 nuget 复制到输出目录,请创建一个包含以下内容的 ProjectName.targets 文件:

<ItemGroup>
    <LogFiles Include="$(MSBuildThisFileDirectory)\..\contentFiles\LogFiles\*.config" />
</ItemGroup>
<Target Name="CopyLogFiles" BeforeTargets="Build">
    <Copy SourceFiles="@(LogFiles)" DestinationFolder="$(TargetDir)CopiedLogFiles\" />
</Target>

在您的 .csproj 文件中添加:

<ItemGroup Label="FilesToCopy">
   <Content Include="ProjectName.targets" PackagePath="build/ProjectName.targets" />
   <Content Include="LogFiles\*.config" Pack="true" PackagePath="contentFiles\LogFiles">
     <PackageCopyToOutput>true</PackageCopyToOutput>
   </Content>
</ItemGroup>

路径和名称当然可以自由选择。

这应该将所有 .config 文件复制到输出目录中名为 CopiedLogFiles 的文件夹!

关于c# - 使用 .csproj 中的 MsBuild 和 dotnet pack 命令将文件从 Nuget 包复制到输出目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51924129/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com