由于 csproj 是 xml,因此您可以在构建之前使用 XmlUpdate "helpers" 修改 csproj 文件中的值。
对于其他文件,您可以使用其他一些任务来完成这项工作。
这是一个有用的目标:
<Target Name="BeforeBuild_VersionTagIt_Target">
<ItemGroup>
<AssemblyInfoFiles Include="$(ProjectDir)\**\*AssemblyInfo.cs" />
</ItemGroup>
<!--
<SvnVersion LocalPath="$(MSBuildProjectDirectory)" ToolPath="$(SVNToolPath)">
<Output TaskParameter="Revision" PropertyName="MyRevision" />
</SvnVersion>
-->
<PropertyGroup>
<MyRevision>9999</MyRevision>
</PropertyGroup>
<FileUpdate Files="@(AssemblyInfoFiles)"
Regex="AssemblyFileVersion\("(\d+)\.(\d+)\.(\d+)\.(\d+)"
ReplacementText="AssemblyFileVersion("$1.$2.$3.$(MyRevision)" />
</Target>
以下是操作 csproj(xml) 的示例。
How to add a linked file to a csproj file with MSBuild. (3.5 Framework)
但基本上,当你构建时,你可以将所有重复的东西放在一个 msbuild 定义文件中(通常带有扩展名 .proj 或 .msbuild)......然后调用 msbuild.exe MyFile.proj。
在 .proj 文件中,您将引用您的 .sln 文件。
例如:
$(WorkingCheckout) 将是一个变量(此处未定义)...具有您从源代码管理中获取 hte 代码副本的目录。
<Target Name="BuildIt" >
<MSBuild Projects="$(WorkingCheckout)\MySolution.sln" Targets="Build" Properties="Configuration=$(Configuration)">
<Output TaskParameter="TargetOutputs" ItemName="TargetOutputsItemName"></Output>
</MSBuild>
<Message Text="BuildItUp completed" />
</Target>
所以下面是更完整的例子。
您可以将此保存为“MyBuild.proj”,然后调用
"msbuild.exe" "MyBuild.proj".
启动 .proj 代码。 (注意,我没有为 FileUpdate 任务导入库)
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="AllTargetsWrapped">
<PropertyGroup>
<!-- Always declare some kind of "base directory" and then work off of that in the majority of cases -->
<WorkingCheckout>.</WorkingCheckout>
</PropertyGroup>
<Target Name="AllTargetsWrapped">
<CallTarget Targets="BeforeBuild_VersionTagIt_Target" />
<CallTarget Targets="BuildItUp" />
</Target>
<Target Name="BuildItUp" >
<MSBuild Projects="$(WorkingCheckout)\MySolution.sln" Targets="Build" Properties="Configuration=$(Configuration)">
<Output TaskParameter="TargetOutputs" ItemName="TargetOutputsItemName"></Output>
</MSBuild>
<Message Text="BuildItUp completed" />
</Target>
<Target Name="BeforeBuild_VersionTagIt_Target">
<ItemGroup>
<AssemblyInfoFiles Include="$(ProjectDir)\**\*AssemblyInfo.cs" />
</ItemGroup>
<!--
<SvnVersion LocalPath="$(MSBuildProjectDirectory)" ToolPath="$(SVNToolPath)">
<Output TaskParameter="Revision" PropertyName="MyRevision" />
</SvnVersion>
-->
<PropertyGroup>
<MyRevision>9999</MyRevision>
</PropertyGroup>
<FileUpdate Files="@(AssemblyInfoFiles)"
Regex="AssemblyFileVersion\("(\d+)\.(\d+)\.(\d+)\.(\d+)"
ReplacementText="AssemblyFileVersion("$1.$2.$3.$(MyRevision)" />
</Target>
</Project>
为了增强上述功能,您将创建一个在“BeforeBuild_VersionTagIt_Target”之前运行的新目标,该目标将从源代码控制中提取您的代码并将其放入 $(WorkingCheckout) 文件夹中。
然后,基本步骤将是: 1. 从 Source-Control 签出代码。 2. 运行更改 AssemblyVersion 的目标(以及您想要操作的任何其他内容)和 3. 构建 .sln 文件。
这是 .proj 文件的基础。你可以做更多。通常通过使用已经存在的帮助库。