使用 GeneratePackageOnBuild 和 contentFiles 构建 dotnet

dotnet build with GeneratePackageOnBuild and contentFiles

我有一个正在使用 dotnet build -c Release /p:Platform=x86 .\Foo.csproj 构建的项目。 csproj 文件为 SDK 格式,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <TargetFramework>net48</TargetFramework>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    <AssemblyName>Foo</AssemblyName>
    <Company>Me</Company>
    <Authors>Me</Authors>
    <Version>4.12.5</Version>
    <AssemblyVersion>4.12.5.0</AssemblyVersion>
    <FileVersion>4.12.5.0</FileVersion>
    <Platforms>x86</Platforms>
    <UseWPF>true</UseWPF>
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="..\Bar.csproj">
      <!-- ... -->
    </ProjectReference>
  </ItemGroup>
  <ItemGroup Label="FilesToCopy">
    <Content Include="..\dependent.dll" Pack="true" PackagePath=".\lib\net48\" PackageCopyToOutput="true" />
    <None Include="image.jpg" Pack="true" PackagePath=".\contentFiles\any\any\" PackageCopyToOutput="true" />
  </ItemGroup>
  <ItemGroup>
    <None Update="tex_cardboard.jpg">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>
  <PropertyGroup>
    <TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
  </PropertyGroup>
  <Target Name="CopyProjectReferencesToPackage" DependsOnTargets="ResolveReferences">
    <ItemGroup>
      <BuildOutputInPackage Include="@(ReferenceCopyLocalPaths-&gt;WithMetadataValue('ReferenceSourceTarget', 'ProjectReference'))" />
    </ItemGroup>
  </Target>
</Project>

这将在 contentFiles\any\any 文件夹中生成带有 image.jpg 的 nuget 包。

如果我理解正确的话,nuget包中的.nuspec文件应该包含:

    <contentFiles>
      <files include="**/*" buildAction="Content" />
    </contentFiles>

但事实并非如此。

如何在 csproj 文件中告诉图像文件作为内容包含而不被编译?

是的,您的包中的 NuSpec 文件将包含 contentFiles 标签 ,除非您 在项目文件中为您的资源定义自定义 PackagePath。它猜测此行为适用于 .csproj 个文件,因为内容文件的默认路径已被该文件覆盖,并且不再被检测为适用于 NuGet 约定的内容文件。来自 .csproj reference and MS Build targets:

This property specifies the default location of where all the content files should go if PackagePath is not specified for them. The default value is "content;contentFiles".

By default, everything gets added to the root of the content and contentFiles\any<target_framework> folder within a package and preserves the relative folder structure, unless you specify a package path [...]

虽然明确指出您可以在具有不同构建操作的许多内容类型上使用 PackagePath,但没有提到这会产生不同的 NuSpec 文件,但实际上确实如此。

Apart from Content items, the and metadata can also be set on files with a build action of Compile, EmbeddedResource, ApplicationDefinition, Page, Resource, SplashScreen, DesignData, DesignDataWithDesignTimeCreateableTypes, CodeAnalysisDictionary, AndroidAsset, AndroidResource, BundleResource or None. [...]

在您的 csproj 文件中,NoneContent 等标签决定了构建操作,因此决定了您的图像文件是嵌入、复制到输出文件夹还是其他任何内容。但是,除了原始问题之外,设置 Pack="true" 足以自动将其包含在包中的 contenFiles\any\<Target framework moniker> 文件夹中。设置 PackageCopyToOutput="true" 会将其复制到使用项目的输出目录。