包复制到输出目录

包复制到输出目录

本文介绍了使用 .csproj 和 dotnet pack 命令中的 MsBuild 将文件从 Nuget 包复制到输出目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

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

非常感谢

解决方案

如果您从 .csproj 创建 nuget,则可以在没有 .nuspec 文件的情况下复制文件代码> 文件如所述 这里.

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

<LogFiles Include="$(MSBuildThisFileDirectory)..contentFilesLogFiles*.config"/></项目组><Target Name="CopyLogFiles" BeforeTargets="Build"><Copy SourceFiles="@(LogFiles)" DestinationFolder="$(TargetDir)CopiedLogFiles"/></目标>

在您的 .csproj 文件中添加:

<Content Include="ProjectName.targets" PackagePath="build/ProjectName.targets"/><Content Include="LogFiles*.config" Pack="true" PackagePath="contentFilesLogFiles"><PackageCopyToOutput>true</PackageCopyToOutput></内容></项目组>

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

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

Last time I had to find out how to extract some files from a Nuget package in took me at least 6 months but I finally managed to find the solution.

The thing is that, this solution assumes I have a .nupkg file and manually add a .targets file to perform the extraction process.

Now, things are different:

  1. I don't have any .nupgk file, we generate one automatically on our VSTS server using the dotnet pack command. Then we consume the package from our Nuget server
  2. We can't afford to take another 6 months to find the solution

Here is my 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="NLogNLog.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="NLogNLog.config">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </None>
    </ItemGroup>

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

As you can see I tried several different settings. This MsBuild results in a NLog.config file included in a NLog folder at the root of the Nuget package file.

During my different tries, depending of the configuration I set, I was able to end-up with the NLog.config file at src/ProjectName.Logging/NLog/NLog.config or even at lib/netstandard2.0/Nlog.config.

So my file is definitely included in my Nuget package file but isn't copied in the output directory of the project that consumes the package.

I tried to specify a .nuspec file when generating my package with dotnet pack like described here but I was never able to get a desired result (either only my NLog.config was included in the Nuget package or all the source files). Moreover, this has several downsides like overriding the configuration in the .csproj file or adding useless complexity. I believe what I want to achieve could be done without using a .nuspec file (maybe I'm wrong).

I noticed the build/ProjectName.targets file is missing in my package and this is probably the missing piece. So how to add this .targets file without manually modifying the package?

Is there another way to copy my config file out of the Nuget package to the output directory?

I really hope someone could help me solve this issue. This is the 2nd time I want to perform the same operation but with a slight difference and once again this is hard to do.

Thanks a lot

解决方案

It is possible to copy files without the .nuspec file, if you create your nuget from the .csproj file as described here.

And to copy files from nuget to output directory, create a ProjectName.targets file with the following content:

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

In your .csproj file add:

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

The paths and names can of course be freely chosen.

This should copy all .config files to a folder called CopiedLogFiles in the output directory!

这篇关于使用 .csproj 和 dotnet pack 命令中的 MsBuild 将文件从 Nuget 包复制到输出目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 02:52