Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 844
Tedford Avatar asked Jul 11 '18 14:07

Tedford


People also ask

How do I add a reference to a NuGet package?

After you install a NuGet package, you can then make a reference to it in your code with the using <namespace> statement, where <namespace> is the name of package you're using. After you've made a reference, you can then call the package through its API.

Does NuGet package include dependencies?

Any time a package is installed or reinstalled, which includes being installed as part of a restore process, NuGet also installs any additional packages on which that first package depends. Those immediate dependencies might then also have dependencies on their own, which can continue to an arbitrary depth.

Where are NuGet packages references stored?

The location of the default global packages folder. The default is %userprofile%\. nuget\packages (Windows) or ~/.

What is package reference in Csproj?

Package references, using <PackageReference> MSBuild items, specify NuGet package dependencies directly within project files, as opposed to having a separate packages. config file. Use of PackageReference doesn't affect other aspects of NuGet; for example, settings in NuGet.


1 Answers

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

like image 66
Matheus Camargo Avatar answered Oct 18 '22 23:10

Matheus Camargo