问题描述
我正在从批处理文件执行MSBuild. MSBuild脚本位于与我希望MSBuild在运行脚本时考虑工作目录的目录不同的目录.调用MSBuild.exe时,如何更改其工作目录?
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?
更多详细信息
假设我有一个位于其他服务器上的MSBuild脚本.我要这样运行命令:
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
我在c:\ temp的命令提示符下运行该命令.假设我的TestScript.msbuild有创建文件的任务.该文件没有路径,只有文件名.我希望在c:\ temp中创建文件.但是它不是在位于服务器上的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.
编辑#2
这是我在测试中使用的脚本:
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>
我要进入命令外壳CD,将其刻录到c:\ temp中,然后执行脚本.带有或不带有@Nick Nieslanik提到的/p:OutDir开关,HelloWorld.txt文件显示在* .msbuild文件所在的文件夹中,而不是c:\ temp.
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.
推荐答案
@jkohlhepp-我现在看到了.在某种程度上,您所做的与我在评论中所描述的相反.
MSBuild通用目标使用MSBuildProjectDirectory来确定输出文件夹,除非您将其覆盖.因此,就您而言,您可以运行
@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.
给定上面的项目文件,您需要对其进行编辑以执行以下操作:
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>
这篇关于更改msbuild.exe的工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!