简体   繁体   English

针对C#解决方案的MSBuild(类似版本)上的自定义

[英]Customizations on MSBuild (like version) for a C# solution

I'm thinking that the final result is going to be "it can't be that easily done", but just seems like it should be. 我认为最终结果将是“不可能那么容易地完成”,但似乎应该如此。 I have a personal project I am working on. 我有一个正在进行的个人项目。 I'd hate to have to manually (or even in script) change versions, company, copyright, and all that on ALL the assembly.cs files and would like all that to be either in a script or in a file I can change (so the script stays the same mostly) when I want to update the version. 我讨厌不得不手动(甚至在脚本中)更改版本,公司,版权以及所有assembly.cs文件上的所有内容,并且希望所有这些内容都可以在脚本或我可以更改的文件中进行(因此该脚本在大多数情况下都保持不变)。 But it seems like MSBuild is mostly a "build as is specified in Visual Studio". 但是,似乎MSBuild主要是“ Visual Studio中指定的内部版本”。 I'd just hate to have all that history of these files where I change just the version and possibly even make a mistake as this project will continue to get bigger and bigger. 我只是不想拥有这些文件的所有历史记录,而只更改版本,甚至可能会犯一个错误,因为该项目将继续变得越来越大。 I'd like to just be able to add a new project to Visual studio and have whatever command line in my powershell script just say "compile this, but give it this company name and this file version instead of whatever is listed in the code file". 我只想能够向Visual Studio中添加一个新项目,并在我的Powershell脚本中添加任何命令行即可,只需说“编译此文件,但是给它这个公司名称和这个文件版本,而不是代码文件中列出的内容即可。 ”。

Google has NOT proven fruitful in this. Google尚未在此方面取得成果。 I've even found it difficult to build my files to a specific folder. 我什至发现很难将文件构建到特定文件夹中。 I've had to so far make sure all my projects are 2 folders deep and was able to say to build them at ....\\, but I would like to be able to change that randomly if I like and have them built elsewhere if I so desire. 到目前为止,我必须确保我的所有项目都位于2个文件夹中,并且能够说要在.... \\上进行构建,但是如果我愿意,可以将其随机更改,并在其他位置进行构建如果我愿意的话。

Is MSBuild perhaps not the way to go? MSBuild也许不是走的路? Is there someway else to build visual studio that would be better from command line? 是否还有其他方法可以从命令行构建Visual Studio? Eventually I also want to auto build the install with wix and be able to match its version with the binary versions. 最终,我还希望使用wix自动构建安装程序,并使其版本与二进制版本匹配。

thank you 谢谢

Since csproj is xml, you can use XmlUpdate "helpers" to modify the values inside the csproj file before you do your build. 由于csproj是xml,因此在进行构建之前,可以使用XmlUpdate“ helpers”修改csproj文件中的值。

For other files, you can use some other Tasks to do the job. 对于其他文件,您可以使用其他一些任务来完成工作。

Here is one helpful target: 这是一个有用的目标:

http://msbuildtasks.tigris.org/ and/or https://github.com/loresoft/msbuildtasks has the ( FileUpdate (and SvnVersion task if that is your Source-Control) ) tasks. http://msbuildtasks.tigris.org/和/或https://github.com/loresoft/msbuildtasks具有(FileUpdate(如果是Source-Control,则为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\(&quot;(\d+)\.(\d+)\.(\d+)\.(\d+)"
            ReplacementText="AssemblyFileVersion(&quot;$1.$2.$3.$(MyRevision)" />

</Target>

Below is an example of manipulating the csproj(xml). 下面是操纵csproj(xml)的示例。

How to add a linked file to a csproj file with MSBuild. 如何使用MSBuild将链接文件添加到csproj文件。 (3.5 Framework) (3.5框架)

But basically, when you build, you can put all the repetative stuff in a msbuild definition file (usually with the extension .proj or .msbuild)...and call msbuild.exe MyFile.proj. 但基本上,在构建时,可以将所有重复性内容放入msbuild定义文件中(通常使用扩展名.proj或.msbuild)...并调用msbuild.exe MyFile.proj。

Inside the .proj file, you will reference your .sln file. 在.proj文件中,您将引用.sln文件。

For example: 例如:

$(WorkingCheckout) would be a variable (not defined here)...that has the directory where you got a copy of hte code from your source-control. $(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>

So below is the more complete example. 因此,下面是更完整的示例。 You would save this as "MyBuild.proj" and then call 您可以将其保存为“ MyBuild.proj”,然后调用

"msbuild.exe" "MyBuild.proj".

Start .proj code. 启动.proj代码。 (Note, I did not import the libraries for the FileUpdate Task) (注意,我没有导入FileUpdate Task的库)

<?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\(&quot;(\d+)\.(\d+)\.(\d+)\.(\d+)"
            ReplacementText="AssemblyFileVersion(&quot;$1.$2.$3.$(MyRevision)" />

</Target>



</Project>

To enhance the above, you would create a new target that would run before "BeforeBuild_VersionTagIt_Target", that would pull your code from source-control and put it in the $(WorkingCheckout) folder. 为了增强上述功能,您将创建一个在“ BeforeBuild_VersionTagIt_Target”之前运行的新目标,该目标将从源代码管理中提取您的代码并将其放在$(WorkingCheckout)文件夹中。

The basic steps would then be: 1. Checkout code from Source-Control. 基本步骤如下:1.从Source-Control检出代码。 2. Run the targets that alter the AssemblyVersion (and whatever else you want to manipulate) and 3. Build the .sln file. 2.运行更改AssemblyVersion的目标(以及您要操纵的其他目标),然后3.生成.sln文件。

That's the basics of a .proj file. 这就是.proj文件的基础。 You can do much more. 您可以做更多的事情。 Usually by using helper libraries that already exists. 通常通过使用已经存在的帮助程序库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM