简体   繁体   English

从Python脚本运行Maven的方法?

[英]Way to run Maven from Python script?

(I am using Windows.) (我正在使用Windows。)

I am trying to run maven from a python script. 我试图从python脚本运行maven。 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: 它工作正常,但我想知道以下内容:

  • Better ways to add parameters instead of appending the string. 更好的方法来添加参数而不是附加字符串。
  • Maybe some specific way to run maven without the above. 也许某种特定方式在没有上述情况下运行maven。
  • A way to show the output (currently it only prints a 1 or 0 based on failure/success). 一种显示输出的方法(目前它只根据失败/成功打印1或0)。

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. 我想要长期实现的目标(我注意到有人有更好的方法)是制作一个简单的脚本来构建项目列表并将另一个文件列表(jar /其他修改过的东西)移动到要部署的文件夹对于VM来说,手动操作非常痛苦。 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. 我有一个批处理脚本工作没有汗,我只是好奇学习Python,并想知道它是否更容易管理因为我可以只做几个列表并迭代每个位置而不是有一行批处理脚本中的每个任务。

(Short version of what my batch script looks like.) (我的批处理脚本的简短版本。)

@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

There is the Apache Maven Invoker API . Apache Maven Invoker API

Mark's answer to Accessing Java API information with Python mentions: Mark 使用Python提到的访问Java API信息的答案提到:

Jython which is Python run on the Java VM. Jython是在Java VM上运行的Python。

See my answers there for an example on how to use the Maven Invoker (from within Java in this particular case). 请参阅我的答案,了解如何使用Maven Invoker(在此特定情况下来自Java内部)。

Re: 回覆:

in case someone has a better method 万一有人有更好的方法

Re: 回覆:

move another list of files (jars/other modified things) to a folder 将另一个文件列表(jar /其他修改过的东西)移动到一个文件夹

Have you considered using Maven itself (the copy-resources goal of its Resources Plugin in this particular case)? 您是否考虑过使用Maven本身(在此特定情况下,其资源插件copy-resources目标)?

Re: 回覆:

I am just curious to learn Python 我只是好奇学习Python

Since you are working with Java anyway: Have you considered Groovy as the scripting language of your choice? 既然您正在使用Java:您是否认为Groovy是您选择的脚本语言?

Some of its advantages: 它的一些优点:

  • The Java language is a subset of the Groovy language. Java语言是Groovy语言的一个子集。 Ie every Java code is also Groovy code. 即每个Java代码也是Groovy代码。 You don't have to use the full-fledged Groovy syntax from the beginning while learning it. 在学习它时,您不必从一开始就使用完整的Groovy语法。
  • It can be scripted. 它可以编写脚本。
  • It is supported as scripting and DSL in tools like Jenkins . 它在Jenkins等工具中作为脚本和DSL支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM