多模块Maven项目 - 模块无法看到对方

时间:2017-06-23 08:49:44

标签: java xml maven intellij-idea multi-module

我有一个简单的Maven项目,看起来像这样 project view

有一个主要的pom.xml看起来像这样:

    <?xml version="1.0" encoding="UTF-8"?>
<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>piche</artifactId>
    <name>PICHE project</name>
    <description>PICHE project</description>
    <groupId>com.pesho.piche</groupId>
    <version>1.0</version>
    <packaging>pom</packaging>

    <properties>
        <cucumber.jvm.version>1.2.5</cucumber.jvm.version>
        <project.version>1.0</project.version>
        <runner.folder>".."</runner.folder>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.pesho.piche</groupId>
            <artifactId>test</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>com.pesho.piche</groupId>
            <artifactId>core</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>${cucumber.jvm.version}</version>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>${cucumber.jvm.version}</version>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-guice</artifactId>
            <version>${cucumber.jvm.version}</version>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-core</artifactId>
            <version>${cucumber.jvm.version}</version>
        </dependency>
        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
            <version>3.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.inject.extensions</groupId>
            <artifactId>guice-assistedinject</artifactId>
            <version>3.0</version>
        </dependency>
        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>1.18</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>22.0</version>
        </dependency>
    </dependencies>
    </dependencyManagement>

    <modules>
        <module>core</module>
        <module>test</module>
    </modules>
</project>

有两个模块 - 核心和测试。 核心模块pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>core</artifactId>
    <name>Core module</name>
    <description>Core Module</description>

    <parent>
        <groupId>com.pesho.piche</groupId>
        <artifactId>piche</artifactId>
        <version>1.0</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>22.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
            <version>3.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

测试模块pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>test</artifactId>
    <name>Test module</name>

    <url>http://maven.apache.org</url>

    <parent>
        <artifactId>piche</artifactId>
        <groupId>com.pesho.piche</groupId>
        <version>1.0</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <dependencies>
        <dependency>
            <groupId>com.pesho.piche</groupId>
            <artifactId>core</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java</artifactId>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-guice</artifactId>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-core</artifactId>
    </dependency>
        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.inject.extensions</groupId>
            <artifactId>guice-assistedinject</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
        </dependency>
    </dependencies>

</project>

我想使用核心内部代码测试。依赖看起来正确。该项目构建没有错误:

[INFO] Reactor Summary:
[INFO] 
[INFO] PICHE project ..................................... SUCCESS [0.317s]
[INFO] Core module ....................................... SUCCESS [1.317s]
[INFO] Test module ....................................... SUCCESS [0.669s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.461s
[INFO] Finished at: Fri Jun 23 11:03:18 EEST 2017
[INFO] Final Memory: 17M/41M
[INFO] ------------------------------------------------------------------------

但是当我尝试在我的测试中从核心导入一个类时,找不到该类。

3 个答案:

答案 0 :(得分:0)

您必须在测试模块pom 中制作核心依赖关系的版本

<dependencies>
    <dependency>
        <groupId>com.pesho.piche</groupId>
        <artifactId>core</artifactId>
        <version>${parent.version}</version> <!-- TODO -->
    </dependency>

AND 删除父POM中的行

<dependencyManagement>
<dependencies>
    <dependency>
        <groupId>com.pesho.piche</groupId>
        <artifactId>test</artifactId>
        <version>1.0</version>
    </dependency>
    <dependency>
        <groupId>com.pesho.piche</groupId>
        <artifactId>core</artifactId>
        <version>1.0</version>
    </dependency>
来自父模块的

AND

mvn clean install 

我希望能帮到你

答案 1 :(得分:0)

实际上,coretest在父pom.xml中的依赖关系是不必要的。要访问核心中的api,您只需要在pom中定义依赖关系并将其安装到本地存储库中 在mvn clean install的根目录下运行core,这将编译此项目并打包并将jar安装到maven本地存储库。然后依赖项可以在另一个项目中相遇,您可以访问定义的api 但core中的任何更改都可能导致您将其重新安装到本地存储库。

答案 2 :(得分:0)

感谢您的所有答案。我注意到了这些建议。问题是我在模块中使用默认包。永远不要那样做!依赖性还可以,但由于类路径选项,我无法看到它们。