Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to run Maven from Python script?

(I am using Windows.)

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:

  • Better ways to add parameters instead of appending the string.
  • Maybe some specific way to run maven without the above.
  • A way to show the output (currently it only prints a 1 or 0 based on failure/success).

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.

(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
like image 312
Captain Man Avatar asked Jun 19 '15 18:06

Captain Man


1 Answers

There is the Apache Maven Invoker API.

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

Jython which is Python run on the Java VM.

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

like image 160
Gerold Broser Avatar answered Sep 18 '22 09:09

Gerold Broser