How to create a new property in MSBuild and reference it in WIX
Asked Answered
G

2

7

We need to create a property to indicate our software version. Then we want to use it inside our WIX project, i.e., reference it in wxs file. We don't want to define it in wxs file because we want the MSBuild to rename output file based on this version number too. A definition of constants in PropertyGroup is not a proper place for us and here is the reason:

The properties defined in the PropertyGroup is Configuration/Platform specific. Whenever we modify this preprocessor in Visual Studio IDE from project properties window, normally we only modify the value for a specific Configuration/Platform combination. (I know it is possible to modify it for All Config/Platform in IDE, but it is actually done by making copies to all combinations. And it is still possible we ruin the synchronization by modifying a value for only one combination. For example, by default when we open the build tab of project properties window, it will show the active Config/Platform). On the other hand, even though we can define a PropertyGroup without any condition, whenever we modify it in the IDE, we are normally only modifying it for the specific combination, not all of them. Since we are purely building our SW in VS IDE, it will be hard to maintain and prone to problem.

I tried to use CreateProperty task of MSBuild inside BeforeBuild target, but it seems in the following execution the value will not be effective at all. For example, if I overwrite an existing property in BeforeBuild, and when I reference it in WIX, it will still use the old value. And if I create a totally new property, WIX is complaining undefined preprocessor.

Is there a proper way to do that: create a MSBuild property and use it inside WIX?

Giacobo answered 6/11, 2012 at 11:31 Comment(1)
What do you mean by overwriting an existing property? Can you show the msbuild?Cotta
C
12

You don't need to use BeforeBuild. This definitely works on wixproj.

my.properties

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
  <PropertyGroup>
    <DefineConstants>$(DefineConstants);foo=bar</DefineConstants>
  </PropertyGroup>
</Project>

updated wixproj

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- all other stuff -->
<Import Project="my.properties" />
</Project>

Wix

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Product Id="*" Name="SetupProject1$(var.foo)" Language="1033" Version="1.0.0.0" Manufacturer="$(var.foo)" UpgradeCode="863d8da1-422b-4b28-aa68-56e3190770d7">
Cotta answered 6/11, 2012 at 12:44 Comment(7)
thanks, I am almost there. Now my only problem is how to use this constant inside wixproj file? Because the target is to use it for MSBuild to rename the output, so how can I reference foo inside .wixproj? I tried $(foo), which would work if I directly define a property, but it doesnä't work with DefineConstantsGiacobo
Set the $(OutputName) property. But you need to do it earlier than at the end of the project. Move the Import element right after the configuration-specific property groups. <OutputName>$(OutputName)-somethingelse</OutputName>Cotta
I am sorry but this doesn't seem to work. It just simply complains preprecessor foo is not defined. But I have done it in another way: I use a MSBuild task to automatically read the version number from one xml file which we maintain and write this value to WIX file directly. That way we only need to define the version one place(in the xml file) and keep using our current WIX file.Giacobo
It works for me. I'm using Wix 3.6, the equivalent for other versions might be slightly different but the underlying concepts are the same. It's not great to update the file, it might mess up up to date checks and interfere with source control, but if that works for you that's great. If you're using a different version of Wix you may want to look at the name of the MSBuild property that holds the constants.Cotta
I was also using Wix3.6, although I tried with 3.7, neither worked. And the really weird thing is at first I thought it worked, but only to find out it was because the preprecessor got secretly created and hence it was available. By "secretly" I mean I couldn't find the definition in .wixproj file, nor any source file visible in VS. I swear I searched all the files but couldn't find it. I am not an expert of MSBuild, so I don't know where else it could get the value. And what makes it worse is everything time I deleted the definition from IDE, it got created again next time I loaded the windowGiacobo
I suggest you try bumping up the msbuild verbosity and check what parameters are used for Candle and Light. There is a wix targets file somewhere under program files (check the import statement in your wixproj and look for that file). You should be able to track the properties and items used as parameters and update accordingly. But I still don't understand what didn't work for you; could you elaborate/provide samples of your updates to the wixproj file?Cotta
That solution certainly works. The only catch is that the my.properties could not have properties from other projects, like a property sheet.Lalo
H
0

Please check preprocessor variables here. I'm not sure actually if you can reference project properties other than standard, but looks like it is possible.

Hairsplitting answered 6/11, 2012 at 12:27 Comment(3)
Thanks for your reply. But what is possible? Basically my question is whether it is possible to create a preprocessor in MSBuild, and use this value in WIX. Because it seems that the manipulation I did in BeforeBuild target of MSBuild won't take effect for the further execution of WIX. If I change the value of one pre-defined preprocessor, in the execution of WIX it is still the old value. If I create a new one, it won't be available at all for WIX. And use DefineConstants in PropertyGroup is also not what I want.Giacobo
If you have old value, please check the project build order (right-click on solution file > Build order). Maybe your installer isn't dependent on the project you are referencing and installer is building before it. If so, you can add custom dependency in Project Depenencies viewHairsplitting
This is the installer project, i.e., I can't use the new value I set at wixproj file for my wxs file.Giacobo

© 2022 - 2024 — McMap. All rights reserved.