MSBuild-子目录中所有bin目录的ItemGroup

问题描述:

我的解决方案"有多个项目(因此还有子目录),并且每个项目文件夹中都有一个"bin"文件夹.

My Solution has multiple projects (and therefore subdirectories), and there is a 'bin' folder in each project folder.

我正在尝试在包含所有这些目录的MSBuild脚本中创建一个ItemGroup.

I'm trying to create an ItemGroup in my MSBuild script that includes all these directories.

我认为这足够了,但其中不包含任何内容:

I thought this would be sufficient, but it doesn't contain anything:

<ItemGroup>
  <BinDirs Include="**\bin" />
</ItemGroup>

我不确定为什么这行不通.谁能指出我正确的方向来实现我的目标?

I'm not sure why this doesn't work. Can anyone point me in the right direction to achieve what I'm trying to do?

关于, 尼克

由于没有答案,因此在Google搜索结果列表中排名很高:

Since this hasn't got an answer, yet comes high on the list of Google results:

Alexey提供的链接提供了解决此问题的几种答案,但是不清楚您给出的示例为什么不起作用.

The link provided by Alexey has several answers to work around this problem, but it's not obvious why the example you've given doesn't work.

在定位目录时,MSBuild ItemGroup集合似乎不喜欢通配符转换.

MSBuild ItemGroup collections don't seem to like wildcard Transforms when targeting directories.

您可以使用显式路径,例如

You can use explicit paths, e.g.

<ItemGroup>
  <BinDirs Include="C:\MyProject\bin" />
</ItemGroup>

或相对于您的构建脚本运行位置的路径,例如

Or paths relative to where your build script is running, e.g.

<ItemGroup>
  <BinDirs Include="..\MyProject\bin" />
</ItemGroup>

但是,除非您要定位文件,否则它不会转换通配符.

However it does not transform your wildcards unless you are targeting files, e.g.

<ItemGroup>
  <ThisWorks Include="..\**\bin\*" />
  <ThisDoesnt Include="..\**\bin" />
</ItemGroup>

该帖子包含使用通配符选择文件夹的几种方法,我倾向于使用的一种方法是:

That post contains several ways to select folders using wildcards, the one I tend to use is:

<ItemGroup>
  <GetAllFiles Include="..\**\bin\*.*" />
  <GetFolders Include="@(GetAllFiles->'%(RootDir)%(Directory)'->Distinct())" />
</ItemGroup>

如文章所述,选择根文件夹并不完美,因为它必须找到文件所在的位置.仅当文件位于其中时,使用bin *.*才能获取bin文件夹.

As noted on the post, it's not perfect at selecting the root folders, as it has to find where there are files. Using bin*.* would only get the bin folder if files were located in it.

如果您的构建类似于标准VS输出,则可能会发现bin文件夹中没有文件,而是具有基于您的配置名称的目录,例如bin \ Debug,在这种情况下,定位bin \ ** \ *将导致您的项目组包含这些文件夹.

If your build is anything like a standard VS output, you will probably find your bin folder has no files, instead having directories based on your configuration names, e.g. bin\Debug, in which case targeting bin\**\* will result in your item group containing those folders.

例如

<ItemGroup>
  <GetAllFiles Include="..\**\bin\**\*" />
  <GetFolders Include="@(GetAllFiles->'%(RootDir)%(Directory)'->Distinct())" />
</ItemGroup>

会得到:

  • .. \ Proj1 \ bin \ Debug
  • .. \ Proj1 \ bin \ Release
  • .. \ Proj2 \ bin \ Debug
  • .. \ Proj2 \ bin \ Release

我还不知道使用通配符方式来获取不带文件的bin文件夹....如果有人找到了,请发贴,因为这样会有用.

I don't know of a wildcard way to get bin folders without files in... yet. If anybody finds one, please post as it would be useful.

希望这可以帮助某人节省一些时间.

Hope this helps someone save some time.