다음을 통해 공유


Visual Studio: Create a NuGet package

This article explains the details of creating a NuGet package in a Visual Studio solution. Create a new project in VS and add the following two files:

  1. *.targets file: when a release build is triggered in the VS solution. This file will get the nuspec file to create and push the NuGet package to the NuGet store
  2. *.nuspec file: This file contains the metadata for creating the NuGet package including the assemblies that needed to be included in the <file></file> tag.

Create a *.targets file as shown below with the path of the nuspec file along with the commands for creating and pushing the nuget package to the nuget store.

?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="AfterBuild" Condition="'$(Configuration)' == 'Release' ">
    <ItemGroup>
      <NuspecFiles Include="$(TargetDir)**\*.nuspec" />
    </ItemGroup>
    <!-- Pack and publish -->
    <Exec Command="$(SolutionDir).nuget\nuget.exe pack "%(NuspecFiles.Identity)""
          WorkingDirectory="$(TargetDir)" />
    <ItemGroup>
      <NupkgFiles Include="$(TargetDir)**\*.nupkg" />
    </ItemGroup>
    <Exec Command="$(SolutionDir).nuget\nuget.exe push "%(NupkgFiles.Identity)" -s "NugetSource" ""APIKEY""
          WorkingDirectory="$(TargetDir)" />
  </Target>
</Project>

Create a nuspec file as follows:

<?xml version="1.0"?>
<package >
  <metadata>
    <id></id>
    <version></version>
    <title></title>
    <authors></authors>
    <owners></owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description></description>
    <releaseNotes></releaseNotes>
    <copyright></copyright>
    <tags></tags>
    <dependencies>
      <dependency id="" version="" />
    </dependencies>
  </metadata>
  <files>
    <file src="" target="" />
  </files>
</package>

Now, when a release build is triggered in the VS solution, a neNuGetet package is created and pushed to the NuGet store.