繁体   English   中英

在 Maven 中编译 Python 源代码

[英]Compile Python Sources in Maven

我的项目中有一些 python 源文件(在src/main/python )以及 java 类(在src/main/java )。 我想将这些 python 文件包含到我的 Maven 目标 jar 中。 有没有插件可以做到这一点?

我可以观察到 IntelliJ 创建的构建工件确实包含 python 文件,但 maven 创建的不包含。

正如您所提到的,您当前的项目结构在src/java有 java 文件。 这不符合 maven 标准。 理想情况下,您应该遵循标准结构,使与 IDE 和 maven 的兼容性更简单。

Java 文件应保存在要打包到 jar 中的src/main/java中。

同样,Python 只是脚本文件,没有经过编译。 所以你可以把这些文件放在src/main/resources

src
|---main
|   |---java (Keep your java files here)
|   |---resources
|   \    |---python (You can keep you python files here)
|        \
|---test
|   |---java (Keep your java unit test files here)
|   |---resources (If you have any resources test specific)
|   \

如果不可能,则需要更新 pom.xml 以确保将src/python视为源文件夹。 参考这个插件: build helper plugin

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>add-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>src/python</source>
                ...
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Python 不会编译成中间文件,而只是执行.py文件。 (就像 Node.js 执行.js文件一样。)

所以我们正在谈论包括和运行。

src/main/python文件夹不是标准的,这意味着没有可以处理 python 文件的插件。 您可以将其定义为

<build>
<resources>
    <resource>
        <directory>src/main/python</directory>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
    </resource>
</resources>

资源只是复制到 jar 中。

从 Java 访问文件

    private static String SOURCE_FILE_NAME = "main.py";
    private static InputStream SOURCE_FILE_INPUT = YourClassName.class.getClassLoader().getResourceAsStream(SOURCE_FILE_NAME);

从 Java 运行 Python 有太多选项,每个选项都有自己的限制:

  • 作为操作系统进程
  • 通过 Java JNI 作为 C/C++ 库
  • Jython(只有 Python,所以实际上已经过时了)
  • GraalVM(截至 2021 年实验性)

使用 GraalVM


        Path path = Paths.get("venv", "bin", "graalpython");
        if (path==null){
            log("venv/ is not yet copied under target/classes/, run `mvn process-resources` or any next maven phase,");
        }
        //String VENV_EXECUTABLE = RunGraalPython3.class.getClassLoader().getResource(path.toString()).getPath();
        String VENV_EXECUTABLE = path.toString();
        log(VENV_EXECUTABLE);

        //try (Context context = Context.create()) {
        //try (Context context = Context.newBuilder(PYTHON).allowAllAccess(true).build()) {
        try (Context context = Context.newBuilder("python").
                allowAllAccess(true).
                option("python.ForceImportSite", "true").
                option("python.Executable", VENV_EXECUTABLE).
                build();) {
            context.eval(PYTHON, "print('Hello Python!')");

            context.eval(PYTHON, "import sys; print(sys.version)");


            InputStreamReader reader = new InputStreamReader(SOURCE_FILE_INPUT);
            Source source;
            try {
                source = Source.newBuilder(PYTHON, reader, SOURCE_FILE_NAME).build();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            context.eval(source);


https://github.com/paulvi/java-python-graalvm-template

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM