Set Wix property to TFS build location

2019-03-20 02:55发布

问题:

I have been trying to find an answer to my question and could not find it; hence I will put the solution here. I hope it is helpful to others.

Problem:

I want my Wix project to build in TFS 2010 build process. As part of this, I want the source files location for my Wix to point to the build location of the TFS. For example, I want:

<File Id="ABC" KeyPath="yes" source="C:\Builds\1\MyBuild\assembly.dll" />

to be:

<File Id="ABC" KeyPath="yes" source="$(var.TFSLOCATION)\assembly.dll" />

The 'TFSLOCATION' is a wix property that needs to be populated with the location of TFS build. This needs to happen during the build process, where the build location path is passed to the Wix project.

Solution:

I read the following article:

http://www.ageektrapped.com/blog/setting-properties-for-wix-in-msbuild/

So this is what I did to my Wix project file (wixproj):

In order to set wix property from TFS MSBuild process the wix project file needs two changes:

<PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <ProductVersion>3.5</ProductVersion>
    <SourceLocation Condition="'$(SourceLocation)' == '' ">UNKNOWN</SourceLocation>
    <ProjectGuid>{cae7e273-2de5-4a60-9c4f-9da5f094caf5}</ProjectGuid>
    <SchemaVersion>2.0</SchemaVersion>
    <OutputName>N4S.MSO.BAM.Installer</OutputName>
    <OutputType>Package</OutputType>
    <WixTargetsPath Condition=" '$(WixTargetsPath)' == '' AND '$(MSBuildExtensionsPath32)' != '' ">$(MSBuildExtensionsPath32)\Microsoft\WiX\v3.x\Wix.targets</WixTargetsPath>
    <WixTargetsPath Condition=" '$(WixTargetsPath)' == '' ">$(MSBuildExtensionsPath)\Microsoft\WiX\v3.x\Wix.targets</WixTargetsPath>
    <SccProjectName>SAK</SccProjectName>
    <SccProvider>SAK</SccProvider>
    <SccAuxPath>SAK</SccAuxPath>
    <SccLocalPath>SAK</SccLocalPath>
  </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> 
  <OutputPath>bin\$(Configuration)\</OutputPath> 
  <IntermediateOutputPath>obj\$(Configuration)\</IntermediateOutputPath>
  <DefineConstants>LOCATION=$(SourceLocation)</DefineConstants>
</PropertyGroup>

In the above xml, please note the following two lines:

<SourceLocation Condition="'$(SourceLocation)' == '' ">UNKNOWN</SourceLocation>

<DefineConstants>LOCATION=$(SourceLocation)</DefineConstants>

The first line specifies a property 'SourceLocation' and sets it to a default 'UNKNOWN' value, if is is not set. The second line defines a constant called 'LOCATION' in the 'Release' configuration. The value of this constant is set to the value of 'SourceLocation' property.

Now, you need to make the following changes to your Product.wxs file (or whatever the name of your wxs file is).

  • Define a wix property first.
<?define TFSLOCATION="$(var.LOCATION)"?>
  • Now, update the File elements.
<File Id="ABC" KeyPath="yes" source="$(var.TFSLOCATION)\assembly.dll" />

TFS 2010 build template change

  • Open the TFS 2010 build template.
  • Look for a task 'Run MSBuild for Project'.
  • Open the properties for this task and go to 'CommandLineArguments' property.
  • Set the value for this property to:
String.Format("/p:SourceLocation={0}", BinariesDirectory)

Done

You now have a wix property populated from your TFS build process.

回答1:

If you set a reference in your wixproj file to the project(s) you are building you can reference their target paths. So if you have two projects MyProject and MyProjectInstaller, set a reference in MyProjectInstaller to MyProject.

Now in the product.wxs file your File elements would look like this:

<File Id='EXE' Name='$(var.MyProject.TargetDir)\MyProject.exe' />
<File Id='DLL' Name='$(var.MyProject.TargetDir)\MyProject.dll' />
...

The benefit to this is that the target dir is correct regardless of whether you're building locally or on a build server.



回答2:

Answering the question so it doesn't show up with no answers even though the answer is in the question.



回答3:

I ended up using the same approach, but with a few important improvements.

1) I passed both the SourcesDirectory and the BinariesDirectory from TFS build process template to MSBuild as separate properties, so that I have access to both of them.

2) So that the resulting MSBuild properties are available to every task executed in the MSBuild project, I added them to $(CustomPropertiesForBuild) in the BeforeBuild target.

3) Rrather than adding the DefineContants element to the PropertyGroup, I added a CreateProperty to the BeforeBuild target.

2 and 3 were done for the following reason having to do with running multiple Wix project solutions in a single TFS build. If you define the constant as originally suggested, two things can happen.

First, if you run heat.exe in your WiX project as part of a build that has multiple WiX projects, an issue can occur where the DevEnv holds onto the process handles and the constant is not redefined on each run unless you clean the Output folder and release the file handle.

Second, if for any reason one of your Wix projects does not get built (the configuration does not specify to build it or the specified configuration is invalid for the project) then for some reason, the MSBuild property gets reset to null and thus the constant is redefined as null, so you lose the property. If on the other hand, you define the property by overriding the BeforeBuild target, everything works properly.

Note that you have to override the BeforeBuild property, not the BeforeEndToEndIteration property, for this to work correctly.