首页 > 解决方案 > MSBuild 知名项元数据和 NuGet 包

问题描述

我正在尝试将自定义文件添加到特定的 NuGet 包(基本上我需要 NuGet 包中包含的所有输出文件,因为它用作 Chocolatey 的工具)。

经过一番搜索,我发现了这个潜在的修复:

  <PropertyGroup>
    <TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);GetToolsPackageFiles</TargetsForTfmSpecificBuildOutput>
  </PropertyGroup>

  <Target Name="GetToolsPackageFiles">
    <ItemGroup>
      <BuildOutputInPackage Include="$(OutputPath)\**\*.dll" />
      <BuildOutputInPackage Include="$(OutputPath)\**\*.exe" />
    </ItemGroup>
  </Target>

不幸的是,这不适用于子目录,所以我尝试了这个:

  <PropertyGroup>
    <TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);GetToolsPackageFiles</TargetsForTfmSpecificBuildOutput>
  </PropertyGroup>

  <Target Name="GetToolsPackageFiles">
    <ItemGroup>
      <BuildOutputInPackage Include="$(OutputPath)\**\*.dll">
          <TargetPath>$([MSBuild]::MakeRelative('$(OutputPath)', %(FullPath)))</TargetPath>
      </BuildOutputInPackage>
      <BuildOutputInPackage Include="$(OutputPath)\**\*.exe">
          <TargetPath>$([MSBuild]::MakeRelative('$(OutputPath)', %(FullPath)))</TargetPath>
      </BuildOutputInPackage>
    </ItemGroup>
  </Target>

根据文档,我应该能够使用 %(FullPath),但是我收到了这个错误:

error MSB4184: The expression "[MSBuild]::MakeRelative(C:\Sour
ce\RepositoryCleaner\output\Release\RepositoryCleaner\netcoreapp3.1\, '')" cannot be evaluated. Parameter "path" cannot have zero length.
 [C:\Source\RepositoryCleaner\src\RepositoryCleaner\RepositoryCleaner.csproj]

知道为什么众所周知的项目在这种情况下似乎不起作用吗?

标签: msbuildnuget

解决方案


通过在目标之外指定项目组然后使用它来解决问题。

  <PropertyGroup>
    <TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);GetToolsPackageFiles</TargetsForTfmSpecificBuildOutput>
  </PropertyGroup>

  <ItemGroup>
    <ToolDllFiles Include="$(OutputPath)\**\*.dll" />
    <ToolExeFiles Include="$(OutputPath)\**\*.exe" />
  </ItemGroup>

  <Target Name="GetToolsPackageFiles">
    <ItemGroup>
      <BuildOutputInPackage Include="@(ToolDllFiles)">
          <TargetPath>$([MSBuild]::MakeRelative('$(OutputPath)', %(ToolDllFiles.FullPath)))</TargetPath>
      </BuildOutputInPackage>
      <BuildOutputInPackage Include="@(ToolExeFiles)">
          <TargetPath>$([MSBuild]::MakeRelative('$(OutputPath)', %(ToolExeFiles.FullPath)))</TargetPath>
      </BuildOutputInPackage>
    </ItemGroup>
  </Target>

推荐阅读