在MSBuild中,我可以在MetaData项目上使用String.Replace函数吗? [英] In MSBuild, can I use the String.Replace function on a MetaData item?

查看:81
本文介绍了在MSBuild中,我可以在MetaData项目上使用String.Replace函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MSBuild v4中,可以在属性上使用函数(例如string.replace).但是如何在元数据上使用函数?

In MSBuild v4 one can use functions (like string.replace) on Properties. But how can I use functions on Metadata?

我想使用string.replace函数,如下所示:

I'd like to use the string.replace function as below:

<Target Name="Build">
        <Message Text="@(Files->'%(Filename).Replace(&quot;.config&quot;,&quot;&quot;)')" />
</Target>   

不幸的是,此输出为(不完全是我想要的): log4net.Replace(.config","); ajaxPro.Replace(.config","); appSettings.Replace(.config","); cachingConfiguration20.Replace(.config"," ); cmsSiteConfiguration.Replace(.config","); dataProductsGraphConfiguration.Replace(.config","); ajaxPro.Replace(.config","); appSettings.Replace(.config", "); cachingConfiguration20.Replace(.config","); cmsSiteConfiguratio

Unfortunately this outputs as (not quite what I was going for): log4net.Replace(".config","");ajaxPro.Replace(".config","");appSettings.Replace(".config","");cachingConfiguration20.Replace(".config","");cmsSiteConfiguration.Replace(".config","");dataProductsGraphConfiguration.Replace(".config","");ajaxPro.Replace(".config","");appSettings.Replace(".config","");cachingConfiguration20.Replace(".config","");cmsSiteConfiguratio

有什么想法吗?

推荐答案

您可以通过一些技巧来做到这一点:

You can do this with a little bit of trickery:

$([System.String]::Copy('%(Filename)').Replace('config',''))

基本上,我们调用静态方法"Copy"来创建一个新字符串(由于某些原因,如果您只是尝试按$('%(Filename)'.Replace('.config','')),则不喜欢它),然后在该字符串上调用replace函数.

Basically, we call the static method 'Copy' to create a new string (for some reason it doesn't like it if you just try $('%(Filename)'.Replace('.config',''))), then call the replace function on the string.

全文应如下所示:

<Target Name="Build">
        <Message Text="@(Files->'$([System.String]::Copy(&quot;%(Filename)&quot;).Replace(&quot;.config&quot;,&quot;&quot;))')" />
</Target>


编辑:MSBuild 12.0似乎破坏了上述方法.或者,我们可以向所有现有的Files项目添加新的元数据条目.我们在定义元数据项的同时执行替换,然后我们可以像访问其他任何元数据项一样访问修改后的值.


MSBuild 12.0 seems to have broken the above method. As an alternative, we can add a new metadata entry to all existing Files items. We perform the replace while defining the metadata item, then we can access the modified value like any other metadata item.

例如

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <ItemGroup>
        <Files Include="Alice.jpg"/>
        <Files Include="Bob.not-config.gif"/>
        <Files Include="Charlie.config.txt"/>
    </ItemGroup>

    <Target Name="Build">
        <ItemGroup>
            <!-- 
            Modify all existing 'Files' items so that they contain an entry where we have done our replace.
            Note: This needs to be done WITHIN the '<Target>' (it's a requirment for modifying existing items like this
            -->
            <Files>
                <FilenameWithoutConfig>$([System.String]::Copy('%(Filename)').Replace('.config', ''))</FilenameWithoutConfig>
            </Files>
        </ItemGroup>

        <Message Text="@(Files->'%(FilenameWithoutConfig)')" Importance="high" />
    </Target>
</Project>

结果:

D:\temp>"c:\Program Files (x86)\MSBuild\12.0\Bin\MSBuild.exe" /nologo test.xml
Build started 2015/02/11 11:19:10 AM.
Project "D:\temp\test.xml" on node 1 (default targets).
Build:
  Alice;Bob.not-config;Charlie
Done Building Project "D:\temp\test.xml" (default targets).

这篇关于在MSBuild中,我可以在MetaData项目上使用String.Replace函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆