Skip to content

Need ability to pack files from build output folder in multi-targeting projects #4704

Closed

Description

@natemcmaster

It is difficult for users to figure out how to include additional items from the build output folder in a nuget package. The right solution requires adding a target that executes at just the right time, after _CopyFilesMarkedCopyLocal and before _GetPackageFiles. This is made more difficult because _GetPackageFiles only executes on outer builds, not inner builds. It also requires understandings of internal workings of the pack targets.

Scenario: packing a target that includes both the build output.

Ideally, it would be nice if I could express something like this my csproj:

<Target Name="GetAdditionalPackageFiles" BeforeTargets="GetPackageFiles" Condition="'$(TargetFramework)' != ''">
   <ItemGroup>
      <Content Include="$(OutDir)*.generated.on.build.txt" PackagePath="content\$(TargetFramework)\" />
    </ItemGroup>
</Target>

Workaround
Manually add items to the _PackageFiles group in the outer build before the GenerateNuspec task. Make sure to set BuildAction metadata because PackTask throws if it is not set.

Activity

  1. natemcmaster commented on Mar 23, 2017

    @natemcmaster
    Author

    So, if I understand NuGet/NuGet.Client#1255, the recommend approach now is this:

    <PropertyGroup>
      <TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);GetMyPackageFiles</TargetsForTfmSpecificBuildOutput>
    </PropertyGroup>
    
    <Target Name="GetMyPackageFiles">
      <ItemGroup>
        <BuildOutputInPackage Include="$(OutDir)*.generated.on.build.txt" />
      </ItemGroup>
    </Target>

    Right @rohit21agrawal ?

  2. rohit21agrawal commented on Mar 23, 2017

    @rohit21agrawal
    Contributor

    @natemcmaster correct, except that you also need to set FinalOutputPath metadata which will be the absolute path of this file on disk.
    The result would be that this will put it in lib\TargetFramework folder.

    If there is something you want to put outside of the lib folder, use the TargetsForTfmSpecificContentInPackage to write the appropriate target and set the PackagePath.

    Feel free to message me for any issues.

  3. natemcmaster commented on Mar 23, 2017

    @natemcmaster
    Author

    Thanks!

  4. SimonCropp commented on Jul 12, 2017

    @SimonCropp

    @rohit21agrawal can u share an example of how to set FinalOutputPath metadata using the example given by @natemcmaster above?

  5. rohit21agrawal commented on Jul 17, 2017

    @rohit21agrawal
    Contributor

    @SimonCropp Here you go. Let me know if this is still not clear. Sorry for the delay in reply.

    <PropertyGroup>
      <TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);GetMyPackageFiles</TargetsForTfmSpecificBuildOutput>
    </PropertyGroup>
    
    <Target Name="GetMyPackageFiles">
      <ItemGroup>
        <BuildOutputInPackage Include="$(OutDir)\generated.on.build.txt" >
           <FinalOutputPath>full-path-goes-here</FinalOutputPath>
        </BuildOutputInPackage>
      </ItemGroup>
    </Target>
    
  6. bording commented on Jul 17, 2017

    @bording

    @rohit21agrawal Can you show a working example that has a real value for FinalOutputPath? I'm trying to adapt https://github.com/Particular/NServiceBus/blob/develop/src/Directory.Build.targets#L17 to use TargetsForTfmSpecificBuildOutput instead, but I'm not seeing the PDBs show up in the package.

    Here's what I'm trying that isn't working:

     <PropertyGroup>
        <TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);IncludePDBsInPackage</TargetsForTfmSpecificBuildOutput>
      </PropertyGroup>
    
      <Target Name="IncludePDBsInPackage" Condition="'$(IncludeBuildOutput)' != 'false'">
        <ItemGroup>
          <BuildOutputInPackage Include="$(OutDir)\$(AssemblyName).pdb">
            <FinalOutputPath>$([System.IO.Path]::GetFullPath('$(OutDir)\$(AssemblyName).pdb'))</FinalOutputPath>
          </BuildOutputInPackage>
        </ItemGroup>
      </Target>
    
  7. rohit21agrawal commented on Jul 17, 2017

    @rohit21agrawal
    Contributor

    @bording couple things to note:

    1. You can't use TargetsForTfmSpecificBuildOutput to add pdb files for your package (the non-symbols package) because we explicitly filter by extension types on this item group.
    2. You are doing the write thing in your targets above, but it won't work for pdb files for the reason i mentioned above.
    3. To add pdb files to your package, use the TargetsForTfmSpecificContentInPackage property and write a target that looks something like this:

    Let me know if you have anymore questions.

    <Target Name=""CustomContentTarget"">
        <ItemGroup>
          <TfmSpecificPackageFile Include=""abc.txt"">
            <PackagePath>mycontent/$(TargetFramework)</PackagePath>
          </TfmSpecificPackageFile>
          <TfmSpecificPackageFile Include=""Extensions/ext.txt"" Condition=""'$(TargetFramework)' == 'net46'"">
            <PackagePath>net46content</PackagePath>
          </TfmSpecificPackageFile>  
        </ItemGroup>
      </Target>
    
  8. natemcmaster commented on Jul 17, 2017

    @natemcmaster
    Author

    @rohit21agrawal don't we also need to chain custom targets to something? What's your recommendation? GenerateNuspecDependsOn or Before/AfterTargets

  9. rohit21agrawal commented on Jul 17, 2017

    @rohit21agrawal
    Contributor

    no, that is already taken care of by pack targets. the targets defined in the properties TargetsForTfmSpecificContentInPackage and TargetsForTfmSpecificBuildOutput are run before pack in the tfm-specific build.

  10. bording commented on Jul 17, 2017

    @bording

    @rohit21agrawal Thanks, using TargetsForTfmSpecificContentInPackage instead seems to be working fine. I think I like using that one better anyway since it doesn't require the absolute path of the file.

    Out of curiosity, what's the reason for the filter on TargetsForTfmSpecificBuildOutput? What values does it let through?

  11. rohit21agrawal commented on Jul 18, 2017

    @rohit21agrawal
    Contributor

    @bording here is the list of output extensions it allows : https://github.com/NuGet/NuGet.Client/blob/dev/src/NuGet.Core/NuGet.Commands/MSBuildProjectFactory.cs#L27

    there are two lists , one for the normal package and one for the symbols package.

    we are open to listening to user feedback if this comes in the way of your scenario - please feel free to open up an issue .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions