MSBuild:使用复制任务

时间:2015-12-04 20:18:37

标签: visual-studio build msbuild tfs2010 msbuild-task

我有一组客户特定的配置文件,这些文件在构建时位于单个Release文件夹中。文件名类似于:

  • CustomerA_InstanceConfigurationX.config
  • CustomerA_InstanceConfigurationY.config
  • CustomerB_InstanceConfigurationX.config
  • CustomerB_InstanceConfigurationY.config

......等等。

在构建期间,我想将客户特定配置文件复制到客户特定的Binaries文件夹中:

  • $(BuildDirectory)\二进制\安装程序\ CustomerA \产品名称\
  • $(BuildDirectory)\二进制\安装程序\ CustomerB \产品名称\

所以CustomerA_InstanceConfigurationX.config和CustomerA_InstanceConfigurationY.config将进入$(BuildDirectory)\ Binaries \ Installers \ CustomerA \ ProductName \等等。

如何设置SourceFiles和DestinationFolder属性以实现此目的?

我将客户列表作为实例属性的元素,并在其周围设置SourceFiles和DestinationFiles:

<ItemGroup>
  <ConfigFilesToCopy Include="$(BuildDirectory)\stage\InstallerDev\%(Instance.Customer)\Setup\bin\Release\%(Instance.Customer)_*.*" />
  <DestionationsForConfigFiles Include="$(BuildDirectory)\Binaries\Installers\%(Instance.Customer)\InstallerDev\" />
</ItemGroup>

<Copy SourceFiles="@(ConfigFilesToCopy)" DestinationFolder="%(DestionationsForConfigFiles.FullPath)" />

这只是将所有客户.config文件复制到 all 客户特定的Binaries文件夹中。

使用消息任务%(Instance.Customer)输出:

  • CustomerA
  • CustomerB
  • CustomerC。

%(ConfigFilesToCopy.Identity)输出:

  • “C:\ Builds \ AgentX \ YYY \ INSTALLER_DEV Build \ stage \ InstallerDev \ CustomerA \ Setup \ bin \ Release \ CustomerA_InstanceX.config”
  • “C:\ Builds \ AgentX \ YYY \ INSTALLER_DEV Build \ stage \ InstallerDev \ CustomerA \ Setup \ bin \ Release \ CustomerA_InstanceY.config”
  • “C:\ Builds \ AgentX \ YYY \ INSTALLER_DEV Build \ stage \ InstallerDev \ CustomerB \ Setup \ bin \ Release \ CustomerB_InstanceX.config”
  • “C:\ Builds \ AgentX \ YYY \ INSTALLER_DEV Build \ stage \ InstallerDev \ CustomerB \ Setup \ bin \ Release \ CustomerB_InstanceY.config”

%(DestionationsForConfigFiles.Identity)输出:

  • C:\ Builds \ Agent8 \ Five0 \ INSTALLER_DEV Build \ Binaries \ Installers \ CustomerA \ InstallerDev \
  • C:\ Builds \ Agent8 \ Five0 \ INSTALLER_DEV Build \ Binaries \ Installers \ CustomerB \ InstallerDev \
  • C:\ Builds \ Agent8 \ Five0 \ INSTALLER_DEV Build \ Binaries \ Installers \ CustomerC \ InstallerDev \

如果某人可以提供一些帮助来实现这一目标或者采用其他方法,那就太好了。 (例如,我可以将客户特定的配置文件重新组织到客户特定的文件夹中。)非常感谢!

[** 更新注释 **:目前,我将每个客户名称硬编码为ConfigFilesToCopy列表项以及DestinationsForConfigFiles项。

  <ConfigFilesToCopy Include="$(BuildDirectory)\stage\InstallerDev\CustomerA\Setup\bin\Release\CustomerA_*.*">
    <DestionationsForConfigFiles>$(BuildDirectory)\Binaries\Installers\CustomerA\InstallerDev\</DestionationsForConfigFiles>
  </ConfigFilesToCopy>

  <ConfigFilesToCopy Include="$(BuildDirectory)\stage\InstallerDev\CustomerB\Setup\bin\Release\CustomerB_*.*">
    <DestionationsForConfigFiles>$(BuildDirectory)\Binaries\Installers\CustomerB\InstallerDev\</DestionationsForConfigFiles>
  </ConfigFilesToCopy>

这很有效,但我基本上想知道是否可以在没有明确使用客户名称的情况下执行相同的操作,这样我每次添加新客户时都不必维护此列表

1 个答案:

答案 0 :(得分:0)

制作文件的目标元数据(我改变了路径,使其更适合SO的代码框,但想法是一样的):

<Target Name="Copy">
  <ItemGroup>
    <ConfigFilesToCopy Include="$(SomeDir)\%(Instance.Customer)\%(Instance.Customer)_*.*">
      <DestionationsForConfigFiles>$(SomeDir)\Binaries\%(Instance.Customer)</DestionationsForConfigFiles>
    </ConfigFilesToCopy>
  </ItemGroup>

  <Message Text="Source=%(ConfigFilesToCopy.Identity) Dest=%(ConfigFilesToCopy.DestionationsForConfigFiles)" />
</Target>