Maven exec plugin - Executing a python script
Asked Answered
D

2

18

I am using maven on Win 7 to build an application. I use the exec plugin to invoke a python script.

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
            <execution>
                <id>create-dir</id>
                <phase>process-classes</phase>
                <goals>
                    <goal>exec</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <executable>src/main/upgrade/create.py</executable>
            <arguments>
                <argument>ChangeSet.txt</argument>
            </arguments>
        </configuration>
    </plugin>

I get the below error when I build the project.

Embedded error: Cannot run program "pathToScript/create.py" CreateProcess error=193, %1 is not a valid Win32 application

I do have python installed and added to the %PATH variable.

How do I fix it such that it will work independent of OS platform ?

.:-EDIT-:.

WORKING CODE SNIPPET

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <executions>
            <execution>
                <configuration>
                    <executable>python</executable>
                    <workingDirectory>src/main/upgrade/</workingDirectory>
                    <arguments>
                        <argument>createChangeSet.py</argument>
                    </arguments>    

                </configuration>
                <id>python-build</id>
                <phase>prepare-package</phase>
                <goals>
                    <goal>exec</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
Delozier answered 20/12, 2012 at 14:35 Comment(2)
The question is why do you need to run a python script? You need to define the the python intepreter on Windows on Linux the shebang-line is important.Stuart
I use your approach to run a time-consuming python script at maven install phase, the python script works but with chaos console log, the output comes from python is in some random wrong order. Do you have the same problem?Collusive
Q
19

In Windows, the script isn't executable. The executable is the python interpreter, and the script is an argument to it, so put <executable>path to your python interpreter</executable> and add the script as an <argument>. I expect the same should work for any platform, but I'm no Python expert.

Quiles answered 24/12, 2012 at 17:36 Comment(1)
Thanks Ryan. That worked. Adding the working script to the original post for anybody else who may need it.Delozier
L
1

Just wanted to add that with the newer version of exec-maven-plugin, the configuration tag must be placed after the executions tag to work.

As in the working snippet above:

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <executions>
            <execution>
                <id>python-build</id>
                <phase>prepare-package</phase>
                <goals>
                    <goal>exec</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
           <executable>python</executable>
           <workingDirectory>src/main/upgrade/</workingDirectory>
           <arguments>
                <argument>createChangeSet.py</argument>
           </arguments>    
        </configuration>
    </plugin>
Lisbethlisbon answered 20/4, 2017 at 19:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.