Skip to content

Execute python scripts in maven projects

Introduction

This post demonstrates how to execute Python scripts in Maven projects. We will execute a Python script during the build process and generate a file to be packaged in the target JAR.

Environments

  • Maven 3.x
  • Python 2.7

The exec-maven-plugin plugin

The exec-maven-plugin allows you to execute scripts during the Maven build lifecycle. There are two goals of this plugin:

  • exec:exec: Executes programs and Java programs in a separate process.
  • exec:java: Executes Java programs in the same VM.

We will use the exec:exec goal to execute a Python script.

The pom.xml

pom.xml
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<configuration>
<executable>python</executable>
<workingDirectory>src/main/resources/</workingDirectory>
<arguments>
<argument>my.py</argument>
</arguments>
</configuration>
<id>python_build</id>
<phase>generate-resources</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Explanation

  • We add a build plugin named exec-maven-plugin to the Maven project.
  • The plugin is configured to run a Python script named my.py in the src/main/resources directory.
  • The plugin will be executed during the generate-resources phase of the Maven build process.

The Python script

Create a new Python script named my.py in the src/main/resources directory of the project with the following content:

my.py
# coding=UTF-8
import os,io
cur_path = os.getcwd() #should be the src/main/resources
with io.open(cur_path+"/tempfile", mode='w') as the_file:
the_file.write(u'hello maven')

What the script does:

  • Retrieves the current path, which should be src/main/resources.
  • Uses the io.open function to create a new file named tempfile.
  • Writes the string hello maven into the file.

Run the script

Execute the following command to run the script:

Terminal window
mvn compile

You should see output similar to this:

Terminal window
[INFO] Building test-maven-python 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:exec (python-build) @ test-maven-python ---
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ test-maven-python ---
[debug] execute contextualize

Check the file and its content:

Terminal window
➜ resources git:(master) ✗ ll tempfile
-rw-r--r-- 1 zzz staff 11B Apr 28 22:19 tempfile
➜ resources git:(master) ✗ cat tempfile
hello maven%

Summary

The exec-maven-plugin allows you to execute Java or system scripts during the Maven build lifecycle. In this demo, we successfully executed a Python script within a Maven project, demonstrating how to integrate Python scripts into your Maven build process.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!