我正在嘗試編寫Python腳本來自動執行一些Maven命令。如何讓Maven與Python子流程一起工作?
以下Python代碼不起作用:
import subprocess
args = ['mvn', '-version']
process = subprocess.Popen(args, stdout=subprocess.PIPE)
導致以下錯誤:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
process = subprocess.Popen(args, stdout=subprocess.PIPE)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
至於下一步,我公司供應的子進程的完整路徑mvn
二進制:
import subprocess
args = ['/usr/local/Cellar/maven/3.2.3/libexec/bin/mvn', '-version']
process = subprocess.Popen(args, stderr=subprocess.PIPE)
out, err = process.communicate()
該命令得到進一步,但打印err
揭示了以下投訴:
Error: JAVA_HOME is not defined correctly.
We cannot execute /usr/libexec/java_home/bin/java
作爲一個全面的檢查,我在我自己的終端上驗證JAVA_HOME
是否設置正確:
$ mvn -version
Apache Maven 3.2.3 (33f8c3e1027c3ddde99d3cdebad2656a31e8fdf4; 2014-08-11T16:58:10-04:00)
Maven home: /usr/local/Cellar/maven/3.2.3/libexec
Java version: 1.8.0_25, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.9.5", arch: "x86_64", family: "mac"
我也能夠在沒有在終端上的麻煩運行mvn
。
我的問題是:爲什麼子進程無法運行mvn
,我該如何做到這一點?
嗯......看來這個環境應該從主叫繼承。你能檢查'os.environ ['JAVA_HOME']'的值嗎? – mgilson 2015-02-09 22:05:47
此外,我不確定這是否重要,但無論出於何種原因,錯誤中的JAVA_HOME以及來自「完整性檢查」的Java家庭似乎都是不同的路徑... – mgilson 2015-02-09 22:07:17
@mgilson'os.environ ['' JAVA_HOME']'是未定義的,但通過在Python中顯式定義它,現在'mvn'子進程調用就可以工作。 – user3898238 2015-02-09 22:11:52