Set up system environment variable Path for MSBuild Exec task

I am trying to call a batch script that was received during an MSBuild process using Exec Task. However, the location of the script is not part of the path system environment variable. So I figure I can update the Path property on the target and then run 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" />

      

However I am getting the following error

RestoreNPMPackages: 2015-07-27 06.31.24.334 Enter Build.xml Target RestoreNPMPackages ... Properties 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: \ PL ATFORM \ Tools \ Eze.Thirdparty.Node.js npm install --no-color --no-optional "npm" is not recognized as an internal or external command, runtime program, or batch file.js npm install --no-color --no-optional "npm" is not recognized as an internal or external command, runtime program, or batch file.js npm install --no-color --no-optional "npm" is not recognized as an internal or external command, runtime program, or batch file.

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

If I add the D: \ PLATFORM \ Tools \ Eze.Thirdparty.Node.js folder to the Path variable on Windows and not to the MSBuild script, the command works without error. Of course, it's not very flexible to set up the Path variable ahead of time.

How can I update updating Path variable on the fly in MSBuild Exec task? thank

+3


source to share


2 answers


was added to the Path variable, which it necessarily has, but Path

is a property in the MsBuild process and is not the same as the environment variable used by the Exec task. You can check this:

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

      

will print the PATH used by Exec and it won't contain your changes because MsBuild starts a separate cmd process when using Exec and doesn't pass environment variables to it.

Also, your command to execute npm is wrong: $(Path)\npm

evaluates whatever you show in your question and then \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 you can't fix

Since you know where npm is, you should just call it directly:



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

      

If for any reason npm requires that the directory in which it was added, was added to the PATH, then just do it, as in the command prompt (set PATH=...) & npm

. To do this for exec you need to avoid &

with &amp

:

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

      

Where Path

changes like in your question. More explanation here, eg .

+6


source


I found the actual solution to this problem at [Jeff Hardy Blog Post]. You basically set PATH on the command line. or

<PropertyGroup>
  <PythonExec><![CDATA[
set PYTHONPATH=C:\Foo
set FOO=42
python script.py
  ]]></PythonExec>
</PropertyGroup>

      



All of the above was from Jefff hardy's blog. I checked a case where I used $ PATH using the "long form" technique and it worked for me.

1 http://blog.jdhardy.ca/2011/12/setting-environment-variables-for.html

0


source







All Articles