MSBuild合并项目组 [英] MSBuild merge item groups
问题描述
需要有关此MSBuild代码的帮助.
Need some help with this MSBuild code.
我想生成4个具有不同设置的app.config文件,并创建2个用于质量检查和生产的安装文件.
I want to generate 4 app.config files with different settings and create 2 setup files for QA and production.
每个安装文件将具有2个物理安装(生产线).
Each setup file will have 2 physical installations (Production lines).
因此,质量检查设置应包括2个app.config,分别对生产线1和2进行质量检查设置,对于生产设置也是如此.
So QA setup should include 2 app.configs with qa settings for production line 1 and 2, the same for production setup.
这是我到目前为止拥有的msbuild的一部分.
Here is a extract of the msbuild I have so far.
<ItemGroup>
<BuildEnvironment Include="QA">
<Server>qa-server</Server>
<BuildEnvironment/>
<BuildEnvironment Include="Prod">
<Server>prod-server</Server>
<BuildEnvironment/>
<Line Include="1">
<Setting>A</Setting>
</Line>
<Line Include="2">
<Setting>B</Setting>
</Line>
<ItemGroup>
<Target Name="PublishSetup" Inputs="@(BuildEnvironment)" Outputs="%(BuildEnvironment.Identity)">
<!-- Doesn't work at all -->
<ItemGroup>
<AppConfig Include="@(BuildEnvironment);@(Line)">
<Path>$(MyOutDir)\App.Config-%(Identity)</Path>
</AppConfig>
</ItemGroup>
<!-- Copy app.config to the four new files -->
<Copy SourceFiles="$(AppConfigFile)" DestinationFiles="%(AppConfig.Path)" />
<!-- Update each new app.config with XmlUpdate (community task), something like the following -->
<XmlUpdate XmlFileName="%(AppConfig.Path)" XPath=".." Value="%(AppConfig.Server)" />
<XmlUpdate XmlFileName="%(AppConfig.Path)" XPath=".." Value="%(AppConfig.Setting)" />
<!-- Build 2 setup.exe, one for qa and one prod using a Exec-task passing in qa and prod as command line argument -->
<Exec Command="setupcompiler.exe /d%(BuildEnvironment.Identity)" />
</Target>
生成的4个app.configs应该是这样的
The 4 resulting app.configs should be like this
app.config-QA-1
<connectionstring datasource="qa-server" ../>
<applicationSetting name="aName" value="A" />
app.config-QA-2
<connectionstring datasource="qa-server" ../>
<applicationSetting name="aName" value="B" />
app.config-Prod-1
<connectionstring datasource="prod-server" ../>
<applicationSetting name="aName" value="A" />
app.config-Prod-2
<connectionstring datasource="prod-server" ../>
<applicationSetting name="aName" value="B" />
推荐答案
该想法是首先构建一个跨产品",即一个包含4种组合的ItemGroup.可以通过组合两个组的@和%来完成,如此处所示.然后,在第二步中,基于现有元数据用额外的元数据填充ItemGroup(添加元数据只是再次声明该组并添加元数据).这有点棘手,因为从Line
你们都想要Identity
和Setting
-我不知道这样做的一种不错的msbuild方式,所以我诉诸于用Identity | Setting构建一个字符串,然后在|以后.
The idea is to first build a 'cross-product', an ItemGroup containing the 4 combinations. Can be done by combining @ and % for the two groups, as shown here. Then in a second step populate the ItemGroup with extra metadata based on existing metadata (adding metadata is just declaring the group again and adding metadata). It's a bit tricky here, because from Line
you both want Identity
and Setting
- I don't know a nice msbuild way of doing this so I resorted to building a string with Identity|Setting, then splitting on the | later on.
<Target Name="PublishSetup">
<ItemGroup>
<AppConfig Include="@(BuildEnvironment)">
<Mod>%(Line.Identity)|%(Line.Setting)</Mod>
</AppConfig>
<AppConfig>
<Line>$([System.String]::Copy('%(Mod)').Split('|')[0])</Line>
<Setting>$([System.String]::Copy('%(Mod)').Split('|')[1])</Setting>
</AppConfig>
<AppConfig>
<Path>app.config-%(Identity)-%(Line)</Path>
</AppConfig>
</ItemGroup>
<Message Text="Path=%(AppConfig.Path) Server=%(AppConfig.Server) Setting=%(AppConfig.Setting)" />
</Target>
输出:
Path = app.config-QA-1服务器= qa服务器设置= A
路径= app.config-Prod-1服务器=产品服务器设置= A
路径= app.config-QA-2服务器=质量保证服务器设置= B
Path = app.config-Prod-2 Server = prod-server Setting = B
Path=app.config-QA-1 Server=qa-server Setting=A
Path=app.config-Prod-1 Server=prod-server Setting=A
Path=app.config-QA-2 Server=qa-server Setting=B
Path=app.config-Prod-2 Server=prod-server Setting=B
这篇关于MSBuild合并项目组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!