GitVer.MSBuild 1.0.2

dotnet add package GitVer.MSBuild --version 1.0.2
                    
NuGet\Install-Package GitVer.MSBuild -Version 1.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GitVer.MSBuild" Version="1.0.2">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="GitVer.MSBuild" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="GitVer.MSBuild">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add GitVer.MSBuild --version 1.0.2
                    
#r "nuget: GitVer.MSBuild, 1.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package GitVer.MSBuild@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=GitVer.MSBuild&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=GitVer.MSBuild&version=1.0.2
                    
Install as a Cake Tool

GitVer.MSBuild - 輕量級 Git 版本自動化工具

零配置的 Git 版本自動化 MSBuild 套件,專為台灣開發者優化

🚀 快速開始

1. 安裝套件

dotnet add package GitVer.MSBuild

或在 .csproj 中加入:

<PackageReference Include="GitVer.MSBuild" Version="1.0.0">
  <PrivateAssets>all</PrivateAssets>
</PackageReference>

2. 建置專案

dotnet build

3. 使用版本信息

using YourNamespace; // 或 using GitVer;

Console.WriteLine($"版本: {GitVersionInfo.Version}");
Console.WriteLine($"標籤: {GitVersionInfo.Tag}");
Console.WriteLine($"提交: {GitVersionInfo.Commit}");
Console.WriteLine($"建置時間: {GitVersionInfo.BuildTimestamp}");

📋 版本格式

自動根據 Git 狀態選擇最適合的版本格式:

  • 完整版本: v1.0.0-abc12345 @ 2025-10-08T15:32:34+08:00
  • 僅標籤: v1.0.0 @ 2025-10-08T15:32:34+08:00
  • 僅提交: abc12345 @ 2025-10-08T15:32:34+08:00
  • 後備版本: dev @ 2025-10-08T15:32:34+08:00

⚙️ 自訂配置

.csproj 中加入屬性來自訂行為:

<PropertyGroup>
  
  <GitVerEnabled>false</GitVerEnabled>
  
  
  <GitVerTimezone>0</GitVerTimezone>
  
  
  <GitVerNamespace>MyApp.Version</GitVerNamespace>
  
  
  <GitVerClassName>AppVersion</GitVerClassName>
  
  
  <GitVerFallbackVersion>unknown</GitVerFallbackVersion>
</PropertyGroup>

🎯 使用場景

主控台應用程式

namespace MyApp;

class Program
{
    static void Main()
    {
        Console.WriteLine($"MyApp {GitVersionInfo.Version}");
        if (args.Contains("--version"))
        {
            Console.WriteLine($"Tag: {GitVersionInfo.Tag}");
            Console.WriteLine($"Commit: {GitVersionInfo.Commit}"); 
            Console.WriteLine($"Built: {GitVersionInfo.BuildTimestamp}");
            return;
        }
        // 應用程式邏輯...
    }
}

Web API

[ApiController]
[Route("api/[controller]")]
public class VersionController : ControllerBase
{
    [HttpGet]
    public object GetVersion()
    {
        return new
        {
            version = GitVersionInfo.Version,
            tag = GitVersionInfo.Tag,
            commit = GitVersionInfo.Commit,
            buildTime = GitVersionInfo.BuildTimestamp
        };
    }
}

記錄檔輸出

logger.LogInformation("Application started: {Version}", GitVersionInfo.Version);

🔧 進階功能

條件編譯

#if DEBUG
Console.WriteLine($"Debug Build: {GitVersionInfo.Version}");
#else
Console.WriteLine($"Release Build: {GitVersionInfo.Version}");
#endif

版本比較

public static class VersionHelper
{
    public static bool IsDevBuild => GitVersionInfo.Version.Contains("dev");
    public static bool HasTag => !string.IsNullOrEmpty(GitVersionInfo.Tag);
    public static DateTime BuildTime => DateTime.Parse(GitVersionInfo.BuildTimestamp);
}

🆚 與其他套件比較

特色 GitVer.MSBuild GitVersion.MsBuild Nerdbank.GitVersioning
零配置 ❌ 需要 GitVersion.yml ❌ 需要 version.json
台灣時區 ✅ 預設 +08:00 ❌ UTC ❌ UTC
輕量級 ✅ 純 MSBuild ❌ 外部工具 ❌ 複雜設定
即用即走 ✅ 安裝即用 ❌ 需要學習 ❌ 需要配置
語義化版本 ❌ 簡單格式 ✅ 完整 SemVer ✅ 完整 SemVer

📄 授權

MIT License

🤝 貢獻

歡迎提交 Issue 和 Pull Request!

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.2 521 10/8/2025
1.0.1 336 10/8/2025
1.0.0 391 10/8/2025

v1.0.2:
• 修正套件使用正確的 README.md 文件 (GitVer.MSBuild 專用說明)
• 確保 NuGet 套件顯示正確的使用說明

v1.0.1:
• 修正 README.md 中的 GitHub 連結
• 更新文件連結到正確的 repository

v1.0.0:
• 初始版本
• 支援 Git tag + commit hash + 建置時間
• 台灣時區支援 (+08:00)
• 強型別 GitVersionInfo 類別生成
• 多重後備策略