【ASP.NET】利用Nuget打包package——命令行方式

通过命令行


官方说明,可以参考:creating-and-publishing-a-package

如果你希望可以使用图形界面的方式,请参考这篇文章

打包dll

使用如下的命令:

nuget spec MyAssembly.dll

会生成MyAssessmbly.nuspec文件,与GUI方式中选择Edit Metadata生成的xml文件一致。

打开*.nuspec文件,可以看到结构如下的文件

<?xml version="1.0"?>
<package >
  <metadata>
    <id>StoShared.NHibernate.dll</id>
    <version>1.0.0</version>
    <authors>rentu</authors>
    <owners>rentu</owners>
    <licenseUrl>http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE</licenseUrl>
    <projectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</projectUrl>
    <iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Package description</description>
    <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
    <copyright>Copyright 2016</copyright>
    <tags>Tag1 Tag2</tags>
    <dependencies>
      <dependency  version="1.0" />
    </dependencies>
  </metadata>
</package>

根据需要,修改相应的配置属性,修改完成后,使用下面的命令进行打包

nuget pack MyAssembly.nuspec

注意,dll需要放到lib文件夹下,才会在install package后被自动添加引用。在lib文件夹下建立类似net40文件夹,可以保证package在.net 4.0上运行。

打包项目

在项目的根文件夹下(包含csproj的文件夹下,使用如下的命令)

nuget spec

会生成*.nuspec文件

<?xml version="1.0"?>
<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>$author$</authors>
    <owners>$author$</owners>
    <licenseUrl>http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE</licenseUrl>
    <projectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</projectUrl>
    <iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>$description$</description>
    <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
    <copyright>Copyright 2016</copyright>
    <tags>Tag1 Tag2</tags>
  </metadata>
</package>

其中的一些通配符,会在我们打包的时候,根据项目的配置文件自动替换,替换规则如下表所示。

TokenSource
$id$The Assembly name
$version$The assembly version as specified in the assembly’sAssemblyVersionAttribute (orAssemblyInformationalVersionAttribute if present).
$author$The company as specified in the AssemblyCompanyAttribute.
$description$The description as specified in the AssemblyDescriptionAttribute.

默认情况下,打包package只会包含编译生成的dll文件和lib文件夹下的文件,可以通过下面的配置添加其他需要的文件。

<files> <filesrc="..\..\SomeRoot\**\*.*" target=""/> </files>

编辑好后,通过下面的命令进行打包

nuget pack MyProject.csproj

可以有以下的参数:

nuget pack A.csproj -IncludeReferencedProjects 打包时包含引用的项目

nuget pack MyProject.csproj -Prop Configuration=Release 打包release的dll

最新博客地址:http://blog.turenlong.com/