用Python脚本进行Maven构建

2024-09-29 22:37:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用python脚本签出给定url的源代码,我想转到downloadedFoler/src目录并执行mvn clean install。我想用同样的脚本来做。提前谢谢。在


Tags: installsrc目录脚本cleanurl源代码mvn
1条回答
网友
1楼 · 发布于 2024-09-29 22:37:40

您可以执行以下操作:

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)

相关问题 更多 >

    热门问题