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
5
  • try like this call mvn -f C:\workspace\pom.xml -pl "proj1,proj2" clean install Commented Jun 19, 2015 at 19:06
  • My batch-script is working as-is, I am trying to make something similar in Python. Commented Jun 19, 2015 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 Commented Jun 19, 2015 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.""_ Commented Jun 21, 2015 at 9: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 :) Commented Jun 21, 2015 at 11:13

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

2 Comments

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"
@CaptainMan "how do I invoke maven through Groovy" – via the Maven Invoker API mentioned in my other answer here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.