Duan Bailey
Duan Bailey

Reputation: 58

Can I specify the publish target in an SDK style WiX project?

I'm attempting to create an installer using WiX4. I was able to successfully add a .wixproj to the solution using Heatwave, and I added ProjectReference tags for the relevant projects that I want to package. I want to be able to build from Visual Studio and also from the command line.

When I run the build, it appears to call dotnet build. For the dependent projects, I actually want the output of a publish, so currently I am working around this by creating a custom target that runs before the build and explicitly call publish using the Exec Command element.

However, I'm wondering if there is a better way to do this? I suspect that currently the build is happening twice, once for my custom build target and once during the build target.

Here's an example of what I have:

<Project Sdk="WixToolset.Sdk/4.0.0">
  <ItemGroup>
    <ProjectReference Include="MyProject.csproj" Platforms="Any CPU" />
  </ItemGroup>
  <ItemGroup>
    <HarvestDirectory Include="MyProject\bin\$(Configuration)\net7.0\publish">
      <ComponentGroupName>ComponentGroup</ComponentGroupName>
      <DirectoryRefId>INSTALLFOLDER</DirectoryRefId>
      <SuppressRootDirectory>true</SuppressRootDirectory>
    </HarvestDirectory>
    <BindPath Include="MyProject\bin\$(Configuration)\net7.0\publish" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="WixToolset.Heat" Version="4.0.1" />
      <PackageReference Include="WixToolset.Util.wixext" Version="4.0.1" />
  </ItemGroup>
  <Target Name="CustomBeforeBuild" BeforeTargets="BeforeBuild">
    <Exec Command="rd /s /q MyProject\bin\$(Configuration)\net7.0\publish" />
    <Exec Command="dotnet publish MyProject -c $(Configuration)" />
  </Target>
</Project>

Is there a way to specify that I want to invoke dotnet publish on MyProject instead of dotnet build, using Msbuild, so that I can get rid of the CustomBeforeBuild target?

Upvotes: 3

Views: 1852

Answers (1)

vdschuck
vdschuck

Reputation: 166

Inside your WixProj you add an item group to reference your project, in this reference, you add the Publish="true" attribute.

<ItemGroup>
    <ProjectReference Include="..\YourProject\YourProject.csproj" Publish="true" />
</ItemGroup>

Let me share with you an example of how I use it

<Project Sdk="WixToolset.Sdk/4.0.2">
  <PropertyGroup>
    <OutputName>App-Test</OutputName>
    <DefineConstants>
        ProductVersion=$(Version);
    </DefineConstants>
  </PropertyGroup>
  <ItemGroup>
    <HarvestDirectory Include=".\obj\$(Configuration)\publish\YourProject">
      <ComponentGroupName>HarvestedComponents</ComponentGroupName>
      <DirectoryRefId>INSTALLFOLDER</DirectoryRefId>
      <SuppressRootDirectory>true</SuppressRootDirectory>
    </HarvestDirectory>
    <BindPath Include=".\obj\$(Configuration)\publish\YourProject" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="WixToolset.Heat" Version="4.0.0" />
    <PackageReference Include="WixToolset.UI.wixext" Version="4.0.0" />
  </ItemGroup>
 <ItemGroup>
   <ProjectReference Include="..\YourProject\YourProject.csproj" Publish="true" />
 </ItemGroup>
</Project>

Upvotes: 7

Related Questions