从Python脚本运行Maven的方法? [英] Way to run Maven from Python script?

查看:99
本文介绍了从Python脚本运行Maven的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我正在使用Windows.)

我正在尝试从python脚本运行maven.我有这个:

I am trying to run maven from a python script. I have this:

import subprocess

mvn="C:\\_home\\apache-maven-2.2.1\\bin\\mvn.bat --version"
p = subprocess.Popen(mvn, shell=True, stdout = subprocess.PIPE)

stdout, stderr = p.communicate()
print p.returncode # is 0 if success

它工作正常,但我想知道以下内容:

It works fine, but I am wondering about the following:

  • 添加参数而不是附加字符串的更好方法.
  • 也许是不使用上述方法运行maven的某些特定方法.
  • 一种显示输出的方式(当前,它仅根据失败/成功而输出1或0).

我要长期完成的工作(如果有人有更好的方法,请注意这一点)是制作一个简单的脚本来构建项目列表并将另一文件列表(罐子/其他修改过的东西)移动到文件夹部署到虚拟机,手动完成是一个巨大的痛苦.我可以不费吹灰之力地在批处理脚本中工作,我只是好奇地学习Python,想知道它是否更容易管理,因为我可以列出几个列表并遍历每个位置,而不用一行批处理脚本中的每个任务.

What I am trying to accomplish long term (I note this in case someone has a better method) is to make a simple script to build a list of projects and move another list of files (jars/other modified things) to a folder to deploy to VMs, it's a huge pain to do manually. I have this working in a batch script no sweat, I am just curious to learn Python and wonder if it'd be easier to manage because I could just make a couple of lists and iterate over each of the locations rather than have a line for each task in the batch script.

(我的批处理脚本的简短版本.)

@set version=7.8.3
@set staging_folder=C:\Users\me\Desktop\staging

@set stage_was=%staging_folder%\was
@set stage_ear=%stage_was%\stuffui.ear
@set stage_war=%stage_ear%\stuff-%version%.war

:: delete stage contents
call del /s /q %staging_folder%
call rmdir /s /q %stage_was%

:: make folders
call mkdir %stage_ear%
call mkdir %stage_war%\WEB-INF\lib

:: maven builds
call mvn -f C:\workspace\pom.xml -pl proj1,proj2 clean install

:: copy to stage
call xcopy C:\workspace\proj1\target\thing1.jar %stage_ear%\ /i /y

call xcopy C:\workspace\proj2\target\thing2.jar %stage_ear%\ /i /y
call xcopy C:\workspace\proj2\target\thing2.jar %stage_war%\WEB-INF\lib\ /i /y

推荐答案

Mark对使用Python访问Java API信息 的回答提到:

Mark's answer to Accessing Java API information with Python mentions:

Jython ,它是在Java VM上运行的Python.

Jython which is Python run on the Java VM.

有关如何使用Maven调用程序的示例,请参见在那里我的答案(在这种情况下,可以从Java内部使用)

See my answers there for an example on how to use the Maven Invoker (from within Java in this particular case).

这篇关于从Python脚本运行Maven的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆