在构建过程中自动创建 NuGet 包 [英] Automating creating NuGet package as part of build process

查看:16
本文介绍了在构建过程中自动创建 NuGet 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个想要扩展的自动化构建过程,以便构建我通过 NuGet 分发的库.目前,运行 nuget.exe 来创建包是手动操作.

I have an automated build process that I'd like to extend so I can build the libraries I am distributing via NuGet. Currently, running nuget.exe to create the packages is a manual operation.

设置 VS 2010 以使我的 NuGet 包 (*.nupkg) 文件成为发布"构建的最终结果的最佳方法是什么?

请记住,我有一些包的其他文件(内容和工具).而且,在大多数情况下,我将多个项目合并到一个 NuGet 包中以支持 .NET 4、Silveright 和 Phone 7.

Keep in mind that I have other files (content and tools) for some of the packages. And, in most cases, I have multiple projects merged into a single NuGet package to support .NET 4, Silveright and Phone 7.

(我应该澄清一下,现有的自动化"过程是一个简单的批处理文件运行程序,它使用命令行构建解决方案.)

更新

我想刷新此讨论,因为问题尚未解决.虽然@pravin 提供的链接很有帮助,但它并没有解决我在一个包中有多个项目以及其他内容(如 PowerShell 脚本、配置和源代码转换等)的事实.

I want to refresh this discussion because the issue has not been resolved. While the link @pravin supplied is helpful, it doesn't address the fact that I have multiple projects in a single package as well as other contents like PowerShell scripts, configuration and source code transformations, etc.

我可以使用的最佳示例是同时具有 .NET 4 和 Silverlight 5 版本的程序集.它们分布在同一个包中.我无法使用构建后事件来创建包,因为该包依赖于两个项目.

The best example I can use is an assembly that has both a .NET 4 and Silverlight 5 version. These are distributed in the same package. I cannot use a post-build event to create the package because the package is dependent upon TWO projects.

推荐答案

创建一个自定义的 MSBuild .proj 文件可能会很好用.您可以在自定义脚本中定义几个目标,第一个在您的解决方案上执行编译.执行以下编译的第二个目标将使用 EXEC MSBuild 任务来调用 nuget.exe 命令行实用程序.然后,更新批处理文件运行程序以执行 msbuild 可执行文件,将自定义项目文件作为参数提供.您可能已经在批处理脚本中使用了 MSBuild,在这种情况下,这只是一个参数交换的问题.您可以在解决方案的解决方案项目中包含您的自定义项目文件.如果您这样做了,您可以轻松地在 Visual Studio 中添加一个外部工具引用,以快速测试您的自定义脚本,以确保它按照您希望的方式构建和生成包.

One thing that might work well is to create a custom MSBuild .proj file. You could define a couple targets in the custom script, the first to execute the compile on your solution. A second target to execute following compilation would use the EXEC MSBuild task to invoke the nuget.exe command line utility. Then, you update your batch file-runner to execute the msbuild executable supplying your custom project file as an argument. You might already be using MSBuild in your batch script, which in that case it would simply be a matter of argument swapping. You could include your custom proj file in the solution items of your solution. If you did that you could easily add an external tool reference in Visual Studio to quickly test out your custom script to make sure it was building and producing the package like you hope.

MSBuild 示例

您可以以此为起点:

<Project DefaultTargets="Compile" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
    <PropertyGroup>
      <SolutionFile></SolutionFile>
      <NugetExecutable>C:PathToNuget
uget.exe</NugetExecutable>
      <NuspecFile></NuspecFile>
    </PropertyGroup>

    <Target Name = "Compile">
        <MSBuild Projects="$(SolutionFile)" Properties="Configuration=Release" />
    </Target>

    <Target Name = "Package">
    <!-- You could use the MSBuild Copy task here to move the compiled code into
           a structure that fits your desired package format -->
      <Exec Command="&quot;$(NugetExecutable)&quot; pack $(NuspecFile)" />
    </Target>
</Project>

然后你可以这样称呼:

"C:WindowsMicrosoft.NETFrameworkv4.0.30319MSBuild.exe" Build.proj /p:SolutionFile=PathToSolutionApp.sln;NuspecFile=foo.nuspec

这篇关于在构建过程中自动创建 NuGet 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆