2012-04-05 81 views
1

我正在研究构建过程的自定义,并且遇到了一个有趣的行为。当使用字符串Property函数并更改一些字符串时,构建开始中断。属性函数的奇怪行为

例如,我试图做这件事:

<PropertyGroup> 
    <BuildDependsOn>$(BuildDependsOn.Replace(";MyTarget", ""))</BuildDependsOn> 
</PropertyGroup> 

的MyTarget是,我想从BuildDependsOn属性中删除我的自定义的目标。当我试图做到这一点,字符串似乎被正确地删除,但我得到的错误,目标不存在(这发生在Visual Studio 2010和通过命令行MSBuild)。

错误是以下几点:

The target "EntityDeploy; 

      BeforeBuild; 
      CoreBuild; 
      AfterBuild" does not exist in the project. 

我想,一旦我修改此属性,MSBuild的停止解析其价值,并试图寻找一个目标(包括白色空格,回车和新换行):

"EntityDeploy; 

     BeforeBuild; 
     CoreBuild; 
     AfterBuild" 

并抛出一个错误,指出这样的目标不存在。

我也曾尝试进行一些更琐碎,了解更多关于这个问题,我试图执行此:

<PropertyGroup> 
    <BuildDependsOn>$(BuildDependsOn.Trim())</BuildDependsOn> 
</PropertyGroup> 

错误再次出现也与Trim()功能。 有什么想法?

编辑:我试图调试,并找出问题,我也碰到过这样的: 当Trim()方法的执行之前检查属性BuildDependsOn,它的价值是这样的:

Value "BuildDependsOn"="\r\n  EntityDeploy;\r\n  \r\n   BeforeBuild;\r\n   CoreBuild;\r\n   AfterBuild\r\n  \r\n " 
EvaluatedValue "\r\n  EntityDeploy;\r\n  \r\n   BeforeBuild;\r\n   CoreBuild;\r\n   AfterBuild\r\n  \r\n " string 
escapedValue "\r\n  EntityDeploy;\r\n  \r\n   BeforeBuild;\r\n   CoreBuild;\r\n   AfterBuild\r\n  \r\n " string 

Trim()方法,该属性值是这样的:

Value "BuildDependsOn"="EntityDeploy%3b\r\n  \r\n   BeforeBuild%3b\r\n   CoreBuild%3b\r\n   AfterBuild" 
EvaluatedValue "EntityDeploy;\r\n  \r\n   BeforeBuild;\r\n   CoreBuild;\r\n   AfterBuild" string 
escapedValue "EntityDeploy%3b\r\n  \r\n   BeforeBuild%3b\r\n   CoreBuild%3b\r\n   AfterBuild" string 

也许它是分号(; < =>%3b)正在打破构建?我怎样才能解决这个问题?

+0

也许这些链接可能会帮助你:http://stackoverflow.com/questions/5103026/in-msbuild-can-i-use-the-string-replace-function-on-a-metadata-item和这一个太http://msdn.microsoft.com/en-us/library/ms171458(v=vs.100).aspx – lnu 2012-04-05 06:59:58

+0

我知道这个OOB,但你不能做惯用的MSBuild的东西,并定义一个属性组与目标和一个出来,然后使用配置来选择哪一个被启用? – 2012-04-05 09:49:06

+0

@RitchMelton在正常情况下,我可以...事实上,我已经发布了我有的问题的简化版本。问题是,当我包含第三方.Targets文件时,第三方目标会自动添加到BuildDependsOn属性中。我想从这个属性中移除它(这样它就不会在构建项目时运行),并在另一个条件中决定是否应该执行目标。 – 2012-04-05 11:45:07

回答

4

我试过你所做的(向BuildDependsOn添加一个修剪)并且它正在工作。

<BuildDependsOn> 
    ConfigBeforeBuild; 
    $(BuildDependsOn.Trim()); 
    ConfigAfterBuild 
</BuildDependsOn> 

您是否为msbuild启用了调试?如果不是所有内容都解释为here

好吧,这里的解决方案(我不工作要么,我的测试是不正确的):

<BuildDependsOn> 
    $([MSBuild]::Unescape($(BuildDependsOn.Replace(";MyTarget", "")))) 
</BuildDependsOn> 

你要取消转义的字符串,以去除%3B。

+0

现在,这真的很奇怪,在我的环境中,肯定不工作......我已经更新了这个问题,并在调试过程中发现了一些结果......似乎分号在某种程度上破坏了构建,至少这就是我所拥有的发现作为一个唯一的区别... – 2012-04-05 08:14:12

+0

谢谢,它现在工作,Unescape正在完美... – 2012-04-10 06:05:38

+0

谢谢 - 我一直在争取几个小时这个确切的问题! – 2012-12-06 10:12:38