更改 msbuild.exe 的工作目录

发布于 2024-12-08 05:38:46 字数 940 浏览 1 评论 0原文

我正在从批处理文件执行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

盛夏尉蓝 2024-12-15 05:38:46

我在寻找问题的解决方案时遇到了这个问题。这是我的解决方案(构建脚本):

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

我相信这正是您最初想要的?

我的问题是我的批处理文件调用了另一个它期望位于同一目录中的文件,但由于我的 ms 构建脚本正在其他地方运行,因此该批处理文件无法找到第二个批处理文件。

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.

暖伴 2024-12-15 05:38:46

@jkohlhepp - 我现在明白了。在某种程度上,您所做的与我在评论中描述的相反。
MSBuild 常见目标使用 MSBuildProjectDirectory 来确定输出文件夹,除非您覆盖它。因此,在您的情况下,您可以运行

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

来强制将输出删除到该位置。

编辑:
鉴于上面的项目文件,您需要对其进行编辑以执行如下操作才能使其正常工作:

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

@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>  
又怨 2024-12-15 05:38:46

在当前版本的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文