【问题标题】:Why won't MSBuild build a project with a dot in the name?为什么 MSBuild 不会构建名称中带有点的项目?
【发布时间】:2011-05-02 22:12:07
【问题描述】:

迄今为止的故事

我有一个很好的解决方案,其中包含一个桌面应用程序项目、几个库项目和几个开发工具项目(也是桌面应用程序)。目前,我的构建服务器将所有代码输出到一个OutputPath。所以我们最终得到了

drop-x.y.z\  
  Company.MainApplication.exe      <-- main application   
  Company.MainApplicationCore.dll  <-- libraries  
  Helper.exe                       <-- developer tools  
  Grapher.exe  
  Parser.exe  
  ...                              <-- the rest of the output

但是,我们正在成长,我们团队以外的人希望使用我们的工具。所以我想组织输出。我决定我们想要的是每个可执行项目都有一个不同的OutputPath

drop-x.y.z\
  Company.MainApplication\
    Company.MainApplication.exe      <-- main application 
    Company.MainApplicationCore.dll  <-- libraries
    ...                              <-- application specific output
  Helper\
    Helper.exe                       <-- developer tools
    ...                              <-- tool specific output
  Grapher\
    Grapher.exe
    ...
  Parser\
    Parser.exe
    ...

我做了什么

我发现了这个简单的命令。我喜欢它,因为它保留了所有使 msbuild 痛苦的解决方案工作目录上下文。

msbuild /target:<ProjectName>

例如,从我的解决方案根目录作为工作目录,我会调用

PS> msbuild /target:Helper /property:OutputPath="$pwd\out\Helper"

我正在通过 PowerShell 对此进行测试,以便 $pwd 解析为我的工作目录的完整路径,或者在这种情况下为解决方案根目录。我得到了我想要的输出。

但是,当我运行这个命令时

PS> msbuild /target:Company.MainApplication /property:OutputPath="$pwd\out\Company.MainApplication"

我得到以下错误输出(没有更多信息,我用/verbosity:diagnostic 运行)

项目中不存在目标“Company.MainApplication”。


我需要什么

该命令在名称中包含一个或多个点的任何项目上都会失败。我尝试了许多工作目录和属性的组合。我尝试了几种转义属性值的方法。我还尝试从目标文件中的&lt;Task&gt; 运行命令。

我也需要知道
A)如何修复此命令以使其正常工作
B)如何以最小的摩擦实现相同的输出

【问题讨论】:

    标签: msbuild


    【解决方案1】:

    尝试使用下划线作为目标参数中点的转义字符,例如

    msbuild /target:Company_MainApplication /property:OutputPath="$pwd\out\Company.MainApplication"
    

    【讨论】:

    • 我非常感谢这个正确的答案,但是为什么,为什么,为什么是这个答案!
    • 好问题!我知道这是因为 TeamCity 中传递给 MsBuild 的所有系统属性都使用下划线代替点。例如,Ant 中的 {build.vcs.number.1} 在 MsBuild 中变为 $(build_vcs_number_1)。由于 itemgroup 引用的语法,也许 MSFT 刚刚决定点应该成为保留字符,例如%myItem.filename%
    【解决方案2】:

    在 -target: 开关后指定目标,格式为 :。如果项目名称包含任何字符 %、$、@、;、.、(、) 或 ',请在指定目标名称中将它们替换为 _。

    https://docs.microsoft.com/en-us/visualstudio/msbuild/how-to-build-specific-targets-in-solutions-by-using-msbuild-exe?view=vs-2019

    Dan Nolan 的答案和 cmets 是正确的。只是想补充一下微软的文档。

    【讨论】:

      【解决方案3】:

      /targets: 开关是标识一个&lt;Target 在项目文件中运行。您需要将 .csproj 文件作为参数提供,该参数/xx 选项标记为前缀。

      您可能还想基于 .sln 文件进行工作。在这种情况下,您仍然没有在 .sln 中指定要以这种方式构建的项目。我会让你搜索正确的语法,以防你最终会这样做。

      【讨论】:

      • 这个thread 接受的答案表明我使用/target:ProjectName 开关是正确的。
      • 注意:以解决方案根为工作目录,此命令msbuild /target:ProjectNamemsbuild MySolution.sln /target:ProjectName相同
      • @AnthonyMastrean 非常感谢您的反馈;我接受Dan Nolan's answer is correct。从来没有听说过这种机制(当时我读过很多文章和 Hashimi 书)——如果有人能找到它的文档链接,我真的很感激。
      猜你喜欢
      • 1970-01-01
      • 2020-12-22
      • 2010-09-12
      • 2011-10-07
      • 2018-06-19
      • 1970-01-01
      • 2016-11-03
      • 2021-04-19
      • 2018-09-02
      相关资源
      最近更新 更多