我在想,最终的结果将是“这不可能那么容易”,但似乎应该是这样。我有个私人项目要做。我不想手动(甚至是在脚本中)更改版本、公司、版权以及所有assembly.cs文件上的所有内容,并且希望在我想要更新版本时,所有这些都在脚本或文件中,我可以更改(所以脚本大部分保持不变)。但是,MSBuild似乎主要是“Visual中指定的构建”。我只是不想有这些文件的所有历史,我只是改变版本,甚至可能会犯一个错误,因为这个项目将继续变得越来越大。我只想将一个新项目添加到Visual中,并在我的powershell脚本中有任何命令行,只需说“编译它,但是给它这个公司名称和这个文件版本,而不是代码文件中列出的任何内容”。
谷歌在这方面并没有取得丰硕成果。我甚至发现很难将我的文件构建到特定的文件夹中。到目前为止,我必须确保我的所有项目都有两个文件夹深,并且能够说是在....\上构建它们,但是如果我愿意的话,我希望能够随意地改变它们,如果我愿意的话,可以在其他地方构建它们。
MSBuild也许不是该走的路吗?还有其他方法可以从命令行构建更好的visual studio吗?最后,我还想用wix自动构建安装,并能够将其版本与二进制版本相匹配。
谢谢
发布于 2015-01-23 17:09:11
因为csproj是xml,所以您可以在构建之前使用XmlUpdate "helpers“来修改csproj文件中的值。
对于其他文件,您可以使用其他一些任务来完成该工作。
以下是一个有用的目标:
http://msbuildtasks.tigris.org/和/或https://github.com/loresoft/msbuildtasks具有( FileUpdate (和SvnVersion任务,如果这是您的源代码管理)任务)。
<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)...and调用msbuild.exe MyFile.proj。
在.proj文件中,您将引用您的.sln文件。
例如:
$(WorkingCheckout)将是一个变量(这里没有定义),...that有一个目录,您可以在其中从源代码管理中获得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.源代码管理中的签出代码。2.运行更改AssemblyVersion的目标(以及您想要操作的其他内容);3.构建.sln文件。
这是.proj文件的基础。你可以做得更多。通常通过使用已经存在的助手库。
https://stackoverflow.com/questions/28097350
复制相似问题