Phing exec command to set environment variable

I am trying to set an environment variable in a build script with phing. This is usually done on the command line as follows:

export MY_VAR=value 

In Phing, I did the following, but it does not work.

 <exec command="export MY_VAR=value" /> 
+7
source share
3 answers

Bold statement: It is not possible to set / export a Unix shell variable in PHP so that it is visible inside the scope that launched the php script.

 php myfile.php (does putenv or shell_exec('export foo=bar');) echo $foo 

Will return nothing. Since PHP cannot do this, phing cannot.

Accessing shell environment variables through several script runs (if that's what you want) also seems to be a consistent design decision, quite well-off.

In addition, I urge you to stick to the finger and learn its meager lesson. Fing helps to some extent stateless person.

+6
source

I see that this is a rather old question, but I do not think that it was answered in the best way. If you want to export a shell variable, for example, say that you are running phpunit from phing and want to export before calling phpunit, try:

 <exec command="export MY_VAR=value ; /path/to/phpunit" /> 

Just do the export and call your command inside one exec tag. Separate the export statement and shell executable with a semicolon, as shown. Your script will be able to access the value using the standard php function:

 $myVar = getenv('MY_VAR'); 
+9
source

I had never heard of phing before, but it looks like a very promising build tool. Thank you for message! I looked at the document on phing.info, I found the following opportunity:

# 0 I would like to clarify one point. You say that

 prompt$ > export MY_VAR=value prompt$ > phing build.xml 

doesn't set MY_VAR to display inside running phing processes? I would be surprised, but I would understand if you do not want to run your build script.

# 1 I think that in the context of the build tool, a function, such as exec, is designed to run a stand-alone program, so although exec can run and set MY_VAR, all this happens in a subprocess that immediately disappears as exec finishes and continues processing the next task in the file build.xml.

If you are just trying to make sure your phing script works with specific values ​​for env_vars, you can try

 Command-line arguments: .... -D<property>=<value> // Set the property to the specified value to be used in the buildfile 

So suppose you can do

 phing -DMY_VAR=value build.xml 

# 2 Did you think you were using the properites file? See http://www.phing.info/docs/guide/stable/chapters/appendixes/AppendixF-FileFormats.html and scroll down for information on build.properties

# 3 also ...

 Phing Built-In Properties Property Contents env.* Environment variables, extracted from $_SERVER. 

would you access them with something like

  ${env.MY_VAR} 

# 4 This is similar to what you really want

 <replacetokens> <token key="BC_PATH" value="${top.builddir}/"/> <token key="BC_PATH_USER" value="${top.builddir}/testsite/user/${lang}/"/> </replacetokens> 

Hope this helps.

+4
source

All Articles