Tedford
Tedford

Reputation: 2932

Force project references to be included in netstandard nuget package

I have a netstandard project which includes two project references. Visual studio 2017 is being used to build the nukpg. When the project is built the produced nupkg only contains the assembly produced by that project and lists the two project references as nuget dependencies. Is there a way to force the packaging to include those assemblies as lib files?

csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net462</TargetFramework>
    <RootNamespace>Verifier.Observations.DevOps.Health</RootNamespace>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    <VersionPrefix>1.0.1</VersionPrefix>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\Verifier.Observations.Aspects\Verifier.Observations.Aspects.csproj" />
    <ProjectReference Include="..\Verifier.Observations\Verifier.Observations.csproj" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="System.ComponentModel.Composition"/>
    <Reference Include="System.Net.Http" />
  </ItemGroup>

</Project>

enter image description here

Update Based upon feedback from @alexgiondea-msft the package is now created as desired using the following

csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <VersionPrefix>1.0.1</VersionPrefix>
    <TargetFramework>net462</TargetFramework>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    <NuspecFile>Verifier.Observations.DevOps.Health.Nuspec</NuspecFile>
   <NuspecProperties>version=$(VersionPrefix);id=$(MSBuildProjectName);author=$(Authors);copy=$(Copyright);iconUrl=$(PackageIconUrl)</NuspecProperties>
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="..\Verifier.Observations.Aspects\Verifier.Observations.Aspects.csproj" />
    <ProjectReference Include="..\Verifier.Observations\Verifier.Observations.csproj" />
  </ItemGroup>
  <ItemGroup>
    <Reference Include="System.ComponentModel.Composition" />
    <Reference Include="System.Net.Http" />
  </ItemGroup>
</Project>

nuspec

<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>$author$</authors>
    <owners>$author$</owners>
    <iconUrl>$iconUrl$</iconUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Inspect automation service to ensure it is up and operational</description>
    <releaseNotes></releaseNotes>
    <copyright>$copy$</copyright>
    <tags>verifier-observation-plugin automation</tags>
    <dependencies>
      <group targetFramework="net462" />
    </dependencies>
    <references>
      <group targetFramework="net462">
        <reference file="Verifier.Observations.DevOps.Automation.dll" />
      </group>
    </references>
  </metadata>
  <files>
    <file src="bin\*\net462\*.dll" target="lib\net462" />
    <file src="bin\*\net462\*.pdb" target="lib\net462" />
  </files>
</package>

Upvotes: 9

Views: 4074

Answers (2)

Matheus Camargo
Matheus Camargo

Reputation: 103

You can add the following target to your .csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>netstandard2.0;net47</TargetFrameworks>
    <TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\ClassLibrary2\ClassLibrary2.csproj" PrivateAssets="all" />
    <ProjectReference Include="..\ClassLibrary3\ClassLibrary3.csproj" Condition="'$(TargetFramework)' == 'net47'" PrivateAssets="all" />
  </ItemGroup>

  <Target Name="CopyProjectReferencesToPackage" DependsOnTargets="ResolveReferences">
    <ItemGroup>
      <BuildOutputInPackage Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference'))" />
    </ItemGroup>
  </Target>
</Project>

Source 1

Source 2

Reference: Advanced extension points to create customized package

Upvotes: 7

Alex Ghiondea - MSFT
Alex Ghiondea - MSFT

Reputation: 3380

You can control where assemblies are deployed in the nuget package using an item in an itemgroup, similar to this:

<ItemGroup>
    <None Include="!!path_to_assembly!!">
        <PackagePath>lib\net462</PackagePath>
        <Pack>true</Pack>
        <Visible>false</Visible>
    </None>
</ItemGroup>

That should include the specified assembly in the package.

Upvotes: 8

Related Questions