How do you pack a .NET Standard NuGet package with

2019-08-24 10:24发布

How do you pack a .NET Standard NuGet package that has its Nuget specifications in the CSProj file without rebuilding it?

I have some automation that needs to run between the build-step and the pack-step that is de-coupled from the build-process itself and shouldn't be a build-event tied to any projects.

When I try to use the nuget CLI, it fails with this:

Error NU5012: Unable to find 'bin\Debug\LibraryNuGetExample\bin\Debug\'. Make sure the project has been built.

This makes no sense as that's not the build output folder! The correct output folder is bin\Debug\** -- I don't understand why it's looking for this directory mapping that I didn't specify anywhere.

I tried using this, but it rebuilt, which I definitely don't want; just nuget pack:

MSBuild LibraryNuGetExample.csproj /t:pack

So I either need to know how to,

  1. Use MSBuid to only pack and not build the package with some currently unknown options
  2. Use the NuGet CLI with currently unknown options to pack it
  3. Some other clever thing(s) I haven't yet considered :)

LibraryNuGetExample.csproj

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

  <PropertyGroup>
    <TargetFrameworks>uap10.0;net45</TargetFrameworks>
    <PackageId>LibraryForNuGetExample</PackageId>
    <Authors>user name</Authors>
    <Company>ACME</Company>
    <Product>Library For NuGet Example</Product>
    <Description>A test package to test automation.</Description>
    <Copyright>ACME © 2018</Copyright>
    <PackageTags>DevOps Builds Testing</PackageTags>
    <PackageVersion>$(AssemblyVersion)</PackageVersion>
    <Platforms>x64;AnyCPU</Platforms>
    <NeutralLanguage>en-US</NeutralLanguage>
  </PropertyGroup>

  <PropertyGroup Condition="'$(TargetFramework)' == 'uap10.0'">
    <NugetTargetMoniker>UAP,Version=v10.0</NugetTargetMoniker>
    <TargetPlatformIdentifier>UAP</TargetPlatformIdentifier>
    <TargetPlatformVersion>10.0.15063.0</TargetPlatformVersion>
    <TargetPlatformMinVersion>10.0.15063.0</TargetPlatformMinVersion>
    <TargetFrameworkIdentifier>.NETCore</TargetFrameworkIdentifier>
    <TargetFrameworkVersion>v5.0</TargetFrameworkVersion>
    <DefineConstants>$(DefineConstants);WINDOWS_UWP</DefineConstants>
    <LanguageTargets>$(MSBuildExtensionsPath)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets</LanguageTargets>
  </PropertyGroup>

   <ItemGroup>
    <None Include="LibraryForNuGetExample.targets" Pack="true" PackagePath="build\uap10.0" />
  </ItemGroup>

  <ItemGroup Condition=" '$(TargetFramework)' == 'uap10.0' ">
    <PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform " Version="5.2.2" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="Windows">
      <HintPath>C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.15063.0\Windows.winmd</HintPath>
      <IsWinMDFile>true</IsWinMDFile>
      <Private>true</Private>
    </Reference>
  </ItemGroup>

  <Target Name="CopyPackage" AfterTargets="Pack" Condition="'$(IsCrossTargetingBuild)' == 'true'">
    <Copy SourceFiles="$(OutputPath)$(PackageId).$(PackageVersion).nupkg" DestinationFolder="$(SolutionDir)Packages" />
  </Target>

</Project>

LibraryForNuGetExample.targets

<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'UAP'">
    <ReferenceCopyLocalPaths>
        Include="$(MSBuildThisFileDirectory)bin\Release\uap10.0\LibraryNuGetExample.dll"
        Include="$(MSBuildThisFileDirectory)bin\Release\uap10.0\LibraryNuGetExample.pdb"
        Include="$(MSBuildThisFileDirectory)bin\Release\uap10.0\LibraryNuGetExample.pri"
    </ReferenceCopyLocalPaths>
  </ItemGroup>
  <ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'NET45'">
    <ReferenceCopyLocalPaths>
        Include="$(MSBuildThisFileDirectory)bin\Release\net45\LibraryNuGetExample.dll"
        Include="$(MSBuildThisFileDirectory)bin\Release\net45\LibraryNuGetExample.pdb"
        Include="$(MSBuildThisFileDirectory)bin\Release\net45\LibraryNuGetExample.pri"
    </ReferenceCopyLocalPaths>
  </ItemGroup>
</Project>

1条回答
\"骚年 ilove
2楼-- · 2019-08-24 10:39

You can use the equivalent of

dotnet pack --no-build

which for MSBuild (since you're building for UAP) is

msbuild -t:Pack -p:NoBuild=true
查看更多
登录 后发表回答