Created
March 3, 2020 16:37
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example output shows this running prior to the out of the box java compilation: