Way to run Maven from Python script?

2024/10/14 2:22:42

(I am using Windows.)

I am trying to run maven from a python script. I have this:

import subprocessmvn="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 /ycall 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
Answer

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

https://en.xdnf.cn/q/69463.html

Related Q&A

Why does testing `NaN == NaN` not work for dropping from a pandas dataFrame?

Please explain how NaNs are treated in pandas because the following logic seems "broken" to me, I tried various ways (shown below) to drop the empty values.My dataframe, which I load from a C…

Python dynamic import methods from file [duplicate]

This question already has answers here:How can I import a module dynamically given its name as string?(10 answers)How can I import a module dynamically given the full path?(37 answers)Closed last yea…

Python list does not shuffle in a loop

Im trying to create an randomized list of keys by iterating:import randomkeys = [1, 2, 3, 4, 5] random.shuffle(keys) print keysThis works perfect. However, if I put it in a loop and capture the output:…

Python extract max value from nested dictionary

I have a nested dictionary of the form:{2015-01-01: {time: 8, capacity: 5}, 2015-01-02: {time: 8, capacity: 7},2015-01-03: {time: 8, capacity: 8} etc}The dictionary is created from a csv file using dic…

Exponential Decay on Python Pandas DataFrame

Im trying to efficiently compute a running sum, with exponential decay, of each column of a Pandas DataFrame. The DataFrame contains a daily score for each country in the world. The DataFrame looks lik…

TensorFlow - why doesnt this sofmax regression learn anything?

I am aiming to do big things with TensorFlow, but Im trying to start small. I have small greyscale squares (with a little noise) and I want to classify them according to their colour (e.g. 3 categories…

Extended example to understand CUDA, Numba, Cupy, etc

Mostly all examples of Numba, CuPy and etc available online are simple array additions, showing the speedup from going to cpu singles core/thread to a gpu. And commands documentations mostly lack good …

Python 2 newline tokens in tokenize module

I am using the tokenize module in Python and wonder why there are 2 different newline tokens:NEWLINE = 4 NL = 54Any examples of code that would produce both tokens would be appreciated.

Prevent encoding errors in Python

I have scripts which print out messages by the logging system or sometimes print commands. On the Windows console I get error messages likeTraceback (most recent call last):File "C:\Python32\lib\l…

How do I get the operating system name in a friendly manner using Python 2.5?

I tried:print os.nameAnd the output I got was::ntHowever, I want output more like "Windows 98", or "Linux".After suggestions in this question, I also tried:import os print os.name i…