自定义MSBuild Exec任务的系统环境变量路径 [英] Customize system environment variable Path for MSBuild Exec Task

查看:82
本文介绍了自定义MSBuild Exec任务的系统环境变量路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Exec Task调用在MSBuild过程中获取的批处理脚本.但是,脚本的位置不是路径系统环境变量的一部分.因此,我认为可以更新目标中的Path属性,然后触发执行任务:

I'm trying to trying to invoke a batch script that acquired during the MSBuild process using the Exec Task. However, the location of script is not part of the path system environment variable. So I figure I can update the Path property within the target and then trigger the Exec Task:

<Target Name="RestoreNPMPackages">
  <Message Text="$([System.DateTime]::Now.ToString(&quot;yyyy-MM-dd hh.mm.ss.fff&quot;)) Entering Build.xml Target RestoreNPMPackages..." Importance="high" />

  <PropertyGroup>
    <Path>$(Path);$(WorkspaceRoot)\Tools\$(Node_jsPackage)</Path>
  </PropertyGroup>

  <Message Text="Property Path in RestoreNPMPackages=$(Path)" Importance="high" />

  <Exec Command="$(Path)\npm install --no-color --no-optional" />

  <Message Text="$([System.DateTime]::Now.ToString(&quot;yyyy-MM-dd hh.mm.ss.fff&quot;)) Exiting Build.xml Target RestoreNPMPackages..." Importance="high" />

但是,我遇到以下错误

RestoreNPMPackages: 2015-07-27 06.31.24.334输入Build.xml目标RestoreNPMPackages ... RestoreNPMPackages中的属性路径= d:\ Delphi Projects \ Libraries; C:\ Windows \ Microsoft.NET \ Framework \ v4.0.30319; C:\ PROGRA〜1 \ Borland \ Delphi5 \ Projects \ Bpl; C:\ PROGRA〜1 \ Borland \ vbroker \ jre \ Bin; C:\ PROGRA〜1 \ Borland \ vbroker \ Bin; C:\ PROGRA〜1 \ Borland \ Delphi5 \ Bin; C:\ Windows \ system32; C:\ Windows; C:\ Windows \ System32 \ Wbem; C:\ Windows \ System32 \ WindowsPowerShell \ v1.0 \; C:\ Program Files(x86)\ Microsoft SQL Server \ 100 \ Tools \ Binn \; C:\ Program Files \ Microsoft SQL Server \ 100 \ Tools \ Binn \; C:\ Program Files \ Microsoft SQL Server \ 100 \ DTS \ Binn \; C:\ Program Files(x86)\ Microsoft SQL Server \ 100 \ Tools \ Binn \ VSShell \ Common7 \ IDE \; C :\\ Program Files(x86)\ Microsoft Visual Studio 9.0 \ Common7 \ IDE \ PrivateAssemblies \; C:\ Program Files(x86)\ Microsoft SQL Server \ 100 \ DTS \ Binn \; C:\ Program Files(x86)\ GNU \ GnuPG; C:\ Program Files \ Microsoft \ Web Platform Installer \; C:\ Program Files(x86)\ Microsoft ASP.NET \ ASP.NET Web Pages \ v1.0 \; C:\ Program Files \ Microsoft SQL Server \ 110 \ Tools \ Binn \; C:\ RealTick \; C:\ Program Files(x86)\ Graphviz 2.28 \ bin; D:\ PL ATFORM \ Tools \ Eze.Thirdparty.Node.js npm install --no-color --no-optional 'npm'不被识别为内部或外部命令, 可操作的程序或批处理文件.

RestoreNPMPackages: 2015-07-27 06.31.24.334 Entering Build.xml Target RestoreNPMPackages... Property Path in RestoreNPMPackages=d:\Delphi Projects\Libraries;C:\Windows\Microsoft.NET\Framework\v4.0.30319;C:\PROGRA~1\Borland\Delphi5\Projects\Bpl;C:\PROGRA~1\Borland\vbroker\jre\Bin;C:\PROGRA~1\Borland\vbroker\Bin;C:\PROGRA~1\Borland\Delphi5\Bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\;C:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files (x86)\GNU\GnuPG;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\RealTick\;C:\Program Files (x86)\Graphviz 2.28\bin;D:\PLATFORM\Tools\Eze.Thirdparty.Node.js npm install --no-color --no-optional 'npm' is not recognized as an internal or external command, operable program or batch file.

从消息"任务中,我可以看到文件夹D:\ PLATFORM \ Tools \ Eze.Thirdparty.Node.js已添加到Path变量,但是由于某种原因,它抱怨"npm"未被识别为内部或内部.外部命令

From the Message task I can see that the folder D:\PLATFORM\Tools\Eze.Thirdparty.Node.js has been added to the Path variable but for some reason it complains 'npm' is not recognized as an internal or external command

如果将文件夹D:\ PLATFORM \ Tools \ Eze.Thirdparty.Node.js添加到Windows中而不是MSBuild脚本中的Path变量中,该命令将正常运行.当然,提前设置Path变量听起来不太灵活.

If I add the folder D:\PLATFORM\Tools\Eze.Thirdparty.Node.js to the Path variable in Windows instead of in the MSBuild script, the command will work with no error. Of course it doesn't sound very flexible to set the Path variable ahead of time.

如何在MSBuild Exec任务中即时更新Path变量?谢谢

How can I make the on the fly update of the Path variable work in MSBuild Exec task? Thanks

推荐答案

已添加到它确实具有的Path变量中,但是Path是MsBuild进程中的一个属性,但不是与Exec任务使用的环境变量相同.您可以验证以下内容:

has been added to the Path variable it sure has, but Path is a property within the MsBuild process and that is not the same as an environment variable used by the Exec task. You can verify this:

<Exec Command="echo %PATH%"/>

将打印出Exec使用的PATH,并且不会包含您的更改,因为MsBuild在使用Exec时会启动一个单独的cmd进程,并且不会将环境变量传递给它.

will print the PATH used by Exec and it will not contain your changes because MsBuild launches a seperate cmd process when using Exec and does not pass environment variables to it.

此外,您执行npm的命令是错误的:$(Path)\npm评估您在问题中显示的所有内容,后跟\npm(类似于 d:\ Delphi Projects \ Libraries; C:\ Windows \ Microsoft.NET \ Framework \ v4.0.30319; C:\ PROGRA〜1 \ Borland \ Delphi5 \ Projects \ Bpl; C:\ PROGRA〜1 \ Borland \ vbroker \ jre \ Bin; C:\ PROGRA〜1 \ Borland \ vbroker \ Bin; C:\ PROGRA .... \ npm ),因此可能无法纠正

Furthermore your command for executing npm is wrong: $(Path)\npm evaluates to everything you show in your question followed by \npm (something like d:\Delphi Projects\Libraries;C:\Windows\Microsoft.NET\Framework\v4.0.30319;C:\PROGRA~1\Borland\Delphi5\Projects\Bpl;C:\PROGRA~1\Borland\vbroker\jre\Bin;C:\PROGRA~1\Borland\vbroker\Bin;C:\PROGRA....\npm) so that cannot possibly correct

由于您知道npm在哪里,您应该直接调用它:

Since you know where npm is you should just invoke it directly:

<Exec Command="$(WorkspaceRoot)\Tools\$(Node_jsPackage)\npm"/>

如果npm由于某种原因要求将其所在的目录添加到PATH,则只需按照在命令行上执行的操作即可:(set PATH=...) & npm.为执行此操作,您需要使用&amp转义&:

If for some reason npm requires the directory where it is located to be added to the PATH then just do that as you would on the command line: (set PATH=...) & npm. To do this for exec you need to escape the & using &amp:

<Exec Command="(set PATH=$(Path)) &amp; npm" />

在您的问题中修改了Path的位置. 此处为例.

Where Path is modified as in your question. More explanation here for instance.

这篇关于自定义MSBuild Exec任务的系统环境变量路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆