WiX 4迁移至WiX 5后SourceDir失效,如何替代heat实现文件采集?
我承认自己有时难以查阅文档,且并非专业制作MSI安装包人员。我有一个包含多个项目的解决方案,其中一个是Installer安装项目,另一个是Windows服务项目Foo。
项目结构
\FooInstaller -> WiX here \Foo -> Dotnet core windows service here Foo.sln
原有WiX 4实现逻辑
安装项目中有SourcePaths.wxi文件,用于区分CI和本地构建的路径:
<Include xmlns="http://wixtoolset.org/schemas/v4/wxs"> <?ifndef $(env.CI)?> <?include SourcePaths.Local.wxi?> <?else?> <?if $(env.CI) != ""?> <?include SourcePaths.CI.wxi?> <?else?> <?include SourcePaths.Local.wxi?> <?endif?> <?endif?> </Include>
本地构建的SourcePaths.Local.wxi内容:
<Include xmlns="http://wixtoolset.org/schemas/v4/wxs"> <?warning This is NOT a CI build (local?)?> <?define FooSource = "SourceDir"?> <!-- 在CI变体中为绝对路径 --> </Include>
通过WiX4的heat工具采集生成组件,替换路径后示例如下:
<Component Id="cmpDufKoLsEXviYiwOW8uU0YqqkTrU" Directory="foodir" Guid="{BD9D7EF7-C97D-48DB-B0B6-1F5CBA3E2DE9}"> <File Id="filPesrMHNYqq.3oDZxmNT0UmP9XUI" KeyPath="yes" Source="$(FooSource)\appsettings.json" /> </Component>
这里$(FooSource)在本地构建时用SourceDir这个特殊值,能自动从项目引用的输出文件夹获取文件,CI构建时用绝对路径,两者都能正常工作。
WiX 5尝试及问题
为了减少自动生成文件数量,尝试使用WiX5的<Files>新特性,代码片段如下:
<Fragment> <?include SourcePaths.wxi?> <ComponentGroup Id="foofiles"> <Files Include="$(FooSource)\**"> <!-- Don't harvest the service because it needs manual authoring (below). --> <Exclude Files="$(FooSource)\foo-service.exe" /> </Files> </Fragment>
构建时出现警告:
warning WIX8601: Missing directory for harvesting files: Could not find a part of the path 'D:\Foo\FooInstaller\SourceDir'
显然WiX5把SourceDir当作字面路径处理,而该目录不存在。
疑问
- 如何解决这个路径解析问题?
- 是否只能继续使用WiX4的heat手动生成+编辑文件的方式?
- 原有的SourceDir特性为何在WiX5中失效?
- 如何配置bindpath来解决这个问题?
解决方案
1. SourceDir失效原因
WiX4中<File>元素的Source属性支持SourceDir作为特殊绑定路径,会自动关联项目引用的输出目录;但WiX5的<Files>元素是批量采集特性,编译时会直接解析字面路径,不再兼容SourceDir的特殊逻辑。
2. 替代方案:使用项目输出变量
如果Installer项目已引用Foo服务项目,可直接用WiX预定义的项目输出变量替代SourceDir:
修改SourcePaths.Local.wxi:
<Include xmlns="http://wixtoolset.org/schemas/v4/wxs"> <?warning This is NOT a CI build (local?)?> <?define FooSource = "$(var.Foo.TargetDir)"?> </Include>
$(var.Foo.TargetDir)会自动指向Foo项目的编译输出目录,本地和CI构建都能适配,<Files>元素可正常采集文件并排除指定exe。
3. 配置BindPath的方式
若需保留类似绑定路径的灵活逻辑,可在WiX项目中配置BindPath:
- 在Installer项目的
.wixproj文件中添加BindPath配置:
<ItemGroup> <BindPath Include="FooOutput"> <Path>$(SolutionDir)Foo\bin\$(Configuration)\$(TargetFramework)</Path> </BindPath> </ItemGroup>
- 修改
SourcePaths.Local.wxi中的FooSource定义:
<Include xmlns="http://wixtoolset.org/schemas/v4/wxs"> <?warning This is NOT a CI build (local?)?> <?define FooSource = "bindpath://FooOutput/"?> </Include>
WiX5会通过BindPath解析到对应的输出目录,避免将路径当作字面量处理。
4. 是否需要回到WiX4的heat方式
如果上述方案能满足需求,完全可以用WiX5的<Files>特性简化流程,无需再手动处理heat生成的文件。仅当有特殊自定义采集需求无法用<Files>实现时,才需要回到heat的方式。
内容的提问来源于stack exchange,提问作者Benjamin Podszun

