问题是,该解决方案假设我有一个`.nupkg`文件并手动添加一个`.targets`文件来执行提取过程。
现在情况不同了:
- 我没有任何`.nupgk`文件,我们在VSTS服务器上使用`dotnet pack`命令自动生成一个。 然后我们从我们的Nuget服务器消耗这个包
- 我们无法再花费6个月的时间找到解决方案
<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
的文件夹下的NLog.config
文件。
在我不同的尝试过程中,根据我设置的配置,我能够最终得到位于src/ProjectName.Logging/NLog/NLog.config
甚至是lib/netstandard2.0/Nlog.config
位置的NLog.config
文件。
因此,我的文件肯定包含在Nuget包文件中,但没有复制到使用该包的项目的输出目录中。
我尝试指定使用带有dotnet pack
生成我的包的.nuspec
文件,就像这里描述的那样,但我从未能够获得期望的结果(要么只包括我的NLog.config
在Nuget包中,要么包括所有源文件)。此外,这有几个缺点,比如覆盖.csproj
文件中的配置或添加无用的复杂性。我相信可以在不使用.nuspec
文件的情况下完成我想要实现的目标(也许我是错的)。
我注意到我的包中缺少build/ProjectName.targets
文件,这可能是缺失的部分。那么如何在不手动修改包的情况下添加此.targets
文件?
是否还有其他方法将我的配置文件从Nuget包中复制到输出目录中?
我真的希望有人能够帮助我解决这个问题。这是我第二次想进行相同的操作,但稍有不同,再一次证明这很难做到。
非常感谢
dotnet pack
命令,无论是在PackageReference
还是Packages.config
方式下都能正常工作。与我的解决方案略有不同的是:你的方法在 Visual Studio 的解决方案资源管理器中不会创建符号链接,但我认为这并不是什么坏事。 - Jérôme MEVELPackageReference
,在Visual Studio浏览器中不要出现指向Nuget缓存中实际位置的符号链接是更好的选择。否则,在Visual Studio内更改此文件将同时更改引用相同Nuget包和版本的计算机上的其他项目,这很可能不是开发人员想要的行为。 - Jérôme MEVEL