Captain Man
Captain Man

Reputation: 7745

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:


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

Upvotes: 7

Views: 14902

Answers (2)

Gerold Broser
Gerold Broser

Reputation: 14782

Re:

in case someone has a better method

Re:

move another list of files (jars/other modified things) to a folder

Have you considered using Maven itself (the copy-resources goal of its Resources Plugin in this particular case)?

Re:

I am just curious to learn Python

Since you are working with Java anyway: Have you considered Groovy as the scripting language of your choice?

Some of its advantages:

  • The Java language is a subset of the Groovy language. I.e. every Java code is also Groovy code. You don't have to use the full-fledged Groovy syntax from the beginning while learning it.
  • It can be scripted.
  • It is supported as scripting and DSL in tools like Jenkins.

Upvotes: 1

Gerold Broser
Gerold Broser

Reputation: 14782

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).

Upvotes: 1

Related Questions