使用GeneratePackageOnBuild和contentFiles构建dotnet

时间:2020-07-07 10:52:44

标签: .net nuget nuget-package

我有一个要与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>

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

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

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

但事实并非如此。

我如何在csproj文件中分辨出图像文件将作为内容包括在内,而不是要编译?

1 个答案:

答案 0 :(得分:1)

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

如果未为它们指定PackagePath,则此属性指定所有内容文件应到达的默认位置。默认值为“ content; contentFiles”。

默认情况下,所有内容都会添加到包中content和contentFiles \ any 文件夹的根目录中,并保留相对文件夹结构,除非您指定包路径[...]

尽管明确指出您可以在具有不同构建操作的许多内容类型上使用PackagePath,但没有提及这会导致生成不同的NuSpec文件,但实际上可以。

>

除了Content项目外,还可以在文件上设置以下内容:和和元数据,其构建动作为Compile,EmbeddedResource,ApplicationDefinition,Page,Resource,SplashScreen,DesignData,DesignDataWithDesignTimeCreateableTypes,CodeAnalysisDictionary,AndroidAsset,AndroidResource,BundleResource或无。 [...]

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

相关问题