7

(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
Captain Man
  • 6,997
  • 6
  • 48
  • 74
  • try like this `call mvn -f C:\workspace\pom.xml -pl "proj1,proj2" clean install` – npocmaka Jun 19 '15 at 19:06
  • My batch-script is working as-is, I am trying to make something similar in Python. – Captain Man Jun 19 '15 at 19:08
  • 1
    I think it is important to tell maven to run in batch mode (so it uses defaults and does not prompt for input): mvn --batch-mode -v – wemu Jun 19 '15 at 19:46
  • 1
    @wemu It better should be `-V` instead of `-v`. See _Maven: The Complete Reference, 6.1.4. Displaying Version Information:_ [_"the `-v` option will terminate the Maven process after printing out the version."_](http://books.sonatype.com/mvnref-book/reference/running-sect-options.html#running-sect-version-option)"_ – Gerold Broser Jun 21 '15 at 09:21
  • 1
    true. just wanted to emphasize the batch mode - and messed up the -v (which does not the same thing as -V) - very true :) – wemu Jun 21 '15 at 11:13

2 Answers2

1

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

Community
  • 1
  • 1
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
  • I am trying to clarify, this Apache Maven Invoker API is java, so you suggest I use Jython to take advantage of it through JVM while still technically using/learning Python, right? – Captain Man Jun 22 '15 at 15:12
1

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.
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
  • Thanks for the suggestions, I will pass on copy-resources as I don't want to fool with pom files if I don't have too and as far as Groovy vs Python it would just raise the same question: "how do I invoke maven through Groovy" – Captain Man Jun 22 '15 at 14:54
  • @CaptainMan _"how do I invoke maven through Groovy"_ – via the Maven Invoker API mentioned in my other answer here. – Gerold Broser Jun 23 '15 at 07:14