问题描述
使用 .NET Framework 库,您可以使用通配符指定版本,并且 NUGET pack 命令会在 VSTS 中运行 NUGET 构建任务时自动附加构建日期和版本.
With a .NET Framework library you could specify a version with a wildcard and NUGET pack command would append the build date and version automatically when running a NUGET Build Task in VSTS.
[assembly: AssemblyVersion("1.0.*")]
NUGET PACK 将生成一个 NUPKG 文件,其版本类似于 1.0.6604.1234
附加日期编号和构建 ID.
NUGET PACK would generate a NUPKG file with a version like 1.0.6604.1234
appending the date number and a build ID.
在 .NET Core 和 .NET 标准中,新的 .csproj
格式不支持这种通配符格式.
In .NET Core and .NET standard the new .csproj
format does not support this wildcard format.
我们不能用 Nuget.exe 打包(原因:这个问题)但是我们可以使用 dotnet pack
除非我需要自动增加内部版本号.VSTS 中的 dotnet
Build Task 允许我完全替换版本号,但我想在 csproj 文件中保留版本,并且只附加一个内部版本号(就像我以前那样).
We can't package with Nuget.exe (reason: this issue) but we can use dotnet pack
except I need to auto-increment the build numbers. The dotnet
Build Task in VSTS allows me to wholly replace the version number, but I want to retain the version in the csproj file, and just append a build number (as I used to).
我发现在 csproj 文件中使用 <VersionPrefix>x.y</VersionPrefix>
可以与 nuget pack
一起使用,然后我可以添加附加参数 VersionSuffix=$(Build.BuildNumber)
到打包任务.
I found that using <VersionPrefix>x.y</VersionPrefix>
in the csproj file would work with nuget pack
and I could then add the additional parameter VersionSuffix=$(Build.BuildNumber)
to the pack task.
一切看起来都不错,直到第一个开发人员在项目属性对话框中更新了项目版本.Visual Studio 忽略了 VersionPrefix 并设置了 标记 - 由于存在
Version
标记,因此忽略内部版本号修复.
All looked good until the first dev updated the project version in the project properties dialog. Visual Studio ignored the VersionPrefix and set the <Version>
tag - and the build number fix is ignored because a Version
tag exists.
有没有办法从 csproj 中读取 Version
?如果是这样,我可以将构建属性设置为 Version=$(ProjectVersion).$(Build.BuildNumber)
?
Is there a way to read the Version
from the csproj? If so I could set the build property to Version=$(ProjectVersion).$(Build.BuildNumber)
?
或者是否有其他方法可以在打包时处理自动递增构建版本?
Or are there alternative ways to handle auto-incrementing the build version when packaging?
推荐答案
感谢 @patricklu-msft 的建议.
Thanks to @patricklu-msft for his suggestions.
似乎没有内置的方法来模拟我们以前使用 NUGET pack
和 dotnet pack
的通配符行为,也没有办法获得 <Version>
标记出项目文件.
There is it appears no built-in way to emulate the wildcard behaviour we previously had NUGET pack
with dotnet pack
, nor was there way to get the <Version>
tag out of the project file.
所以我创建了一个新的 VSTS Build 任务来执行此操作:VersionTaskReader 在 MarketPlace.
So I've created a new VSTS Build task that does this: VersionTaskReader in the MarketPlace.
这个扩展可以指向一个 .csproj
或 .vbproj
并且会设置一个环境变量 VERSION
和 VERSION_BUILD
附加了 BUILDID
.如果需要,您可以选择添加前缀以使每个实例不同.
This extension can be pointed to a .csproj
or .vbproj
and will set an environment variable VERSION
, and VERSION_BUILD
which has the BUILDID
appended. You can optionally add a prefix to make each instance different if needed.
例如,如果您的项目包含 <Version>1.1</Version>
那么 VERSION_BUILD
将类似于 1.1.8680
>
For example, if your project contains <Version>1.1</Version>
then the VERSION_BUILD
would be something like 1.1.8680
然后 dotnet pack
任务可以使用版本控制选项屏幕中的环境变量 VERSION_BUILD
,以便内部版本号自动递增.
Then the dotnet pack
task can use the environment variable VERSION_BUILD
in the versioning options screen, so that the build number automatically increments.
这篇关于在 VSTS 构建过程中使用 `dotnet pack` 将构建号添加到包版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!