问题描述
此问题与,但不尽相同。在撰写本文时(1.1)和VS2017,使用了.NET Core的最新稳定版本,.NET Core已从基于JSON的项目文件切换为CSPROJ文件。
所以-我想做的是设置一个CI环境,在此环境中,我希望能够在构建之前修改某些内容,以用正确的版本号标记我的构建。
如果我使用这样的旧属性(SharedAssemblyInfo.cs技巧):
[程序集:AssemblyFileVersion( 3.3.3.3)]
[程序集:AssemblyVersion( 4.4.4.4)]
在项目中的某个地方,我得到了
CS0579-复制了'System.Reflection.AssemblyFileVersionAttribute'
和
CS0579-复制'System.Reflection.AssemblyVersionAttribute'
生成
时出错。 / p>
深入研究它时,我发现有一个文件看起来像是在 \obj\Debug\netcoreapp1.1
中的构建过程(在构建之前不存在):
// ----------------------------- -------------------------------------------------
//<自动生成>
//此代码是由工具生成的。
//运行时版本:4.0.30319.42000
//
//对该文件所做的更改可能会导致错误的行为,并且如果重新生成代码
将会丢失该行为。
//< /自动生成>
// -------------------------------------------- ----------------------------------
使用系统;
使用System.Reflection;
[assembly:System.Reflection.AssemblyCompanyAttribute( TestApplication)]
[assembly:System.Reflection.AssemblyConfigurationAttribute( Debug)]
[assembly:System.Reflection .AssemblyDescriptionAttribute(包装说明)]
[程序集:System.Reflection.AssemblyFileVersionAttribute( 1.1.99.0)]
[程序集:System.Reflection.AssemblyInformationalVersionAttribute( 1.1.99)]
[程序集:System.Reflection.AssemblyProductAttribute( TestApplication)]
[程序集:System.Reflection.AssemblyTitleAttribute( TestApplication)]
[程序集:System.Reflection.AssemblyVersionAttribute( 1.1 .99.0)]
//由MSBuild WriteCodeFragment类生成。
问题-我该怎么做?
因此,我可以看到必须以某种方式从在项目属性包页面中输入的值生成此值,但是我不知道在CI机器上更改这些值的正确方法。
理想情况下,我希望能够在我的(Jenkins)CI脚本中指定所有这些信息,但是我只希望能够设置版本号。
编辑-更多信息
阅读完第一个答案后,我想说清楚我正在创建服务和NuGET软件包-我希望有一种对所有内容进行版本控制的方法,就像旧的JSON项目一样,我可以只更新一个文件。
更新
我正在编写CSPROJ文件更改脚本,我认为这是我需要的部分,修改如下所示...
< 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>我的公司< / Company>
< Authors> AuthorName< / Authors>
< Product> ProductName< / Product>
<说明/>
< Copyright>版权©2017< / Copyright>
< / PropertyGroup>
所以-这里的问题是存在多个 PropertyGroup元素;其他似乎已贴上标签-但不知道CSPROJ是如何组合在一起的,我不能说情况总是如此。
我正在做一个前提,即始终要填写包裹的详细信息,否则,上面的value标记不会出现在XML中-因此,我可以使用脚本来更新适当的值。如果值标签不存在,我将不清楚将值插入哪个PropertyGroup元素(以及哪个顺序,因为这似乎很重要;更改顺序使我无法在VS2017中加载项目)。
我仍在寻求比这个更好的解决方案!
更新:有人标记了这个问题之后作为可能的重复项()-我以前没有看过这个问题,现在阅读它似乎几乎是一样的,只是我不想只设置版本号。另外,此问题的答案不能解决我的问题-仅询问我在问题中提出的内容。我的问题的公认答案恰好是我解决问题所需要的答案-因此,尽管另一个问题排在第一位并且看起来是相同的-但这对我没有任何帮助。
您可以通过传递 / p来覆盖命令行中的任何属性。 :PropertyName = Value
作为 dotnet恢复
, dotnet构建
和<$ c $的参数c> dotnet pack 。
当前,版本构成按以下方式工作:
如果 Version
未设置,请使用 VersionPrefix
(如果未设置,默认为1.0.0),并且-如果存在-附加 VersionSuffix
。
所有其他版本都默认为 Version
。
因此,例如,可以在csproj中设置< VersionPrefix> 1.2.3< / VersionPrefix>
,然后调用 dotnet pack --version-suffix beta1
生成 YourApp.1.2.3-beta1.nupkg
(如果您有项目参考,则需要版本后缀同样要应用到此,您需要先调用 dotnet restore / p:VersionSuffix = beta1
-这是一个已知的命令
当然,您也可以使用自定义变量,请参见的示例。
有关受支持的程序集属性的完整参考,我建议您看一下生成逻辑的源代码(用 $()
包围的值是所使用的属性)。
并且因为我已经在谈论源代码,所以是组成版本和其他一些属性的逻辑。
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 getCS0579 - Duplicate 'System.Reflection.AssemblyFileVersionAttribute'
andCS0579 - 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 \obj\Debug\netcoreapp1.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.
UPDATEI 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项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!