Msbuild 如何从外部文件读取属性值?
我已在生成过程中自动生成AssemblyInfo.cs文件。以下是.csproj文件的一部分:Msbuild 如何从外部文件读取属性值?,msbuild,build,build-process,csproj,Msbuild,Build,Build Process,Csproj,我已在生成过程中自动生成AssemblyInfo.cs文件。以下是.csproj文件的一部分: <PropertyGroup> <Major>2</Major> <Minor>3</Minor> <Build>0</Build> <Revision>0</Revision> </PropertyGroup> <Target Name="Be
<PropertyGroup>
<Major>2</Major>
<Minor>3</Minor>
<Build>0</Build>
<Revision>0</Revision>
</PropertyGroup>
<Target Name="BeforeBuild">
<SvnVersion LocalPath="$(MSBuildProjectDirectory)" ToolPath="C:\Program Files\VisualSVN Server\bin">
<Output TaskParameter="Revision" PropertyName="Revision" />
</SvnVersion>
<AssemblyInfo CodeLanguage="CS"
OutputFile="$(MSBuildProjectDirectory)\Properties\VersionInfo.cs"
AssemblyVersion="$(Major).$(Minor).$(Build).$(Revision)"
AssemblyFileVersion="$(Major).$(Minor).$(Build).$(Revision)"/>
</Target>
2.
3.
0
0
但我不知道如何在.csproj文件之外指定Major
和Minor
属性,所以我不必每次更改版本时都卸载项目。我需要从项目内部的特殊文本文件加载它们,或者在“项目属性”对话框中以某种方式设置它们。有什么建议吗?请使用,以便您可以在“项目属性表”对话框中设置这些属性
您需要创建属性文件并编辑项目文件(仅一次),才能将导入指令添加到属性文件中 您可以使用外部工具
<Exec Command="newversion incMinor AssemblyInfo.cs > newversion.log" />
如果您担心的是VS锁定csproj文件,我对这个问题的回答可能会对您有所帮助
您可以将BeforeBuild任务(包括版本属性组)的内容移动到单独的proj文件中,并使用MSBuild任务调用它(使用上面链接的答案中的示例生成的随机文件名)。这将允许您手动编辑版本号属性,而无需卸载/加载csproj文件。使用
ReadLinesFromFile
在单独的文件中生成版本:
<ReadLinesFromFile File="Properties\Version.txt">
<Output TaskParameter="Lines" ItemName="Ver" />
</ReadLinesFromFile>
<SvnVersion LocalPath="$(MSBuildProjectDirectory)" ToolPath="C:\Program Files (x86)\VisualSVN Server\bin">
<Output TaskParameter="Revision" PropertyName="Revision" />
</SvnVersion>
<Message Text="Version: @(Ver).$(Revision)" />
<AssemblyInfo
CodeLanguage="CS"
OutputFile="$(MSBuildProjectDirectory)\Properties\VersionInfo.cs"
AssemblyVersion="@(Ver).$(Revision)"
AssemblyFileVersion="@(Ver).$(Revision)"/>
可以使用直接调用某些.NET函数的功能来执行此操作。本质上,您可以在设置属性值时调用
<PropertyGroup>
<Version>$([System.IO.File]::ReadAllText("Version.txt"))</Version>
</PropertyGroup>
<SvnVersion LocalPath="$(MSBuildProjectDirectory)" ToolPath="C:\Program Files (x86)\VisualSVN Server\bin">
<Output TaskParameter="Revision" PropertyName="Revision" />
</SvnVersion>
<Message Text="Version: $(Version).$(Revision)" />
<AssemblyInfo
CodeLanguage="CS"
OutputFile="$(MSBuildProjectDirectory)\Properties\VersionInfo.cs"
AssemblyVersion="$(Version).$(Revision)"
AssemblyFileVersion="$(Version).$(Revision)"/>
$([System.IO.File]::ReadAllText(“Version.txt”))
Version.txt文件将只包含前三个版本号,即“1.2.3”或其他版本号
我不确定这是什么时候添加到MSBuild的,但可能是在这个问题被询问和回答之后。我希望能够更改我的程序集版本(“主要”和“次要”属性)没有卸载项目和编辑.CSPROJ文件在记事本中。这种方法只适用于C++,因为C使用项目设计器而不是完全不同的属性页。VIONI.TXT文件是什么样子的?