Monday, July 31, 2023

Create nuget package


Nuget package is compressed file with extension *.nupkg extension and this package can be shared to other teams.

we can create nuget packages using nuget cli or dotnet cli 

#### Prerequisites

Install [Nuget.exe](https://dist.nuget.org/win-x86-commandline/latest/nuget.exe) CLI 

dotnet.exe with [DotNet SDK](https://www.microsoft.com/net/download/)


#### 1. Generate Manifest (.nuspec) 

we can generate initial .nuspec file from project 
```sh 
# we can generate .nuspec file from project file
	nuget spec Logger.csproj

#we can generate .nuspec file from project dll as well 
	nuget spec .dll

```

Note: NuGet throws  error if you try to create a package with default values in .nuspec file, so you must change the following fields before proceeding.

 - licenseUrl
 - projectUrl
 - iconUrl
 - releaseNotes
 - tags

#### 2.1 Create Package using nuget cli

```sh
# generate package by running nuget pack  where cs project located
	nuget  pack 

# generate package by specifying project location and configuration 
	nuget pack .\Logger\Logger.csproj -properties Configuration=Release

# generate package for specifying project and configuration
nuget pack Logger.csproj -properties Configuration=Release

# generate package for specifying project after building project with build configuration release and include referenced projected if project is using external project references
nuget pack Logger.csproj -Build -properties Configuration=Release  -IncludeReferencedProjects


# Create a package from Logger.csproj, using MSBuild version 12 to build the project and package properties (owner and version)
nuget pack Logger.csproj -Build -Symbols -MSBuildVersion 12 -Properties owners=scott,rob;version="1.0.0"

#create a package using nuspec file and exclude exe and bat files 

nuget pack logger.nuspec -exclude "*.exe" -exclude "*.bat"
```

#### 2.2 Create Package using dotnet cli

```sh
# generate package by running nuget pack  where cs project located
	dotnet pack

# generate package by specifying project location and configuration 
	dotnet pack .\Logger\Logger.csproj -properties Configuration=Release

# generate package for specifying project and configuration
	dotnet pack Logger.csproj -properties Configuration=Release

# generate package for specifying project after building project with build configuration release and include referenced projected if project is using external project references
dotnet pack Logger.csproj -Build -properties Configuration=Release  -IncludeReferencedProjects


# Create a package from Logger.csproj, using MSBuild version 12 to build the project and package properties (owner and version)
dotnet pack Logger.csproj -Build -Symbols -MSBuildVersion 12 -Properties owners=scott,rob;version="1.0.0"

#create a package using nuspec file and exclude exe and bat files 

dotnet pack logger.nuspec -exclude "*.exe" -exclude "*.bat"
```

#### 3. Publish Package in nuget repository 

Once .nupkg file is created , you can publish to [nuget repository](https://nuget.org)

 - Sign into [nuget.org](https://nuget.org) account or create an account if you don't have one already.
 - Click on your username which is on the upper right, and select API Keys and then on webpage click on Create.

```sh 
nuget push Logger.1.0.0.nupkg oz5fgepyspx6fzm67guqybyr8vanjboudmner4e1gsy24b -Source https://api.nuget.org/v3/index.json

#push package using dotnet cli to the default push source specified in the NuGet config file 
dotnet nuget push foo.nupkg -k 3003d786-dd37-6004-efef-d4f3e8ef9b3a

#push package using dotnet cli to nuget repository 
dotnet nuget push foo.nupkg -k 6003d786-cc36-6004-efef-d4d3e8ef9b3a -s https://api.nuget.org/v3/index.json
```
#### 3. Publish Package in Nexus repository 

Once .nupkg file is created , if want to push a NuGet package to a Nexus repository, 

we can use the nuget push command. This command requires to use the NuGet API Key and the URL of the target hosted repository 

For example, if your Nexus server is located at http://nexus_server and your repository is named nexus_nuget_repository

```sh
	nuget push [logger.nupkg] -Source http://nexus_server/repository/nexus_nuget_repository/ -ApiKey [api_key] 

#push package using dotnet cli to nuget repository 
	dotnet nuget push logger.nupkg -k 6003d786-cc36-6004-efef-d4d3e8ef9b3a -s http://nexus_server/repository/nexus_nuget_repository/

```


No comments:

Post a Comment