简体   繁体   English

如何使用 maven 将 Java 项目的 python 文件包含到 .war 中?

[英]How to include python files of Java project into .war with maven?

I have Java project made with maven.我有用 maven 制作的 Java 项目。 So I have typical maven project layout.所以我有典型的 Maven 项目布局。 And I use Jython.我使用 Jython。 So I got few python files.所以我得到了几个python文件。 Wich I use through PythonInterpreter in Java classes.我在 Java 类中通过 PythonInterpreter 使用。
I place my python files in src/main/py folder.我将 python 文件放在 src/main/py 文件夹中。 And I use this path to import the modules by interpreter.我使用此路径通过解释器导入模块。 It works fine on my laptop.它在我的笔记本电脑上运行良好。

The problem is:问题是:
When I do mvn install, this folder does not goes to the war.当我执行 mvn install 时,这个文件夹不会进入战争。
I read about maven resources plugin and added this folder as a resource.我阅读了 maven 资源插件并将此文件夹添加为资源。 Like this:像这样:

<resources>
    <resource>
        <directory>src/main/py</directory>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
    </resource>
</resources>

I that case it adds everything that folder contents, to web-inf/ directly, but not in the src/main/py.在这种情况下,它将文件夹内容的所有内容直接添加到 web-inf/,但不在 src/main/py 中。 So that path is invalid for application in war archive.因此该路径对于战争档案中的应用程序无效。

Question is:问题是:
How should I place this python resource and what I should write in pom.xml, to be able to use the same path on the laptop, and the server?我应该如何放置这个 python 资源以及我应该在 pom.xml 中写什么,以便能够在笔记本电脑和服务器上使用相同的路径?

Can you try including targetPath in the resource element as below:您可以尝试在资源元素中包含 targetPath ,如下所示:

<resource>
            <targetPath>../</targetPath>
            <directory>src/main/py</directory>
            <includes>
                <include>**/*.py</include>
            </includes>

</resource>

我建议使用 src/main/scripts 文件夹并检查它们是否打包到 war 中,如果这不起作用,请检查maven-war-plugin的文档,并按照文档中的描述使用 war 插件定义它。

<resources>
    <resource>
        <directory>src/main/py</directory>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
    </resource>
</resources>

is actually enough to include into jar/war file.实际上足以包含到 jar/war 文件中。

For running see跑步见

Compile Python Sources in Maven 在 Maven 中编译 Python 源代码

It is not recommended to use src/main/py in your python script, think as src/main/py as working/current folder for you python不建议在你的 python 脚本中使用src/main/py ,将src/main/py视为你的工作/当前文件夹 python

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

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