views:

2130

answers:

2

Hi folks,

I have two questions re: visual studio 2008 and post-build events.

1) How do i dynamically list the msbuild.exe full path, to be called by the post-build event? Currently, i have the following (which works beautifully, btw):-

C:\Windows\Microsoft.NET\Framework64\v3.5\msbuild.exe 
    "$(ProjectDir)MSBuild\MSBuildSettings.xml"

.. but that would only work if u have a x64 bit environment. Is there a way to use some built in magic setting? eg. $(MsBuildPath)msbuild.exe "blah....xml" ??

2) My msbuild task does some stuff then generates a txt file as the output. I define the output path in the msbuild xml file as such..

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/MsBuild/2003"&gt;
    <UsingTask TaskName="CustomTask" AssemblyFile="MyAssembly.dll"/>
    <Target>
        <CustomTask
           ...
           OutputFile="Foo.txt"
        />
    <Target>
</Project>

How can i pass in the real output folder, to the msbuild file? I tried...

OutputFile="$(OutDir)Foo.txt"

but that failed :(

Cheers!

Update #1 - Answer to the second question..

I've figured out a way to answer the second question -- not sure if it's the best one though .. so I'm open to other ideas :)

XML file changes :: add a property group that sets the internal variable name IF no outside arguments were passed to the msbuild.exe executable.

    <PropertyGroup>
        <OutputFile Condition=" '$(OutputFile)'=='' ">
            ..\SomeFolder\Foo.txt</OutputFile>
    </PropertyGroup>

    <Target>
        <CustomTask
           ...
           OutputFile="$(OutputFile)"
        />
    <Target>
</Project>

Now, call the msbuild executable like ...

msbuild.exe "C:\Temp Foo\Blah\MsbuildSettings" /p:OutputFile="C:\Folder 1\Folder 2\blah\"

and that works :)

A: 

You could put your custom task inside the vbproj (csproj if it's a c# project) file and then have the post-build event use the VS-defined MSBuild variables. I'm thinking of this process off the top of my head, so bear with me on this, lol.

  1. In Visual Studio, unload the project you want to have the post-build event with, and then right-click on the unloaded project node and select 'Edit...'.
  2. Put the UsingTask at the top, just like what you have.
  3. Reload the project, and open up the project properties and go to edit the post-build event there.
  4. You should be able to access all those nifty variables now, and add them to your custom task.

Project files are MSBuild scripts in themselves, allowing you to do all sorts of nifty MSBuild scripting tricks.

Edit: Sorry, I don't have an answer for the first question you asked.

arabian tiger
hmm. interesting :) I've figured out another way also (while you were adding your answer). Check out my addition to the opening post.
Pure.Krome
That is one way for solving #2, where you'd just have to update the property variable in one location should the path change.Meanwhile, for answering #1, if you use the VS 2005 (or 2008) Cross x64 Command Prompt for running your script, all you need to do is type in MSBuild <your MSBuild file> and you'll be set.
arabian tiger
Actually, for #1, Si answered it correct, below :) works perfectly!
Pure.Krome
+1  A: 

For question 1, use property MSBuildBinPath

For question 2, use property OutputPath (C# or VB projects)

OutputFile="$(OutputPath)Foo.txt"
Si
hmm. can u elaborate on this output path variable? I've also updated my initial post .. was that the same thing?
Pure.Krome
Have a read of your csproj file (assuming C# here), you will see that each build type (debug|release) for each platform (AnyCPU|etc) has a property group based on the conditions, inside that property group there is always an OutputPath property defined, which is where the assemblies are generated and any content you mark as "Copy to output directory".
Si