为 .NET Core 项目设置版本号 - CSPROJ - 而不是 JSON 项目 [英] Setting the version number for .NET Core projects - CSPROJ - not JSON projects

查看:40
本文介绍了为 .NET Core 项目设置版本号 - CSPROJ - 而不是 JSON 项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题非常类似于设置.NET的版本号核心项目,但不一样.使用撰写本文时最新的稳定版本 .NET Core (1.1) 和 VS2017,.NET Core 已从基于 JSON 的项目文件切换到 CSPROJ 文件.

所以 - 我想要做的是设置一个 CI 环境,我希望能够在构建之前修改某些东西,以使用正确的版本号标记我的构建.>

如果我使用这样的旧属性(SharedAssemblyInfo.cs 技巧):

[程序集:AssemblyFileVersion("3.3.3.3")][装配:装配版本(4.4.4.4")]

在项目的某个地方,我得到
CS0579 - 重复的System.Reflection.AssemblyFileVersionAttribute"

CS0579 - 重复的System.Reflection.AssemblyVersionAttribute"
构建时出错.

深入研究后,我发现在objDebug etcoreapp1.1objDebug etcoreapp1.1 中,有一个看起来像这样的文件是在构建过程中生成的(在我构建之前它不存在)代码>:

//------------------------------------------------------------------------------//<自动生成>//此代码是由工具生成的.//运行时版本:4.0.30319.42000////对此文件的更改可能会导致不正确的行为,并且在以下情况下会丢失//重新生成代码.//</自动生成>//------------------------------------------------------------------------------使用系统;使用 System.Reflection;[程序集:System.Reflection.AssemblyCompanyAttribute("TestApplication")][程序集:System.Reflection.AssemblyConfigurationAttribute("Debug")][程序集:System.Reflection.AssemblyDescriptionAttribute("包描述")][程序集:System.Reflection.AssemblyFileVersionAttribute("1.1.99.0")][程序集:System.Reflection.AssemblyInformationVersionAttribute("1.1.99")][装配:System.Reflection.AssemblyProductAttribute("TestApplication")][程序集:System.Reflection.AssemblyTitleAttribute("TestApplication")][程序集:System.Reflection.AssemblyVersionAttribute("1.1.99.0")]//由 MSBuild WriteCodeFragment 类生成.

问题 - 我该怎么做?
所以我可以看到这必须以某种方式从在项目属性包页面"中输入的值生成,但我不知道在我的 CI 机器上更改这些值的正确方法是什么.

理想情况下,我希望能够在我的 (Jenkins) CI 脚本中指定所有这些信息,但我只能设置版本号.

编辑 - 更多信息
阅读第一个答案后,我想明确表示我正在创建服务和 NuGET 包 - 我更喜欢使用 1 种方式对所有内容进行版本控制,就像旧的 JSON 项目一样,我可以只更新一个文件.

更新我正在编写对 CSPROJ 文件的更改脚本,在我看来,它相当笨拙,因为我需要修改的部分看起来像这样...

<OutputType>Exe</OutputType><TargetFramework>netcoreapp1.1</TargetFramework><版本>1.0.7777.0</版本><AssemblyVersion>1.0.8888.0</AssemblyVersion><FileVersion>1.0.9999.0</FileVersion><公司>我的公司</公司><作者>作者姓名</作者><产品>产品名称</产品><说明/><版权>版权 © 2017</Copyright></PropertyGroup>

所以 - 这里的问题是有多个PropertyGroup"元素;其他人似乎被贴上了标签——但不知道 CSPROJ 是如何组合在一起的,我不能说情况总是如此.

我的工作前提是始终填写包详细信息,否则值标签(上方)不会出现在 XML 中 - 因此我可以使用脚本来更新适当的值.如果值标签不存在,我将不清楚要将值插入到哪个 PropertyGroup 元素(以及哪个顺序,因为这似乎很重要;更改顺序阻止我在 VS2017 中加载项目).

我仍然在寻找比这个更好的解决方案!

更新:有人将此问题标记为可能重复后(AutoVisual Studio 2017 (.NET Core) 中的版本控制(.NET Core)) - 我之前没有看到这个问题,现在阅读它似乎几乎相同,只是我不想设置版本号.此外,这个问题的答案并不能解决我的问题——只问我在问题中提出的问题.我的问题的公认答案正是我解决问题所需的答案-因此,虽然另一个问题首先出现并且看起来相同-但它根本没有帮助我.也许模组可以提供帮助?

解决方案

您可以通过将 /p:PropertyName=Value 作为参数传递给 dotnet restore<来覆盖命令行中的任何属性/code>、dotnet builddotnet pack.

目前,版本组合的工作原理如下:如果未设置 Version,请使用 VersionPrefix(如果未设置,则默认为 1.0.0)并且 - 如果存在 - 附加 VersionSuffix.

然后所有其他版本默认为 Version 是什么.

例如,您可以在 csproj 中设置 1.2.3 然后调用 dotnet pack --version-suffix beta1 来生成a YourApp.1.2.3-beta1.nupkg(如果您有项目引用并且希望版本后缀也适用,则需要调用 dotnet restore/p:VersionSuffix=beta1 在此之前 - 这是工具中的一个已知错误).

当然,您也可以使用自定义变量,请参阅这个 GitHub 问题举几个例子.

对于支持的程序集属性的完整参考,我建议查看构建逻辑的源代码这里(用$()包围的值是使用的属性).由于我已经在谈论源,this 是组成版本和其他一些属性的逻辑.

This question is very similar to Setting the version number for .NET Core projects, but not the same. Using the latest stable version of .NET Core at the time of writing (1.1) and VS2017, .NET Core has switched from JSON based project files to CSPROJ files.

So - what I am trying to do is set up a CI environment where I would like to be able to modify something prior to a build to stamp my builds with the correct version number.

If I use the attributes like this the old (SharedAssemblyInfo.cs trick):

[assembly: AssemblyFileVersion("3.3.3.3")]
[assembly: AssemblyVersion("4.4.4.4")]

somewhere in the project, I get
CS0579 - Duplicate 'System.Reflection.AssemblyFileVersionAttribute'
and
CS0579 - Duplicate 'System.Reflection.AssemblyVersionAttribute'
errors when building.

When digging into it a bit, I find that there is a file which looks like this generated during the build process (it doesn't exist before I build) in objDebug etcoreapp1.1:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Reflection;

[assembly: System.Reflection.AssemblyCompanyAttribute("TestApplication")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyDescriptionAttribute("Package Description")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.1.99.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.1.99")]
[assembly: System.Reflection.AssemblyProductAttribute("TestApplication")]
[assembly: System.Reflection.AssemblyTitleAttribute("TestApplication")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.1.99.0")]

// Generated by the MSBuild WriteCodeFragment class.

Question - How do I do this bit?
So I can see that this must somehow be generated from the values entered in the project properties 'package page', but I don't know what the right way would be to change these values on my CI machine.

Ideally, I'd like to be able to specify all this information in my (Jenkins) CI script, but I'd settle for just being able to set the version number.

EDIT - More Info
After reading the first answer, I wanted to make it clear that I am creating both services and NuGET packages - and I would prefer to have 1 way of versioning everything, which would be like the old JSON project where I could just update a single file.

UPDATE I am going with scripting a change to the CSPROJ file which in my opinion is rather hacky as the section I need to modify looks like this...

<PropertyGroup>
 <OutputType>Exe</OutputType>
 <TargetFramework>netcoreapp1.1</TargetFramework>
 <Version>1.0.7777.0</Version>
 <AssemblyVersion>1.0.8888.0</AssemblyVersion>
 <FileVersion>1.0.9999.0</FileVersion>
 <Company>MyCompany</Company>
 <Authors>AuthorName</Authors>
 <Product>ProductName</Product>
 <Description />
 <Copyright>Copyright © 2017</Copyright>
</PropertyGroup>

So - the problem here is that there are multiple 'PropertyGroup' elements; the others appear to be labelled - but not knowing how the CSPROJ is put together, I can't say this will always be the case.

I am working on the premise that the package details will always be filled in, otherwise the value tags (above) don't appear in the XML - so then I can use a script to update the values in place. If the value tags were not there, I would have no clear idea which PropertyGroup element to insert the values into (and also which order, as this appears to be important; changing the order stopped me from loading the project in VS2017).

I am still holding out for a better solution than this one!

Update: After someone marking this question as a possible duplicate (Auto Versioning in Visual Studio 2017 (.NET Core)) - I hadn't seen this question before and now reading it seems to be almost the same except that I dont just want to set the version number. Also, the answers to this question do not solve my problem - only asks what I asked in my question. The accepted answer to my question is exactly the answer I need to solve my problem - so while the other question came first and appears the same - it does not help me at all. Maybe a mod can help?

解决方案

You can override any property from the command line by passing /p:PropertyName=Value as arguments to dotnet restore, dotnet build and dotnet pack.

Currently, Version composition works as this: If Version is unset, use VersionPrefix (defaults to 1.0.0 if unset) and - if present - append VersionSuffix.

All other version are then defaulted to whatever Version is.

So for example you can set <VersionPrefix>1.2.3</VersionPrefix> in your csproj and then call dotnet pack --version-suffix beta1 to produce a YourApp.1.2.3-beta1.nupkg (if you have project reference that you want the version suffix to be applied to as well, you need to call dotnet restore /p:VersionSuffix=beta1 before that - this is a known bug in the tooling).

Of course, you can use custom variables as well, see this GitHub issue for a few examples.

For a complete reference of supported assembly attributes, i suggest looking at the source code of the build logic here (the values surrounded with $() are the properties used). And since i'm already talking about the source, this is the logic that composes the version and a few other properties.

这篇关于为 .NET Core 项目设置版本号 - CSPROJ - 而不是 JSON 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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