使 TeamCity 将 Subversion 内部版本号集成到程序集版本中
我想要调整类库的 TeamCity 构建配置的输出,以便生成的 dll 文件具有以下版本号:3.5.0.x,其中 x 是 TeamCity 已选取的颠覆修订版号。
我发现我可以使用 BUILD_NUMBER
环境变量来获取 x,但不幸的是我不明白我还需要做什么。
我发现“教程”都说“您只需将其添加到脚本中”,但他们没有说哪个脚本,并且“this”通常指的是 MSBuild 社区扩展中的 AssemblyInfo 任务。
我是否需要以某种方式构建自定义 MSBuild 脚本才能使用它? “脚本”与解决方案文件或 C# 项目文件相同吗?
我对MSBuild过程根本不了解,除了我可以将解决方案文件直接传递给MSBuild,但我需要添加到“脚本”的是XML,而解决方案文件显然不像XML。
那么,有人可以向我指出如何进行这项工作的分步指南吗?
这就是我最终得到的结果:
- 安装 MSBuild 社区任务
编辑我的核心的 .csproj 文件类库,并更改底部,使其显示为:
<导入项目=“$(MSBuildToolsPath)\Microsoft.CSharp.targets”/> <导入项目=“$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets”/> <目标名称=“BeforeBuild”>
<目标名称=“AfterBuild”> 更改我的所有 AssemblyInfo.cs 文件,以便它们不指定 AssemblyVersion 或 AssemblyFileVersion(回想起来,我会考虑将 AssemblyVersion 放回去)
- 添加了一个指向当前全局 GlobalInfo.cs 的链接,该链接位于所有项目之外
- 确保此文件构建一次,以便我有一个源代码管理中的默认文件
现在,仅当设置了环境变量 BUILD_NUMBER(这是我通过 TeamCity 构建时)时,才会更新 GlobalInfo.cs。
我选择保持 AssemblyVersion 不变,以便引用仍然有效,并且仅更新 AssemblyFileVersion,以便我可以看到 dll 来自哪个版本。
I want to adjust the output from my TeamCity build configuration of my class library so that the produced dll files have the following version number: 3.5.0.x, where x is the subversion revision number that TeamCity has picked up.
I've found that I can use the BUILD_NUMBER
environment variable to get x, but unfortunately I don't understand what else I need to do.
The "tutorials" I find all say "You just add this to the script", but they don't say which script, and "this" is usually referring to the AssemblyInfo task from the MSBuild Community Extensions.
Do I need to build a custom MSBuild script somehow to use this? Is the "script" the same as either the solution file or the C# project file?
I don't know much about the MSBuild process at all, except that I can pass a solution file directly to MSBuild, but what I need to add to "the script" is XML, and the solution file decidedly does not look like XML.
So, can anyone point me to a step-by-step guide on how to make this work?
This is what I ended up with:
- Install the MSBuild Community Tasks
Edit the .csproj file of my core class library, and change the bottom so that it reads:
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" /> <Target Name="BeforeBuild"> <AssemblyInfo Condition=" '$(BUILD_NUMBER)' != '' " CodeLanguage="CS" OutputFile="$(MSBuildProjectDirectory)\..\GlobalInfo.cs" AssemblyVersion="3.5.0.0" AssemblyFileVersion="$(BUILD_NUMBER)" /> </Target> <Target Name="AfterBuild">
Change all my AssemblyInfo.cs files so that they don't specify either AssemblyVersion or AssemblyFileVersion (in retrospect, I'll look into putting AssemblyVersion back)
- Added a link to the now global GlobalInfo.cs that is located just outside all the project
- Make sure this file is built once, so that I have a default file in source control
This will now update GlobalInfo.cs only if the environment variable BUILD_NUMBER is set, which it is when I build through TeamCity.
I opted for keeping AssemblyVersion constant, so that references still work, and only update AssemblyFileVersion, so that I can see which build a dll is from.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CSPROJ 文件实际上是一个 MSBuild 文件。
在 VS.NET 中卸载相关的类项目,对其进行编辑并取消注释
BeforeBuild
目标。从 MSBuild 社区扩展添加FileUpdate
MSBuild 任务。在 MSBuild 文件中,您可以使用环境变量
$(build_vcs_number_1)
从 TeamCity 检索 BUILD_NUMBER。请注意,您可能希望为您检查的“生产”条件创建一个额外的配置,因为当您在本地构建时,这显然不起作用。只需将其用作 FileUpdate 任务的
ReplacementText
属性的输入即可。请注意,如果您的修订号超过 65535 标记 (
UInt16
),则无法在AssemblyVersion
属性中使用它。不过,我建议您使用
AssemblyInformationalVersion
,它只是一个没有该限制的字符串。当然,除非您有信心不会达到此修订上限,但这似乎是一种狡猾的方法。或者,您可以设计一种方法(abcd 是您的版本号),对 c 使用修订版 div 1000,对 d 使用修订版 mod 1000。
The CSPROJ file is effectively an MSBuild file.
Unload the relevant class project in VS.NET, edit it and uncomment the
BeforeBuild
target. Add theFileUpdate
MSBuild task from the MSBuild Community Extensions.In your MSBuild file, you can retrieve the BUILD_NUMBER from TeamCity by using the environment variable
$(build_vcs_number_1)
. Note that you may want to create an additional Configuration for 'Production' which' condition you check for, as this will obviously not work when you build locally.Simply use that as input for the FileUpdate task's
ReplacementText
property.Note that if your revisions numbers go above the 65535 mark (
UInt16
), you cannot use it in theAssemblyVersion
attribute.What I'd suggest you use instead though, is
AssemblyInformationalVersion
, which is just a string that does not have that limitation. Unless of course you are confident that you won't hit this revision upper boundary, but that seems a dodgy way of going about it.Alternatively, you can devise a way (a.b.c.d being your version number) of using revision div 1000 for c and revision mod 1000 for d.
在 TeamCity 6.5 中,使用 AssemblyInfo patcher 可以非常容易实现这一点。
%system.build.number%
In TeamCity 6.5 this is VERY EASY to achieve using AssemblyInfo patcher.
%system.build.number%