更改 msbuild.exe 的工作目录
我正在从批处理文件执行 MSBuild。 MSBuild 脚本所在的目录与我希望 MSBuild 在运行脚本时考虑工作目录的目录不同。调用MSBuild.exe时,如何更改其工作目录?
编辑:更多详细信息
假设我有一个位于其他服务器上的 MSBuild 脚本。我想这样运行一个命令:
msbuild.exe \\my_server\c$\My\Path\To\Scripts\TestScript.msbuild
我使用命令提示符在 c:\temp 运行该命令。假设我的 TestScript.msbuild 有一个创建文件的任务。该文件没有路径,只有文件名。我希望该文件是在 c:\temp 内创建的。但它并不是在服务器上的 msbuild 文件旁边创建的。这就是我想要改变的行为。
编辑#2
这是我在测试中使用的脚本:
<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>
我将进入命令 shell CDing 到 c:\temp,然后执行该脚本。无论是否使用 @Nick Nieslanik 提到的 /p:OutDir 开关,HelloWorld.txt 文件都会出现在 *.msbuild 文件所在的文件夹中,而不是 c:\temp 中。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在寻找问题的解决方案时遇到了这个问题。这是我的解决方案(构建脚本):
我相信这正是您最初想要的?
我的问题是我的批处理文件调用了另一个它期望位于同一目录中的文件,但由于我的 ms 构建脚本正在其他地方运行,因此该批处理文件无法找到第二个批处理文件。
I ran across this while looking for a solution to my problem. Here's my solution (build script):
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 - 我现在明白了。在某种程度上,您所做的与我在评论中描述的相反。
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
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:
在当前版本的 MSBuild 中,可以在 msbuild 文件中使用众所周知的属性
MSBuildStartupDirectory
来检索调用 MSBuild 的文件夹的绝对路径。https: //learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-reserved-and-well-known-properties?view=vs-2019
这个在提出问题时,msbuild 中可能不存在该选项。我不想花太多时间去调查它。
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.