マイクロソフト系技術情報 Wiki」は、「Open棟梁Project」,「OSSコンソーシアム .NET開発基盤部会」によって運営されています。

目次

概要

NuGetパッケージを作成してみたくなったので調査。

Nugetでできること。

使っていれば、だいたい解っていると思うけど、

基本的な手順

Nugetのサイトにサインイン

NugetのサイトからNuget.exeを取得

[Install NuGet]
latest nuget.exe - all downloads - documentation

nuspecファイルの生成

nuspecファイルとは?

nuspecファイルの出力準備

nuget.exeからnuspecファイルを出力

nuspecファイルの編集

以下、ザックリとした説明。

id

例 : Oracle.DataAccess?(.dll)とか、Microsoft.AspNet?.Identity(.dll)とか。

title

description

パッケージのザックリとした説明。

authors、owners

projectUrl、licenseUrl、iconUrl

各種URLを設定する。

version

.NET Framework アセンブリへの参照の追加

<frameworkAssemblies> 
  <frameworkAssembly assemblyName="System.Web" /> 
</frameworkAssemblies>

他のNugetパッケージへの依存関係の追加

<dependencies> 
  <dependency id="jQuery" /> 
</dependencies>

バージョンを指定可能。範囲指定も可能。

アセンブリの参照の追加(*.dll)

<files> 
  <file src="bin\Release\*.*" target="lib\net45" /> 
</files>

参考

https://docs.microsoft.com/ja-jp/nuget/create-packages/creating-a-package

<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
  <metadata>
    <!-- The identifier that must be unique within the hosting gallery -->
    <!-- ホスティングギャラリー内で一意でなければならないID -->
    <id>Contoso.Utility.UsefulStuff</id>

    <!-- The package version number that is used when resolving dependencies -->
    <!-- 依存関係を解決するときに使用されるパッケージのバージョン番号 -->
    <version>1.8.3.331</version>

    <!-- Authors contain text that appears directly on the gallery -->
    <!-- Authorsには、ギャラリーに直接表示されるテキストが含まれています -->
    <authors>Dejana Tesic, Rajeev Dey</authors>

    <!-- Owners are typically nuget.org identities that allow gallery users to easily find other packages by the same owners.  -->
    <!-- ownersは通常、ギャラリーのユーザーが同じownerによる他のパッケージを簡単に見つけることを可能にするnuget.org IDです。 -->
    <owners>dejanatc, rjdey</owners>

    <!-- License and project URLs provide links for the gallery -->
    <!-- licenseUrlとprojectUrlはギャラリーにリンクを提供します -->
    <licenseUrl>http://opensource.org/licenses/MS-PL</licenseUrl>
    <projectUrl>http://github.com/contoso/UsefulStuff</projectUrl>

    <!-- The icon is used in Visual Studio's package manager UI -->
    <!-- このアイコンは、Visual StudioのパッケージマネージャUIで使用されます -->
    <iconUrl>http://github.com/contoso/UsefulStuff/nuget_icon.png</iconUrl>

    <!-- If true, this value prompts the user to accept the license when installing the package. -->
    <!-- trueの場合、この値は、パッケージのインストール時にライセンスを受け入れるようにユーザーに指示します。 -->
    <requireLicenseAcceptance>false</requireLicenseAcceptance>

    <!-- Any details about this particular release -->
    <!-- このリリースに関する詳細 -->
    <releaseNotes>Bug fixes and performance improvements</releaseNotes>

    <!-- The description can be used in package manager UI. Note that the nuget.org gallery uses information you add in the portal. -->
    <!-- descriptionは、パッケージマネージャのUIで使用できます。 nuget.orgギャラリーでは、ポータルに追加した情報が使用されます。 -->
    <description>Core utility functions for web applications</description>

    <!-- Copyright information -->
    <!-- 著作権情報 -->
    <copyright>Copyright ©2016 Contoso Corporation</copyright>

    <!-- Tags appear in the gallery and can be used for tag searches -->
    <!-- tagsはギャラリーに表示され、tag検索に使用できます -->
    <tags>web utility http json url parsing</tags>

    <!-- Dependencies are automatically installed when the package is installed -->
    <!-- dependencyは、パッケージのインストール時に自動的に(他のパッケージが)インストールされます。 -->
    <dependencies>
      <dependency id="Newtonsoft.Json" version="9.0" />
    </dependencies>
  </metadata>

  <!-- A readme.txt will be displayed when the package is installed -->
  <!-- パッケージのインストール時に readme.txt が表示されます -->
  <files>
    <file src="readme.txt" target="" />
  </files>
</package>

パッケージの作成

プロジェクト・ファイルを使用

nuspecファイルを使用

NuGet への登録

コマンドラインから

Webサイトから

NuGet Gallery でアップロードする場合、API Keysは不要。

公開前にテスト実行する。

バージョンアップする。

nuspec ファイルの version を書き換えて同じように nupkg をアップロード。

高度な設定

パッケージにファイルを含める

パッケージに含めるファイル

作業ディレクトリのフォルダ構造レイアウトの規則

#フォルダ説明パッケージのインストール時の動作
1toolsPackage Manager ConsoleパッケージマネージャコンソールからアクセスできるPowershellスクリプトとプログラム内容はプロジェクトフォルダにコピーされ、ツールフォルダはPATH環境変数に追加されます。
2libアセンブリファイル(.dll)、ドキュメント(.xml)ファイル、およびシンボル(.pdb)ファイルアセンブリは参照として追加されます。 .xmlと.pdbはプロジェクトフォルダにコピーされます。
3content任意のファイルコンテンツはプロジェクトルートにコピーされます
4buildMSBuild .targetsと.propsファイルプロジェクトファイル(NuGet 2.x)またはproject.lock.json(NuGet 3.x)に自動的に挿入されます。

パッケージへのインクルード

nuspecファイルの<metadata>要素と同じ階層に、
<files>か<contentFiles>要素を追加することで、ファイルをインクルード出来る。

複数のTarget Frameworksのサポート

https://docs.microsoft.com/ja-jp/nuget/create-packages/supporting-multiple-target-frameworks

パッケージにファイルを含める」の方法で、複数バージョンを同梱するだけでいい。

設定

以下は、nuspecファイルの設定例

 <files>
   <file src="bin\Release\net46\MyAssembly.dll" target="lib\net46" />
   <file src="bin\Release\net461\MyAssembly.dll" target="lib\net461" />    
   <file src="bin\Release\uap\MyAssembly.dll" target="lib\uap" />    
   <file src="bin\Release\netcore\MyAssembly.dll" target="lib\netcore" />
 </files>

結果

パッケージのインストール後のpackagesフォルダのlibフォルダは以下のようになる。

参考

Dapperのnuspecファイルが参考になる。

パッケージ

Localized Packages(ローカライズ)

https://docs.microsoft.com/ja-jp/nuget/create-packages/creating-localized-packages

以下の2つの方法がある。

Symbol Packages(デバッグ用)

package type

NuGet 3.5以降で設定可能。

注意

一度登録すると通常消せない。

ビルド

'*.pdbを同梱する場合は、ビルド時のフォルダ構成に注意すること。

デバッグ

依存関係

Dependenciesタグ

NuGetサイトに表示されるのは、*.nspecファイルのDependenciesタグ明記したもの。

自動的に追加されるNuGetパッケージ

しかし、NuGetから追加された時には、

不明だが自動的に依存関係にあるNuGetパッケージが追加される。

参考

NuGetパッケージの開発と公開

nuspecファイルの定義例

著名なパッケージを参考にする。

Open棟梁のパッケージを参考にする。

nuspecファイルのメタデータ要素

必要なメタデータ要素(最小要件)

#要素説明
1id大文字と小文字を区別しない、nuget.orgなどのパッケージ・ギャラリー内で一意なパッケージ識別子。
IDにはURLに有効でないスペースや文字含めない。
2versionmajor.minor.patchパターンのパッケージのバージョン。
バージョン番号には、プレリリースパッケージに記載されているリリース前のサフィックスが含まれている場合があります。
3descriptionnuget.orgでパッケージのページを表示したときに表示される概要説明文
4authorsパッケージ著者のカンマ区切りのリスト。

オプションのメタデータ要素(必要に応じて追加)


Tags: :デプロイ, :NuGet, :.NET開発


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS