【问题标题】:MSBuild, example that filters entire solution of .cs filesMSBuild,过滤 .cs 文件的整个解决方案的示例
【发布时间】:2008-10-21 13:06:21
【问题描述】:

我想学习 MSBuild,想知道是否有人可以让我开始使用一个简单的构建脚本来过滤掉我的 vs.net 2008 项目中所有扩展名为 .cs 的文件。

  1. 如何运行构建?
  2. 您通常还将构建存储在哪里?

【问题讨论】:

    标签: msbuild


    【解决方案1】:

    您通常使用以下语法从命令行运行 MSBuild 脚本:

    MSBuild <scriptfilename> /t:targetname
    

    您可以在这里获取更多信息:http://msdn.microsoft.com/en-us/library/0k6kkbsd.aspx

    您想通过解析项目文件中的所有 .cs 文件来完成什么?请记住,对于 VS2005 及更高版本,项目文件本身就是 MSBuild 脚本,因此您只需在命令行中调用 MSBuild 并将项目文件的名称命名为合适的目标。

    话虽如此,如果您创建一个单独的脚本文件,我通常会将它们存储在根项目文件夹中。

    如果您希望文件列表作为一个以分号分隔的字符串属性:

    <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
      <Import Project="Project.csproj" Condition="Exists(Project.csproj')"/>
    
      <Target Name="Test">
         <Message Text="@(Compile)"/>
      </Target>
    </Project>
    

    如果您希望能够单独显示每个文件:

    <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
      <Import Project="Project.csproj" Condition="Exists(Project.csproj')"/>
    
      <Target Name="Test">
         <Message Text="%(Compile.FullPath)"/>
      </Target>
    </Project>
    

    遍历每个示例,第一行 (&lt;Project ...&gt;) 确定这是一个 MSBuild 项目文件这一事实,定义了 DefaultTargets(如果没有给出目标 (/t: targetname) 时将运行的目标)命令行)用于验证文件的 XML 架构 (xmlns) 和要使用的 MSBuild 版本 (ToolsVersion)。

    第二行 (&lt;Import ...&gt;) 告诉 MSBuild 包含名为“Project.csproj”的 MSBuild 脚本的内容(如果存在)。

    最后,我们定义一个名为“Test”的目标,其中包含一个任务。该任务是“消息”任务,它在屏幕上打印一条消息(“文本”中包含的任何内容)。

    在第一个示例&lt;Message Text="@(Compile)"/&gt; 中,我们将一个名为“Compile”的ItemGroup 引用为分号分隔的列表。在第二个示例中,我们引用了相同的 ItemGroup,但循环遍历该 ItemGroup 中的每个项目并打印“FullPath”元数据内容。 (Compile ItemGroup 在我们在第 2 行导入的 .csproj 中定义。)

    【讨论】:

      【解决方案2】:
      C:\projects\_Play\SimpleIpService>type \\sysrdswbld1\public\bin\mrb-vs2008.cmd
      @echo off
      
      call "c:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"
      
      echo %0 %*
      echo %0 %* >> %MrB-LOG%
      cd
      if not ""=="%~dp1" pushd %~dp1
      cd
      if exist %~nx1 (
              echo VS2008 build of '%~nx1'.
              echo VS2008 build of '%~nx1'. >> %MrB-LOG%
              set MrB-BUILDLOG=%MrB-BASE%\%MrB-WORK%.%MrB-NICEDATE%.%MrB-NICETIME%.build-errors.log
              msbuild.exe %~nx1 /t:Rebuild /p:Configuration=Release > %MrB-BUILDLOG%
              findstr /r /c:"[1-9][0-9]* Error(s)" %MrB-BUILDLOG%
              if not errorlevel 1 (
                      echo ERROR: sending notification email for build errors in '%~nx1'.
                      echo ERROR: sending notification email for build errors in '%~nx1'. >> %MrB-LOG%
                      call mrb-email "Mr Build isn't happy about build errors in '%~nx1'" %MrB-BUILDLOG%
              ) else (
                      findstr /r /c:"[1-9][0-9]* Warning(s)" %MrB-BUILDLOG%
                      if not errorlevel 1 (
                              echo ERROR: sending notification email for build warnings in '%~nx1'.
                              echo ERROR: sending notification email for build warnings in '%~nx1'. >> %MrB-LOG%
                              call mrb-email "Mr Build isn't happy about build warnings in '%~nx1'" %MrB-BUILDLOG%
                      ) else (
                              echo Successful build of '%~nx1'.
                              echo Successful build of '%~nx1'. >> %MrB-LOG%
                      )
              )
      ) else (
              echo ERROR '%1' doesn't exist.
              echo ERROR '%1' doesn't exist. >> %MrB-LOG%
      )
      popd
      

      【讨论】:

      • OP 正在寻找 MSBuild 脚本,而不是批处理文件。
      • 对不起,如果这不是他们想要的,但它是一个调用 msbuild.exe 的批处理文件。
      • 是的,它是一个调用 msbuild.exe 的批处理文件,但问题是要求提供一个示例 MSBuild 脚本文件。是的,有人问如何运行构建,但直接在命令行上调用它要简单得多(尤其是对于不熟悉 MSBuild 的人)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-16
      • 2011-08-13
      • 1970-01-01
      • 1970-01-01
      • 2013-09-05
      • 2011-09-09
      • 1970-01-01
      相关资源
      最近更新 更多