多模块项目中的Maven兄弟依赖问题

时间:2016-09-28 10:11:48

标签: java maven m2e

我有一个多模块项目,包含1个主模块(引擎)和2个子模块(客户端和服务器)

发动机(主模块)    - 客户    - 服务器

当我尝试在客户端中使用服务器模块时,它会抛出一个编译错误,指示找不到服务器软件包。

请找到以下所有3个POM。

引擎POM

<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>dev.kasse</groupId>
<artifactId>engine</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
    <module>server</module>
    <module>client</module>
</modules>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
    </dependency>
</dependencies>

客户端pom

<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>
    <parent>
    <groupId>dev.kasse</groupId>
    <artifactId>engine</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>client</artifactId>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    **<dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>server</artifactId>
        <scope>test</scope>
        <version>${project.version}</version>
    </dependency>**
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
    </dependency>
    <dependency>
        <groupId>commons-configuration</groupId>
        <artifactId>commons-configuration</artifactId>
        <version>1.10</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <forkCount>0</forkCount>
                <reuseForks>true</reuseForks>               
            </configuration>
        </plugin>
    </plugins>
</build>

服务器POM

    <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>
    <parent>
        <groupId>dev.kasse</groupId>
        <artifactId>engine</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>server</artifactId>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
        </dependency>
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>
  </project>

4 个答案:

答案 0 :(得分:1)

您的模块需要

<packaging>jar</packaging>

覆盖

<packaging>pom</packaging>

来自父文件。

答案 1 :(得分:1)

陷入类似的问题。这似乎是由春季靴子引起的。包装的罐子。 Spring引导创建一个可执行jar,将类发送到jar内的BOOT-INF文件夹。

将以下内容添加到pom的build \ plugins部分可解决此问题(此处为客户端pom)

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <configuration>
                        <forceCreation>true</forceCreation>
                    </configuration>
                </execution>
            </executions>
        </plugin>

另一种解决方案是:Maven Multi-module dependency package not found

答案 2 :(得分:1)

我有一个类似的项目设置,但是您的解决方案对我来说很好。

在您的情况下可能仍然可以解决该问题的一种方法是,使用 dependencyManagment

父pom 中定义版本
<dependencyManagement>
   <dependencies>
      <dependency>
            <groupId>${groupId}</groupId>
            <artifactId>server</artifactId>
            <version>${project.version}</version>
        </dependency>
   </dependencies>
</dependencyManagement>

答案 3 :(得分:0)

对服务器的依赖性具有“测试”范围:这意味着它将不包含在运行时包中

<dependency>
    <groupId>${project.groupId}</groupId>
    <artifactId>server</artifactId>
    <scope>test</scope> <!-- remove this tag -->
    <version>${project.version}</version>
</dependency>

有关依赖范围的更多信息,请参阅:here