Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize system environment variable Path for MSBuild Exec Task

Tags:

msbuild

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

However, I am getting the following error

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.

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

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.

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

like image 480
user2963296 Avatar asked Jul 27 '15 23:07

user2963296


People also ask

How to use environment variables in an MSBuild project?

To use an environment variable in an MSBuild project. Reference the environment variable the same way you would a variable declared in your project file. For example, the following code references the BIN_PATH environment variable: <FinalOutput>$(BIN_PATH)MyAssembly.dll</FinalOutput> You can use a Condition attribute to provide...

Is it possible to set multiple variables in MSBuild?

If you want, you can chain multiple set commands together to set multiple variables: To actually use this in the MSBuild file, you’ll need to escape it like so: Getting the quoting right for <Exec> can be tricky; I use the <Message> task for debugging the command line.

How do I set the default value of an environment variable?

For example, the following code references the BIN_PATH environment variable: You can use a Condition attribute to provide a default value for a property if the environment variable was not set. Use a Condition attribute on a property to set the value only if the property has no value.

What is exec task in MSBuild?

This task is useful when a specific MSBuild task for the job that you want to perform is not available. However, the Exec task, unlike a more specific task, cannot do additional processing or conditional operations based on the result of the tool or command that it runs.


1 Answers

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%"/>

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.

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

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

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

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

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

like image 95
stijn Avatar answered Oct 08 '22 23:10

stijn