Maven exec 插件 - 执行 python 脚本

Posted

技术标签:

【中文标题】Maven exec 插件 - 执行 python 脚本【英文标题】:Maven exec plugin - Executing a python script 【发布时间】:2012-12-08 02:34:37 【问题描述】:

我在 Win 7 上使用 maven 构建应用程序。我使用 exec 插件来调用 python 脚本。

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

我在构建项目时收到以下错误。

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

我确实安装了 python 并将其添加到 %PATH 变量中。

如何修复它以使其独立于操作系统平台工作?

.:-EDIT-:.

工作代码片段

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

【问题讨论】:

问题是为什么需要运行python脚本?您需要在 Linux 上的 Windows 上定义 python 解释器,shebang-line 很重要。 我使用你的方法在 maven 安装阶段运行了一个耗时的 python 脚本,python 脚本可以工作,但是使用混乱的控制台日志,来自 python 的输出是一些随机错误的顺序。你有同样的问题吗? 【参考方案1】:

在 Windows 中,该脚本不可执行。可执行文件是 python 解释器,脚本是它的参数,所以输入&lt;executable&gt;path to your python interpreter&lt;/executable&gt; 并将脚本添加为&lt;argument&gt;。我希望这同样适用于任何平台,但我不是 Python 专家。

【讨论】:

谢谢瑞恩。那行得通。将工作脚本添加到原始帖子中以供可能需要它的任何其他人使用。【参考方案2】:

只是想在较新版本的 exec-maven-plugin 中添加,配置标签​​必须放在执行标签之后才能工作。

和上面的工作 sn-p 一样:

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

【讨论】:

以上是关于Maven exec 插件 - 执行 python 脚本的主要内容,如果未能解决你的问题,请参考以下文章

运行黄瓜并生成没有maven / gradle插件的html报告

Maven 命令深度理解

这是啥 Maven 错误? “生命周期配置未涵盖插件执行...)”

Maven:将插件执行绑定到另一个插件的执行,而不是生命周期阶段

maven 插件执行的隐式 ID 是啥?

使用 maven 插件执行器规则失败