I am checking out a source for a given url using a python script and I want to go to the downloadedFoler/src directory and perform a mvn clean install
. I want to do it in the same script. Thank in advance.
Asked
Active
Viewed 4,379 times
1

Yasitha
- 2,233
- 4
- 24
- 36
-
I tried os.system("mvn clean install") and it works but I need to perform this in the correct location. – Yasitha Jan 27 '14 at 09:50
-
2Try this question: [how-do-i-cd-in-python](http://stackoverflow.com/questions/431684/how-do-i-cd-in-python) – Kraay89 Jan 27 '14 at 10:03
-
You have to go into the location where pom.xml file is located which mean in your case a level higher. – khmarbaise Jan 27 '14 at 10:13
1 Answers
1
You can do the following:
import os
import subprocess
# Context Manager to change current directory.
# I looked at this implementation on stackoverflow but unfortunately do not have the link
# to credit the user who wrote this part of the code.
class changeDir:
def __init__(self, newPath):
self.newPath = os.path.expanduser(newPath)
# Change directory with the new path
def __enter__(self):
self.savedPath = os.getcwd()
os.chdir(self.newPath)
# Return back to previous directory
def __exit__(self, etype, value, traceback):
os.chdir(self.savedPath)
# folderPath = path of the folder you want to run mvn clean install on
with changeDir(folderPath):
# ****** NOTE ******: using shell=True is strongly discouraged since it possesses security risks
subprocess.call(["mvn", "clean", "install"], shell=True)

SaurabhM
- 7,995
- 1
- 15
- 20