Skip to content

Instantly share code, notes, and snippets.

@busbey
Created March 3, 2020 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save busbey/bc6022b69ae7d2f1502bdbed7c1737c6 to your computer and use it in GitHub Desktop.
Save busbey/bc6022b69ae7d2f1502bdbed7c1737c6 to your computer and use it in GitHub Desktop.
Example of calling a python script as a part of a maven build
#!/usr/bin/env python2
# Copyright 2020 Sean Busbey
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# place in src/main/python/example.py
""" Example python use in a maven build. echos args """
import sys
sys.dont_write_bytecode = True
print("By default the stdout/stderr from the script will go to the maven logger")
print("\tif you want to change that there's a config on the exec-maven-plugin")
print("Exit with 0 means the build goes on. anything else fails the build")
print("\tif you need to allow for other non-0 exit codes there's a config on the exec-maven-plugin")
print("This invocation was called as follows:")
print('\n\t* '.join(sys.argv))
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!--
Copyright 2020 Sean Busbey
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-->
<modelVersion>4.0.0</modelVersion>
<groupId>com.manvsbeard.example</groupId>
<artifactId>python-in-build</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<python.exec>/usr/local/bin/python</python.exec>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>generate some helper classes</id>
<phase>generate-sources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${python.exec}</executable>
<!-- If you need network resources then set this to true -->
<requiresOnline>false</requiresOnline>
<arguments>
<!--
Alternatively, you can pass the python script directly
as the "executable" and then rely on a #! line for python.
Note that this is a build script and not a part of the
output of the build, so it shouldn't be in
src/main/resources or src/main/scripts
-->
<argument>${project.basedir}/src/main/python/example.py</argument>
<argument>--long=foo</argument>
<argument>-f</argument>
<argument>positional</argument>
<argument>as if you had quoting</argument>
<argument>${project.version}</argument>
<argument>${project.build.directory}/generated-sources/java</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@busbey
Copy link
Author

busbey commented Mar 3, 2020

example output shows this running prior to the out of the box java compilation:


busbey-mba13:maven-python-in-build busbey$ mvn package
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------< com.manvsbeard.example:python-in-build >---------------
[INFO] Building python-in-build 1.0.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- exec-maven-plugin:1.6.0:exec (generate some helper classes) @ python-in-build ---
By default the stdout/stderr from the script will go to the maven logger
	if you want to change that there's a config on the exec-maven-plugin
Exit with 0 means the build goes on. anything else fails the build
	if you need to allow for other non-0 exit codes there's a config on the exec-maven-plugin
This invocation was called as follows:
/Users/busbey/tmp_projects/scratch/maven-python-in-build/src/main/python/example.py
	* --long=foo
	* -f
	* positional
	* as if you had quoting
	* 1.0.0-SNAPSHOT
	* /Users/busbey/tmp_projects/scratch/maven-python-in-build/target/generated-sources/java
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ python-in-build ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/busbey/tmp_projects/scratch/maven-python-in-build/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ python-in-build ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ python-in-build ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/busbey/tmp_projects/scratch/maven-python-in-build/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ python-in-build ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ python-in-build ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ python-in-build ---
[WARNING] JAR will be empty - no content was marked for inclusion!
[INFO] Building jar: /Users/busbey/tmp_projects/scratch/maven-python-in-build/target/python-in-build-1.0.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.167 s
[INFO] Finished at: 2020-03-03T10:42:23-06:00
[INFO] ------------------------------------------------------------------------

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment