Maven配置文件没有看到子模块中的依赖项

时间:2014-03-16 23:47:26

标签: java maven

我搜索过SO和互联网,但没有成功。这就是我的简短内容:

带有dev和appserv配置文件的多模块项目。使用dev配置文件时(仅mvn clean install因为default = true),然后加载特定的依赖项。必须在Child-A中加载一个依赖项Child-B。我总是从父POM开始构建。

我在父POM中通过激活定义了个人资料,并在Child-B我有相同的个人资料。

这是我的父POM

<project ..>
    <groupId>com.organisation</groupId>
    <artifactId>project-name</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>

    <modules>
        <module>child-b</module>
        <module>child-a</module>
    </modules>

    <properties>
        <javax.ws.rs.version>2.0</javax.ws.rs.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- project dependencies -->
            <dependency>
                <groupId>${project.groupId}</groupId>
                <artifactId>child-a</artifactId>
                <version>${project.version}</version>
            </dependency>
            <!-- build dependencies -->
            <dependency>
                <groupId>javax.ws.rs</groupId>
                <artifactId>javax.ws.rs-api</artifactId>
                <version>${javax.ws.rs.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <dependencyManagement>
                <dependencies>
                    <dependency>
                        <groupId>${project.groupId}</groupId>
                        <artifactId>child-a</artifactId>
                        <version>${project.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>javax.ws.rs</groupId>
                        <artifactId>javax.ws.rs-api</artifactId>
                        <version>${javax.ws.rs.version}</version>
                    </dependency>
                </dependencies>
            </dependencyManagement>
        </profile>

        <profile>
            <id>app-server</id>
            <activation>
                <activeByDefault>false</activeByDefault>
                <property>
                    <name>env</name>
                    <value>app-server</value>
                </property>
            </activation>
        </profile>
    </profiles>
</project>

我的Child-B模块调用了Child-A,IDE在构建Child-B时也没有看到Child-A的类。

<project ..>
    <parent>
        <groupId>com.organisation</groupId>
        <artifactId>project-name</artifactId>
        <version>1.0</version>
    </parent>

    <artifactId>child-b</artifactId>
    <packaging>jar</packaging>

    <profiles>
        <profile>
            <id>dev</id>
            <dependencies>
                <dependency>
                    <groupId>${project.parent.groupId}</groupId>
                    <artifactId>child-a</artifactId>
                </dependency>
                <dependency>
                    <groupId>javax.ws.rs</groupId>
                    <artifactId>javax.ws.rs-api</artifactId>
                </dependency>
            </dependencies>
        </profile>

        <profile>
            <id>app-server</id>
                <dependencies>
                    ...
                </dependencies>
        </profile>
    </profiles>

    <dependencies>
        ...
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        ...
    </dependencies>
</project>

我不知道这里有什么问题,无法理解问题所在,为什么Child-B没有Child-A作为依赖。在POM内的父profile内,我尝试删除dependencyManagement,但结果仍然相同 - Child-A模块中没有显示Child-B。显然没有配置文件一切正常。

1 个答案:

答案 0 :(得分:0)

在Child-b中,您将依赖项置于child-b而不是依赖于child-a

<dependency>
   <groupId>${project.parent.groupId}</groupId>
   <artifactId>child-b</artifactId>
</dependency>

而不是

<dependency>
   <groupId>${project.parent.groupId}</groupId>
   <artifactId>child-a</artifactId>
</dependency>