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

问题描述 投票:2回答:2

上次我必须找到如何从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导致在Nuget包文件的根目录下的NLog.config文件夹中包含NLog文件。

enter image description here

在我不同的尝试期间,根据我设置的配置,我能够在NLog.config或甚至src/ProjectName.Logging/NLog/NLog.config结束lib/netstandard2.0/Nlog.config文件。

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

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

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

有没有其他方法可以将我的配置文件从Nuget包复制到输出目录?

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

非常感谢

c# .net msbuild nuget .net-standard
2个回答
2
投票

如果你从.nuspec文件创建你的nuget,可以复制没有.csproj文件的文件,如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的文件夹中!


2
投票

好的,我终于找到了解决方案,其中包括一个.nuspec文件和一个.targets文件。

ProjectName.csproj只需要包括这个

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
        <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
        <NuspecFile>ProjectName.Logging.nuspec</NuspecFile>  
    </PropertyGroup>

    <!-- This is just for some projects referencing this project directly instead of the Nuget package -->
    <ItemGroup>
        <Content Include="NLog\NLog.config">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </Content>
    </ItemGroup>

    <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" />
        <PackageReference Include="NLog.Extensions.Logging" Version="1.2.1" />
        <PackageReference Include="NLog.Web.AspNetCore" Version="4.6.0" />
        <PackageReference Include="System.Data.Common" Version="4.3.0" />
        <PackageReference Include="System.Data.SqlClient" Version="4.5.1" />
    </ItemGroup>
</Project>

ProjectName.nuspec中,您将所有与Nuget包相关的内容放入其中

<?xml version="1.0"?>
<package >
    <metadata>
        <id>ProjectName.Logging</id>
        <version>1.1.0</version>
        <authors>Jérôme MEVEL</authors>
        <description>Just a logging component</description>
        <releaseNotes>Extract the NLog.config file automatically</releaseNotes>
        <dependencies>
            <dependency id="Dapper" version="1.50.5" />
            <dependency id="Microsoft.Extensions.DependencyInjection" version="2.1.1" />
            <dependency id="Microsoft.Extensions.Logging" version="2.1.1" />
            <dependency id="Microsoft.Extensions.Logging.Abstractions" version="2.1.1" />
            <dependency id="NLog" version="4.5.8" />
            <dependency id="NLog.Extensions.Logging" version="1.2.1" />
            <dependency id="NLog.Web.AspNetCore" version="4.6.0" />
            <dependency id="System.Data.Common" version="4.3.0" />
            <dependency id="System.Data.SqlClient" version="4.5.1" />
        </dependencies>
    </metadata>
    <files>
        <file src="bin\Release\netstandard2.0\ProjectName.Logging.dll" target="lib/netstandard2.0/ProjectName.Logging.dll" />    
        <file src="ProjectName.Logging.targets" target="build/ProjectName.Logging.targets" />
        <file src="NLog/Nlog.config" target="content/Nlog.config" />
    </files>
</package>

最后是ProjectName.targets。小心!该文件位于计算机的Nuget缓存中。您将能够在Visual Studio的项目根目录中看到它,但不能在Windows资源管理器中看到它(至少在Windows中)。因此,如果您在Visual Studio中修改该文件,它将为此计算机上引用相同Nuget包(和相同版本)的所有其他项目修改它。

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <Content Include="$(MSBuildThisFileDirectory)\..\content/NLog.config">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </Content>
    </ItemGroup>
</Project>

我使用了生成Nuget包 dotnet pack nuget pack命令(现在我有更多经验,我知道dotnet pack.nuspec文件不兼容,有几个错误)这是结果:

enter image description here

最后我可以安装我的软件包,在构建过程中,Nlog.config文件将从Nuget缓存中复制到我项目的输出目录中。


0
投票

我认为How do you set nuget contentFiles CopyToOutput value to true when using a .Net Standard library .csproj?为这个问题提供了更好的答案。 https://github.com/NuGet/NuGet.Client/pull/1450

您可以在.csproj中将PackageCopyToOutput设置为true,以将nuget内容文件声明为“CopyToOutput = true”。这样,任何引用nuget的项目都会在引用的csproj文件中使用<CopyToOutput>true</CopyToOutput>标记内容文件,指示msbuild将内容文件复制到ouput目录:

在nuget项目的.csproj中:

<Content Include="...">
    <PackageCopyToOutput>true</PackageCopyToOutput>
</Content>
© www.soinside.com 2019 - 2024. All rights reserved.