Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change working directory of msbuild.exe

Tags:

msbuild

I am executing MSBuild from a batch file. The MSBuild script is in a different directory than the directory I want MSBuild to consider the working directory when running the script. When invoking MSBuild.exe, how do I change its working directory?

Edit: More details
Let's say I have an MSBuild script located on some other server. I want to run a command thusly:

msbuild.exe \\my_server\c$\My\Path\To\Scripts\TestScript.msbuild

I run that command with my command prompt at c:\temp. Let's say my TestScript.msbuild has a task to create a file. The file has no path just a filename. I would expect that the file gets created inside c:\temp. But it doesn't it gets created next to the msbuild file that is sitting on the server. This is the behavior I want to change.

Edit #2
Here is the script I'm using in my test:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">   
    <ItemGroup>
        <Files Include="HelloWorld.txt" />
    </ItemGroup>

    <Target Name="TouchFiles">
        <Touch Files="@(Files)" AlwaysCreate="True" />
    </Target>
</Project>

I am going into a command shell CDing into c:\temp and then executing the script. With or without the /p:OutDir switch that @Nick Nieslanik mentions, the HelloWorld.txt file appears in the folder where the *.msbuild file is and not c:\temp.

like image 735
RationalGeek Avatar asked Oct 04 '11 14:10

RationalGeek


2 Answers

@jkohlhepp - I see now. You are doing the opposite of what I described in my comment to some degree.
MSBuild common targets use the MSBuildProjectDirectory to determine the output folder unless you override that. So in your case, you could run

msbuild.exe \\my_server\c$\My\Pat\To\Scripts\TestScript.msbuild /p:OutDir=c:\temp 

to force the output to be dropped in that location.

EDIT:
Given the project file above, you'd need to edit it to do something like the following for this to work:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <OutDir Condition=" '$(OutDir)' == '' ">bin\debug\</OutDir>
  </PropertyGroup>
  <ItemGroup>  
    <!-- Without prefacing files with paths, they are assumed relative to the proj file -->
    <FilesToCreate Include="$(OutDir)HelloWorld.txt" />  
  </ItemGroup>  
  <Target Name="TouchFiles">  
     <Touch Files="@(FilesToCreate)" AlwaysCreate="True" />  
  </Target>  
</Project>  
like image 29
Nick Nieslanik Avatar answered Oct 19 '22 22:10

Nick Nieslanik


I ran across this while looking for a solution to my problem. Here's my solution (build script):

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="Default">
    <Exec Command="build.bat" WorkingDirectory="..\[your dir]\" />
  </Target>
</Project>

I believe that's more what you were originally looking for?

My problem was that my batch file called another that it expected to be in the same directory, but since my ms build script was being run elsewhere, the batch file failed to find the second batch file.

like image 185
Plausibly Deniable Avatar answered Oct 19 '22 21:10

Plausibly Deniable