Geert van Horrik
Geert van Horrik

Reputation: 5724

MSBuild well-known item metadata and NuGet pack

I am trying to add custom files to a specific NuGet package (basically I need all output files included in the NuGet package since it serves as a tool for Chocolatey).

After some searching, I found this potential fix:

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

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

Unfortunately, this won't work correctly for subdirectories, so I tried this:

  <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>

According to the docs, I should be able to use %(FullPath), but I am getting this error:

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]

Any idea why the well-known items don't seem to work in this scenario?

Upvotes: 0

Views: 260

Answers (1)

Geert van Horrik
Geert van Horrik

Reputation: 5724

Got a fix by specifying the item group outside the target and then using that instead.

  <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>

Upvotes: 1

Related Questions