我正在使用基于 ant 的构建系统,并且对更新 AssemblyInfo.cs 有类似的问题。
我的构建系统已经生成了在构建时包含在其他项目中的 version.h 文件。对于 C#,我决定使用生成的 VersionInfo.cs 来注入版本信息到 AssemblyInfo.cs 中。
AssemblyInfo.cs (设置一次指定 VersionInfo 常量)
using System.Reflection;
using System.Runtime.InteropServices;
[assembly: AssemblyCompany(VersionInfo.Company)]
[assembly: AssemblyProduct(VersionInfo.ProductName)]
[assembly: AssemblyCopyright(VersionInfo.Copyright)]
[assembly: Guid("12345678-1234-1234-1234-1234567890ab")]
[assembly: AssemblyVersion(VersionInfo.product.FileVersion)]
[assembly: AssemblyFileVersion(VersionInfo.sdk.FileVersion)]
VersionInfo.cs (由ant构建系统动态生成)
internal struct VersionInfo
{
public const string Company = @"My Company Corp";
public const string Copyright = @"Copyright (c) 2015 My Company Corp. All rights reserved.";
public const string ProductName = @"My Software Service";
public const string SpecialBuild = @"";
public const string ProductConfiguration = @"";
public const string ProductCode = @"MSS";
public const string ProductUrl = @"www.MyCompany.com";
public const string ProductEmail = @"support@MyCompany.com";
public const string ProductVendor = @"My Company Corp";
internal struct product
{
public const string FileVersion = @"1.5.0.240";
}
internal struct sdk
{
public const string FileVersion = @"1.4.5.222";
}
internal struct service
{
public const string FileVersion = @"1.3.2.142";
}
}
version.xml (ant import - snipped)
<echo file="${artifacts.dir}\VersionInfo.cs" append="false">// Shared Product Version Header${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">// Automatically Generated: ${version.generated.time}${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">// ReSharper disable CheckNamespace${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">// ReSharper disable InconsistentNaming${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">internal struct VersionInfo${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">{${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true"> public const string Company = @"${product.company}";${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true"> public const string Copyright = @"${product.copyright}";${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">}${line.separator}</echo>
version.properties (ant属性文件,用于指定产品版本信息)
product.company=My Company Corp
product.copyright=Copyright (c) 2015 My Company Corp. All rights reserved.
product.website=www.MyCompany.com
product.email=support@MyCompany.com
product.vendor=My Company Corp
product=1.5.0.240
service=1.3.2.142
sdk=1.4.5.222
C#预构建事件(将自动生成的VersionInfo.cs复制到项目属性目录)
@ECHO OFF
SETLOCAL
SET ARTDIR=$(ProjectDir)..\..\..\MySoftwareService\installer
SET VERCS=%ARTDIR%\VersionInfo.cs
ECHO Updating %VERCS%
xcopy /D /Y "%VERCS%" "$(ProjectDir)Properties"
MSBuild扩展 (无)
<!
顺便说一下,我的版本方案是:主版本.次版本.修订版本.构建版本,这不是AssemblyInfo.cs文件指定的。