c# - 開発 - Visual Studio 2017(.NET Core)の自動バージョン管理
visual studio linux c# (9)
.NETCoreApp 1.1(Visual Studio 2017)でバージョンを自動インクリメントする方法を見つけるために、数時間の大半を費やしました。
AssemblyInfo.csがフォルダー
obj/Debug/netcoreapp1.1/
動的に作成されていることを知っています。
次の古いメソッドは受け入れません。
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.*")]
プロジェクトをパッケージに設定すると、そこにバージョンを設定できますが、これはAssemblyInfo.csファイルのビルドに使用されるようです。
私の質問は、.NET Core(または.NETStandard)プロジェクトでバージョンを制御する方法を誰かが考え出したということです。
.csprojの
<PropertyGroup>
セクション内
に
<Deterministic>False</Deterministic>
を追加します
AssemblyVersion *を機能させるための回避策については、 「。Net Core#22660の[AssemblyVersion]のワイルドカードに関する混乱を招くエラーメッセージ」 で説明してい ます。
ワイルドカードは、ビルドが確定的でない場合にのみ許可されます。これは、.Net Coreプロジェクトのデフォルトです。
<Deterministic>False</Deterministic>
をcsprojに追加すると、問題が修正されます。
.Net Core開発者が http://blog.paranoidcoding.com/2016/04/05/deterministic-builds-in-roslyn.html で説明されている決定的ビルドを有益であると見なし、 コンパイラが決定的である必要がある 理由 :同じ入力が同じ出力を生成する# 372
ただし、TeamCity、TFS、または他のCI / CDツールを使用している場合は、バージョン番号を管理し、それらによってインクリメントし、パラメーターとしてビルドに渡すことをお勧めします(他の回答で提案されているように)
msbuild /t:pack /p:Version=YourVersionNumber
NuGetパッケージの パッケージ番号
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<Version Condition=" '$(BUILD_BUILDNUMBER)' == '' ">0.0.1-local</Version>
<Version Condition=" '$(BUILD_BUILDNUMBER)' != '' ">$(BUILD_BUILDNUMBER)</Version>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="1.1.2" />
</ItemGroup>
</Project>
@Gigiは正しいため(現時点では)、上記の回答を受け入れましたが、イライラして次のPowerShellスクリプトを思い付きました。
まず、ソリューションフォルダー(UpdateBuildVersion.ps1)にスクリプトがあります。
#Get Path to csproj
$path = "$PSScriptRoot\src\ProjectFolder\ProjectName.csproj"
#Read csproj (XML)
$xml = [xml](Get-Content $path)
#Retrieve Version Nodes
$assemblyVersion = $xml.Project.PropertyGroup.AssemblyVersion
$fileVersion = $xml.Project.PropertyGroup.FileVersion
#Split the Version Numbers
$avMajor, $avMinor, $avBuild = $assemblyVersion.Split(".")
$fvMajor, $fvMinor, $fvBuild = $fileVersion.Split(".")
#Increment Revision
$avBuild = [Convert]::ToInt32($avBuild,10)+1
$fvBuild = [Convert]::ToInt32($fvBuild,10)+1
#Put new version back into csproj (XML)
$xml.Project.PropertyGroup.AssemblyVersion = "$avMajor.$avMinor.$avBuild"
$xml.Project.PropertyGroup.FileVersion = "$fvMajor.$fvMinor.$fvBuild"
#Save csproj (XML)
$xml.Save($path)
これをcsprojファイルに追加しました:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyVersion>0.0.1</AssemblyVersion>
<FileVersion>0.0.1</FileVersion>
<PreBuildEvent>powershell.exe –NonInteractive –ExecutionPolicy Unrestricted -command "& {$(SolutionDir)UpdateBuildVersion.ps1}"</PreBuildEvent>
</PropertyGroup>
</Project>
PreBuildEventに設定しても、ファイルがメモリに読み込まれるまでバージョン番号は更新されないため、バージョン番号は次のビルドまで反映されません。 実際、これをPostBuildEventに変更すると、同じ効果が得られます。
また、次の2つのスクリプトを作成しました:(UpdateMinorVersion.ps1)
#Get Path to csproj
$path = "$PSScriptRoot\src\ProjectFolder\ProjectName.csproj"
#Read csproj (XML)
$xml = [xml](Get-Content $path)
#Retrieve Version Nodes
$assemblyVersion = $xml.Project.PropertyGroup.AssemblyVersion
$fileVersion = $xml.Project.PropertyGroup.FileVersion
#Split the Version Numbers
$avMajor, $avMinor, $avBuild = $assemblyVersion.Split(".")
$fvMajor, $fvMinor, $fvBuild = $fileVersion.Split(".")
#Increment Minor Version - Will reset all sub nodes
$avMinor = [Convert]::ToInt32($avMinor,10)+1
$fvMinor = [Convert]::ToInt32($fvMinor,10)+1
$avBuild = 0
$fvBuild = 0
#Put new version back into csproj (XML)
$xml.Project.PropertyGroup.AssemblyVersion = "$avMajor.$avMinor.$avBuild"
$xml.Project.PropertyGroup.FileVersion = "$fvMajor.$fvMinor.$fvBuild"
#Save csproj (XML)
$xml.Save($path)
(UpdateMajorVersion.ps1)
#Get Path to csproj
$path = "$PSScriptRoot\src\ProjectFolder\ProjectName.csproj"
#Read csproj (XML)
$xml = [xml](Get-Content $path)
#Retrieve Version Nodes
$assemblyVersion = $xml.Project.PropertyGroup.AssemblyVersion
$fileVersion = $xml.Project.PropertyGroup.FileVersion
#Split the Version Numbers
$avMajor, $avMinor, $avBuild = $assemblyVersion.Split(".")
$fvMajor, $fvMinor, $fvBuild = $fileVersion.Split(".")
#Increment Major Version - Will reset all sub nodes
$avMajor = [Convert]::ToInt32($avMajor,10)+1
$fvMajor = [Convert]::ToInt32($fvMajor,10)+1
$avMinor = 0
$fvMinor = 0
$avBuild = 0
$fvBuild = 0
#Put new version back into csproj (XML)
$xml.Project.PropertyGroup.AssemblyVersion = "$avMajor.$avMinor.$avBuild"
$xml.Project.PropertyGroup.FileVersion = "$fvMajor.$fvMinor.$fvBuild"
#Save csproj (XML)
$xml.Save($path)
MSBuildプロパティ関数を使用して、現在の日付に基づいてバージョンサフィックスを設定できます。
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<VersionSuffix>pre$([System.DateTime]::UtcNow.ToString(yyyyMMdd-HHmm))</VersionSuffix>
</PropertyGroup>
これにより、 PackageName.1.0.0-pre20180807-1711.nupkgの ような名前のパッケージが出力されます。
MSBuildプロパティ関数の詳細: https://docs.microsoft.com/en-us/visualstudio/msbuild/property-functions : https://docs.microsoft.com/en-us/visualstudio/msbuild/property-functions
Visual Studio Team Services / TFSまたは他のCIビルドプロセスを使用して、バージョン管理機能が組み込まれている場合、msbuildの
Condition
属性を利用できます。次に例を示します。
msbuild /t:build /p:Version=YourVersionNumber /p:AssemblyVersion=YourVersionNumber
これにより、.NET Coreコンパイラは、
BUILD_BUILDNUMBER
環境変数が存在する場合はそれを使用し、ローカルマシンでビルドを実行している場合は
0.0.1-local
フォールバックします。
dotnet build /p:AssemblyVersion=1.2.3.4
私は、「。NET Core(または.NETStandard)プロジェクトでバージョンを制御する方法を誰もが理解していました」と応答していました。 CIビルドのコンテキストでこの問題を解決しようとしているこの質問を見つけました。 アセンブリバージョンをCIビルド番号に設定したかった。
これらの値は、
.csproj
ファイルで設定されるようになりました。
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<AssemblyVersion>1.0.6.0</AssemblyVersion>
<FileVersion>1.0.6.0</FileVersion>
<Version>1.0.1</Version>
</PropertyGroup>
これらは、プロジェクト設定の「
パッケージ」
タブに移動した場合に表示される値と同じです。
*
を使用してバージョンを自動インクリメントすることはできないと思いますが、あなたができることは、バージョンを置き換える後処理ステップを導入することです(たとえば、継続的な統合の一部として)。
私は、csproj構成形式を使用するVS2017のNet Coreアプリのバージョンインクリメンターを探しています。
私は、project.json形式では機能したが、.csproj形式の解決策を見つけるのに苦労したdotnet bumpというプロジェクトを見つけました。 筆者は、ドットネットバンプが.csproj形式のソリューションを実際に考案しました。これはMSBumpと呼ばれます。
GitHubには、次のプロジェクトがあります。
https://github.com/BalassaMarton/MSBump
コードとNugetで利用可能なコードを確認できます。 NugetでMSBumpを検索するだけです。
here .csproj .NET Coreバージョン文字列を設定するための簡単なCLIツールを作成しました。 CIビルド中に自動バージョンバンピングを行うために、GitVersionなどのツールと組み合わせることができます(必要な場合)。
GITのtags / describe機能を使用して、GIT設定に基づいて.Net Core / .Net Whateverプロジェクトのバージョン管理を有効にします。
プロジェクトのルートフォルダーにあり、csprojファイルに含まれているPrebuild.targets.xmlファイルを使用しています。
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="PreBuild.targets.xml" />
...
<PropertyGroup>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
「GenerateAssembyInfo」タグを使用して、自動アセンブリ情報生成を無効にします。
次に、Prebuild.targets.xmlはCommonAssemblyInfo.csファイルを生成します。このファイルには、GITバージョンに基づいて必要なバージョンタグを含めることができます
注:Prebuilds.targets.xmlはどこか他の場所で見つかったので、面倒なことはしませんでした。)
Prebuild.targets.xmlファイル:
<?xml version="1.0" encoding="utf-8" ?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask
TaskName="GetVersion"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
<ParameterGroup>
<VersionString ParameterType="System.String" Required="true" />
<Version ParameterType="System.String" Output="true" />
<Commit ParameterType="System.String" Output="true" />
<VersionSuffix ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<!--<Reference Include="" />-->
<Using Namespace="System"/>
<Using Namespace="System.IO"/>
<Using Namespace="System.Text.RegularExpressions" />
<Code Type="Fragment" Language="cs">
<![CDATA[
var match = Regex.Match(VersionString, @"^(?<major>\d+)\.(?<minor>\d+)(\.?(?<patch>\d+))?-(?<revision>\d+)-(?<commit>[a-z0-9-]+)$");
int major, minor, patch, revision;
Int32.TryParse(match.Groups["major"].Value, out major);
Int32.TryParse(match.Groups["minor"].Value, out minor);
Int32.TryParse(match.Groups["patch"].Value, out patch);
Int32.TryParse(match.Groups["revision"].Value, out revision);
_Version = new Version(major, minor, patch, revision).ToString();
_Commit = match.Groups["commit"].Value;
]]>
</Code>
</Task>
</UsingTask>
<UsingTask
TaskName="GitExistsInPath"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
<ParameterGroup>
<Exists ParameterType="System.Boolean" Output="true" />
</ParameterGroup>
<Task>
<!--<Reference Include="" />-->
<Using Namespace="System"/>
<Using Namespace="System.IO"/>
<Using Namespace="System.Text.RegularExpressions" />
<Code Type="Fragment" Language="cs">
<![CDATA[
var values = Environment.GetEnvironmentVariable("PATH");
foreach (var path in values.Split(';')) {
var exeFullPath = Path.Combine(path, "git.exe");
if (File.Exists(exeFullPath)) {
Exists = true;
return true;
}
var cmdFullPath = Path.Combine(path, "git.cmd");
if (File.Exists(cmdFullPath)) {
Exists = true;
return true;
}
}
Exists = false;
]]>
</Code>
</Task>
</UsingTask>
<Target Name="CreateCommonVersionInfo" BeforeTargets="CoreCompile">
<Message Importance="high" Text="CreateCommonVersionInfo" />
<GitExistsInPath>
<Output TaskParameter="Exists" PropertyName="GitExists"/>
</GitExistsInPath>
<Message Importance="High" Text="git not found!" Condition="!$(GitExists)"/>
<Exec Command="git describe --tags --long --dirty > $(ProjectDir)version.txt" Outputs="$(ProjectDir)version.txt" WorkingDirectory="$(SolutionDir)" IgnoreExitCode="true" Condition="$(GitExists)">
<Output TaskParameter="ExitCode" PropertyName="ExitCode" />
</Exec>
<Message Importance="high" Text="Calling git failed with exit code $(ExitCode)" Condition="$(GitExists) And '$(ExitCode)'!='0'" />
<ReadLinesFromFile File="$(ProjectDir)version.txt" Condition="$(GitExists) And '$(ExitCode)'=='0'">
<Output TaskParameter="Lines" ItemName="OutputLines"/>
</ReadLinesFromFile>
<Message Importance="High" Text="Tags: @(OutputLines)" Condition="$(GitExists) And '$(ExitCode)'=='0'"/>
<Delete Condition="Exists('$(ProjectDir)version.txt')" Files="$(ProjectDir)version.txt"/>
<GetVersion VersionString="@(OutputLines)" Condition="$(GitExists) And '$(ExitCode)'=='0'">
<Output TaskParameter="Version" PropertyName="VersionString"/>
<Output TaskParameter="Commit" PropertyName="Commit"/>
</GetVersion>
<PropertyGroup>
<VersionString Condition="'$(VersionString)'==''">0.0.0.0</VersionString>
</PropertyGroup>
<Message Importance="High" Text="Creating CommonVersionInfo.cs with version $(VersionString) $(Commit)" />
<WriteLinesToFile Overwrite="true" File="$(ProjectDir)CommonAssemblyInfo.cs" Encoding="UTF-8" Lines='using System.Reflection%3B
// full version: $(VersionString)-$(Commit)
[assembly: AssemblyVersion("$(VersionString)")]
[assembly: AssemblyInformationalVersion("$(VersionString)")]
[assembly: AssemblyFileVersion("$(VersionString)")]' />
</Target>
</Project>
編集:MSBUILDを使用して構築している場合
$(SolutionDir)
トラブルの原因になる可能性があります
$(ProjectDir)
代わりに