Hello Hello everyone, I'm TANZAME, we met again.
NuGet will not repeat here what is long-winded, the garden a search a lot. To share with everyone today is how the unified management of our bags in the daily development process, how to build a package via batch scripts and automatically upload it to NuGet. In the actual project development process, we want to upload your own package, the general steps are: nuget spec => nuget pack => nuget push, a package must be repeated at least three actions, if there are N packet would have to be repeated N * 3 times, think not able to, so there will be some sharing topic today.
- Build directory
Since it is a unified management, resulting package is naturally placed in the same folder, rather than scattered in various .proj directory. Here we create a directory in the directory solution, the aim is to facilitate the bat script to find solutions to the following sub-projects. For example, I'm new here is .nuget this directory should be noted that if the directory names with special characters, then can not directly create a new right, you need to use the command prompt, direct use shortcut keys in the directory where the solution SHIFT + Right will be able to directly open command prompt, this operation may be omitted cd lot.
- Download NuGet
NuGet official website to download the command-line interface (CLI). nuget.exe
It provides a complete nuget function can be used to install, create, publish and manage packages, without having to make any changes to the project file.
- Visit nuget.org/downloads , and select NuGet version 3.3 or higher (2.8.6 is not compatible with Mono). Always recommend using the latest version. To publish packages to nuget.org, must be at least version 4.1.0.
- Each download directly download
nuget.exe
the file. Let browser to save the file to the selected folder. This file is not the installer; if run directly in the browser, you will not see anything. - Add the folder to the
nuget.exe
location to place the PATH environment variable, so you can use the CLI tool from anywhere. Here we put it on the new step .nuget folder, and set the PATH environment variable.
- Generated list
.nuspec file that contains a list of XML metadata package, this package is used to generate the list at the same time and provide the user with information. The list of documents that we need to generate only once, do not need to be regenerated later. .net Core sdk characteristics and use of .NET Standard does not require .nuspec project file, if it is .net Core sdk characteristics and use .NET Standard Project This step is ignored. Go to the project directory, the SHIFT + right to bring up the command prompt, enter nuget spec command to generate a list of packet meta data we need.
.nuget file will cut the list to a file the first step in the new folder, cut past the project the following will not be nothing out of a file, looked much cleaner. Then do some adjustments fill in the relevant information of our own projects, like this:
<?xml version="1.0" encoding="utf-8"?> <package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd"> <metadata> <id>TZM.XFramework</id> <version>$version$</version> <title>$title$</title> <authors>$author$</authors> <owners>$author$</owners> <license type="expression">Apache-2.0</license> <projectUrl>https://github.com/TANZAME/TZM.XFramework</projectUrl> <iconUrl>http://go.microsoft.com/fwlink/?LinkID=386613</iconUrl> <description>TZM.XFramework is a lightweight and high performance object-relational mapper for .NET use the original Entity Framework api.</description> <copyright>Copyright 2019</copyright> <tags>.NET SqlServer MSSQL Database Data O/RM ADO.NET</tags> <repository type="git" url="https://github.com/TANZAME/TZM.XFramework" /> <dependencies /> <frameworkAssemblies> <frameworkAssembly assemblyName="System.Data"/> <frameworkAssembly assemblyName="System.ComponentModel.DataAnnotations"/> <frameworkAssembly assemblyName="System.Net.Http"/> </frameworkAssemblies> </metadata> </package>
- Scripting
The first step in the new folder create a bat file, rename package.bat, followed by the preparation of our automated scripts. Full bat script stamp Here , directly on the code snippet.
- Here I set nuget pack package property to Release, and not automatically generated, so you need to run scripts compiled in Release mode. -Build parameters, then add too much information see output thief uncomfortable, remove it here, we have to manually compile.
- Filling api_key. Go nuget official website log in to your account and create a key, copy and paste into api_key variables.
- Note .net framework program (fx) and use the command .net core project is not the same
- So far all our preparatory steps have been completed, double-click package.bat run the script, hands free.
@echo off set api_key=xxxxxxlef2j57rw4q26qcrvycvznyvcurgfxbzxxxxxxxx set source_api_uri=https://api.nuget.org/v3/index.json set startup_dir=%~dp0 cd ..\ set startup_dir=%cd% cd .nuget :: 打包 TZM.XFramework -Build echo pack TZM.XFramework copy TZM.XFramework.nuspec %startup_dir%\net45\TZM.XFramework nuget pack %startup_dir%\net45\TZM.XFramework\TZM.XFramework.csproj -Properties Configuration=Release del %startup_dir%\net45\TZM.XFramework\TZM.XFramework.nuspec echo= :: 打包 TZM.XFrameworkCore echo pack TZM.XFrameworkCore dotnet pack --no-build --configuration Release --output %startup_dir%\.nuget\ %startup_dir%\netcore\TZM.XFrameworkCore\TZM.XFrameworkCore.csproj :: 批量推送包 for /R %cd% %%f in (*.nupkg) do ( echo= dotnet nuget push %%f -k %api_key% -s %source_api_uri% ) echo= pause
Finally renderings posted a final run:
- to sum up
With this script, we can in a folder unified management of our package, so that a key is generated, while uploading project files to keep cool, just do not be too convenient ~ ah ~ ..
Reference: https://docs.microsoft.com/zh-cn/nuget/
Technical exchange group: 816 425 449