Maven的Python项目依赖性

时间:2019-04-24 10:38:12

标签: python eclipse maven pom.xml

我正在为相互依赖的多个Python项目进行基本设置(我正在使用Python 3.6)。稍后还将涉及Java,因此我选择了Maven进行构建管理。到目前为止,我有一个亲子游戏,一个常见和一个少数特定项目。

常规文件夹结构为:

父母绒球
||-pom.xml

常见
||-src / main / python / xxx / yyy / common
||-Utils.py
子文件夹1
子文件夹2
…|-
||-pom.xml

项目1
||-src / main / python / xxx / yyy / project1
子文件夹A
子文件夹B
…|-
||-pom.xml


实际的POM文件如下所示:

父pom项目“ pom.xml”:

<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>xxx.yyy</groupId>
  <artifactId>parent-pom</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <dependencyManagement>    
    <dependencies>
        <dependency>
            <groupId>xxx.yyy</groupId>
            <artifactId>common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>xxx.yyy</groupId>
            <artifactId>project1</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

      </dependencies>
  </dependencyManagement>

  <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
    </plugins>
  </build>  

</project>

常见项目“ pom.xml”:

<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">
  <modelVersion>4.0.0</modelVersion>
  <artifactId>common</artifactId>
  <packaging>pom</packaging>

  <parent>
    <groupId>aaa</groupId>
    <artifactId>parent-pom</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>

</project>

project1项目“ pom.xml”:

<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">
  <modelVersion>4.0.0</modelVersion>
  <artifactId>project1</artifactId>
  <packaging>pom</packaging>

  <parent>
    <groupId>xxx.yyy</groupId>
    <artifactId>parent-pom</artifactId>  
    <version>0.0.1-SNAPSHOT</version>
  </parent>  

  <dependencies>
    <dependency>
        <groupId>xxx.yyy</groupId>
        <artifactId>common</artifactId>
    </dependency>  
  </dependencies>

  <build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.6.0</version>
            <executions>
                <execution>
                    <configuration>
                        <executable>python</executable>
                        <workingDirectory>src\main\python\xxx\yyy\project1\</workingDirectory>
                        <arguments>
                            <argument>Launcher.py</argument>
                        </arguments>
                    </configuration>
                    <id>python-build</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
  </build>

</project>


现在我在 project1 中有一个名为Launcher的类,该类应从 common 包中导入类Utils

Launcher.py

from xxx.yyy.common.Utils import Utils

class Launcher:

    def __init__(self):    
        Utils.doSomething()

if ('__main__' == __name__):
    Launcher()

Utils.py

class Utils:

    @staticmethod
    def doSomething():
        print ("Test")


但是导入

from xxx.yyy.common.Utils import Utils

无法解决:

[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< xxx.yyy:project1 >--------------------------
[INFO] Building project1 0.0.1-SNAPSHOT
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ project1 ---
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:exec (python-build) @ project1 ---
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ project1 ---
[INFO] Installing C:\...\project1\pom.xml to C:\...\.m2\repository\xxx\yyy\project1\0.0.1-SNAPSHOT\project1-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.949 s
[INFO] Finished at: 2019-04-24T12:19:16+02:00
[INFO] ------------------------------------------------------------------------

C:\...\project1>mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< xxx.yyy:project1 >--------------------------
[INFO] Building project1 0.0.1-SNAPSHOT
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ project1 ---
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:exec (python-build) @ project1 ---
Traceback (most recent call last):
  File "Launcher.py", line 1, in <module>
    from xxx.yyy.common.Utils import Utils
ModuleNotFoundError: No module named 'xxx'
[ERROR] Command execution failed.
org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
    at org.apache.commons.exec.DefaultExecutor.executeInternal (DefaultExecutor.java:404)
    at org.apache.commons.exec.DefaultExecutor.execute (DefaultExecutor.java:166)
    at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:804)
    at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:751)
    at org.codehaus.mojo.exec.ExecMojo.execute (ExecMojo.java:313)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.787 s
[INFO] Finished at: 2019-04-24T12:19:24+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:exec (python-build) on project project1: Command execution failed.: Process exited with an error: 1 (Exit value: 1) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

我想,仅仅声明POM依赖关系不足以访问不同Python / Maven项目中的模块和软件包。

在POM文件中是否可以解决此问题?

1 个答案:

答案 0 :(得分:0)

@khmarbaise:问题是如何通过将Python项目添加到POM文件中的PYTHONPATH来解决ModuleNotFoundError。抱歉,我应该更清楚地询问了。

但是,我发现了解决方法。我只需要在Python执行部分的配置中将PYTHONPATH路径添加到environmentVariables标记中...

解决方案:

<build>
<plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.6.0</version>
        <executions>
            <execution>
                <configuration>
                    <executable>python</executable>
                    <workingDirectory>src\main\python\xxx\yyy\project1\</workingDirectory>
                    <arguments>
                        <argument>Launcher.py</argument>
                    </arguments>
                    <environmentVariables>
                        <PYTHONPATH>
                            ${project.basedir.parent}/common/src/main/python/   <!-- added PYTHONPATH -->
                        </PYTHONPATH>
                    </environmentVariables>
                </configuration>
                <id>python-build</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
            </execution>
        </executions>
    </plugin>
</plugins>