强制将项目引用包含在netstandard nuget包中

时间:2018-07-11 14:46:19

标签: visual-studio-2017 nuget-package .net-standard

我有一个netstandard项目,其中包括两个项目引用。 Visual Studio 2017正在用于构建nukpg。构建项目时,生成的nupkg仅包含该项目生成的程序集,并将两个项目引用列为nuget依赖项。有没有一种方法可以强制包装将那些程序集包含为lib文件?

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

更新 现在,根据来自@ alexgiondea-msft的反馈,使用以下命令按需创建该程序包

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>

2 个答案:

答案 0 :(得分:5)

您可以使用项目组中的项目控制程序集在nuget软件包中的部署位置,类似于:

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

该软件包中应包含指定的程序集。

答案 1 :(得分:1)

您可以将以下目标添加到.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

参考: Advanced extension points to create customized package