Alex Malcolm
Alex Malcolm

Reputation: 549

Packaging/Deploying Maven from Python

Big picture is I'm trying to automate my deployment process of building with maven and deploying to a web logic server. Little picture is I'm using subprocess to see if I can call maven from within python. When I attempt this subprocess mistakes mvn for a file. Here is my code so far:

import subprocess


def main():
    print(subprocess.check_output(["mvn", "-v"]))


if __name__ == '__main__':
    main()

And here's my error:

C:\pythondev\python.exe "C:/pythondev/development/deployment scripts/redploy-to-localhost.py"
Traceback (most recent call last):
  File "C:/pythondev/development/deployment scripts/redploy-to-localhost.py", line 9, in <module>
    main()
  File "C:/pythondev/development/deployment scripts/redploy-to-localhost.py", line 5, in main
    subprocess.check_output(["a"])
  File "C:\pythondev\lib\subprocess.py", line 376, in check_output
    **kwargs).stdout
  File "C:\pythondev\lib\subprocess.py", line 453, in run
    with Popen(*popenargs, **kwargs) as process:
  File "C:\pythondev\lib\subprocess.py", line 756, in __init__
    restore_signals, start_new_session)
  File "C:\pythondev\lib\subprocess.py", line 1155, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

Process finished with exit code 1

Although my issue is with subprocess I'm open to answers that suggest a better alternative.

Upvotes: 2

Views: 372

Answers (2)

Martin J.H.
Martin J.H.

Reputation: 2205

I ran into the same issue and was hesistant to use shell=True, because the internet tells me this is evil.

When I run where mvn in my cmd.exe, I can see that there are two matches:

  • mvn, which is a Unix shell-script (it starts with #!/bin/sh),
  • mvn.cmd, which is a Windows batch file.

I think what happens when you execute mvn something -something in cmd.exe is the following: Windows tries finding an executable called mvn. It finds the mvn file, but realizes that this file is not executable. It then tries finding files like mvn.com, mvn.exe, ... (see the %PATHEXT% system variable). When it finds mvn.cmd, it executes that and everyone is happy.

As far as I understand it, the problem with subprocess.check_output (and subprocess.run, and so on) is that the path-"expansion" via %PATHEXT% is not being performed. So the solution is that you have to give the extension manually and run your command as

print(subprocess.check_output(["mvn.cmd", "-v"]))

Upvotes: 2

Alex Malcolm
Alex Malcolm

Reputation: 549

Try this it worked for me.

print(subprocess.check_output(["mvn", "-v"], shell=True))

Upvotes: 0

Related Questions