开发者

Change working directory of msbuild.exe

开发者 https://www.devze.com 2023-04-10 20:00 出处:网络
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 MSBuil

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, t开发者_如何学Pythonhe HelloWorld.txt file appears in the folder where the *.msbuild file is and not c:\temp.


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.


@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>  


In current versions of MSBuild the well-known property MSBuildStartupDirectory can be used in the msbuild file to retrieve the absolute path of the folder where MSBuild is called.

https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-reserved-and-well-known-properties?view=vs-2019

This option perhaps did not exist in msbuild around the time when the question was asked. I didn't want to spend too much time investigating it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号