如何在不重建项目的情况下使用 msbuild 创建 nuget 包? [英] How to create nuget package with msbuild without rebuilding project?

查看:26
本文介绍了如何在不重建项目的情况下使用 msbuild 创建 nuget 包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一个包含构建、测试和部署阶段的 CI/CD 管道.我可以在构建阶段构建我的项目

I would like to setup a CI/CD pipeline with build, test and deploy stages. I can build my project in build stage with

msbuild srcMyProject.csproj /t:Restore 
msbuild srcMyProject.csproj /p:Configuration=Release /p:OutputPath=../BuildOutput

接下来我将针对已经构建的 ..BuildOutputMyProject.dll 构建和运行测试.

Next I will build and run tests against ..BuildOutputMyProject.dll that has already been built.

msbuild testsMyProject.Tests.csproj /t:Restore
msbuild testsMyProject.Tests.csproj /p:Configuration=Release /p:OutputPath=../BuildOutput /p:BuildProjectReferences=false
vstest.console BuildOutputMyProject.Tests.dll

到目前为止,它似乎有效.

Up to this point it seems to work.

现在我想生成 nuget 包.我可以打电话:

Now I would like to generate nuget package. I can call:

msbuild srcMyProject.csproj /t:Pack /p:Configuration=Release /p:OutputPath=../BuildOutput /p:VersionPrefix=1.2.3

这将在 BuildOutput 文件夹中创建 MyProject.1.2.3.nupkg.但是它会重新构建该项目.

And that would create MyProject.1.2.3.nupkg in BuildOutput folder. However it re-builds that project.

我正在寻找类似于 dotnet cli 的东西.

I'm looking something similar to dotnet cli.

dotnet pack --no-build

但我不能使用 dotnet,因为我的项目有 COM 引用.

But I cannot use dotnet because my project has a COM reference.

我也查看了 Nuget.exe,但是当我调用 nuget pack

I also looked into Nuget.exe, but it seems to throw an error when I call nuget pack

Unable to cast object of type 'System.String' to type NuGet.Frameworks.NuGet.Frameworks1051960.NuGetFramework'.

msbuild 是否有可以跳过构建的属性?

推荐答案

这取决于您的工作流程是否可以接受,但您可以在 中使用 <GeneratePackageOnBuild> 属性.csproj 文件的 <PropertyGroup> 以在构建项目时获取包.万一您的测试没有通过,您可以丢弃 .nupkg 文件,但至少没有额外的重建来获取 NuGet 包.

It depends on whether this is acceptable in your workflow, but you could use the <GeneratePackageOnBuild> property in a <PropertyGroup> of your .csproj file to get the package whenever you build your project. In case your tests do not go through, you can then discard the .nupkg file, but at least there is no additional rebuild for getting the NuGet package.

文章使用 VS2017 和 Nuget 轻松支持多个目标框架 (TFM)1,但一个示例性非常简单的代码段如下所示:

There is a more complete example of how to use this setting at the end of the article Easily supporting multiple target frameworks (TFMs) with VS2017 and Nuget1, but an exemplary very simple snippet would look like this:

<PropertyGroup>
    <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>


1:虽然原始链接没有报告 404,但它在 2021 年 10 月显示为空白.但是,可以找到同一篇文章 在另一个 URL 上.


1: While the original link does not report a 404, it appears as blank in October 2021. The same article can, however, be found on another URL.

这篇关于如何在不重建项目的情况下使用 msbuild 创建 nuget 包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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