Maven多模块项目打包报错

Maven multi module project packaging error

我正在尝试使用 Maven 开发多模块项目。但是当我想打包我的项目时它会出错。

它说网络中不存在包 class。我已经在问答部分尝试了解决方案。但是没有用。你能查看我的回购并告诉我我做错了什么吗?

[INFO] 8 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] xguru .............................................. SUCCESS [  1.330 s]
[INFO] data ............................................... SUCCESS [  5.453 s]
[INFO] web ................................................ FAILURE [  0.746 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.064 s
[INFO] Finished at: 2018-12-04T12:15:14+03:00
[INFO] Final Memory: 48M/391M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:testCompile (default-testCompile) on project web: Compilation failure: Compilation failure:
[ERROR] /C:/Users/asus/IdeaProjects/xguru/web/src/test/java/com/alikaan/xguru/controller/OwnerControllerTest.java:[3,31] package com.alikaan.xguru.model does not exist
[ERROR] /C:/Users/asus/IdeaProjects/xguru/web/src/test/java/com/alikaan/xguru/controller/OwnerControllerTest.java:[4,33] package com.alikaan.xguru.service does not exist
[ERROR] /C:/Users/asus/IdeaProjects/xguru/web/src/test/java/com/alikaan/xguru/controller/OwnerControllerTest.java:[32,13] cannot find symbol
[ERROR] symbol:   class OwnerService
[ERROR] location: class com.alikaan.xguru.controller.OwnerControllerTest
[ERROR] /C:/Users/asus/IdeaProjects/xguru/web/src/test/java/com/alikaan/xguru/controller/OwnerControllerTest.java:[37,9] cannot find symbol
[ERROR] symbol:   class Owner
[ERROR] location: class com.alikaan.xguru.controller.OwnerControllerTest
[ERROR] /C:/Users/asus/IdeaProjects/xguru/web/src/test/java/com/alikaan/xguru/controller/OwnerControllerTest.java:[45,9] cannot find symbol
[ERROR] symbol:   class Owner
[ERROR] location: class com.alikaan.xguru.controller.OwnerControllerTest
[ERROR] /C:/Users/asus/IdeaProjects/xguru/web/src/test/java/com/alikaan/xguru/controller/OwnerControllerTest.java:[45,25] cannot find symbol
[ERROR] symbol:   class Owner
[ERROR] location: class com.alikaan.xguru.controller.OwnerControllerTest
[ERROR] /C:/Users/asus/IdeaProjects/xguru/web/src/test/java/com/alikaan/xguru/controller/OwnerControllerTest.java:[46,9] cannot find symbol
[ERROR] symbol:   class Owner
[ERROR] location: class com.alikaan.xguru.controller.OwnerControllerTest
[ERROR] /C:/Users/asus/IdeaProjects/xguru/web/src/test/java/com/alikaan/xguru/controller/OwnerControllerTest.java:[46,26] cannot find symbol
[ERROR] symbol:   class Owner
[ERROR] location: class com.alikaan.xguru.controller.OwnerControllerTest
[ERROR] -> [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/MojoFailureException
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :web

Process finished with exit code 1

这是我的 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>

    <groupId>com.alikaan</groupId>
    <artifactId>xguru</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <modules>
        <module>data</module>
        <module>web</module>
    </modules>
    <packaging>pom</packaging>


    <name>xguru</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>


    <properties>
        <start-class>com.alikaan.xguru.XguruApplication</start-class>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <configuration>
                    <goals>install</goals>
                    <autoVersionSubmodules>true</autoVersionSubmodules>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

网页:

<?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">
    <parent>
        <artifactId>xguru</artifactId>
        <groupId>com.alikaan</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>web</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.alikaan</groupId>
            <artifactId>data</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.0.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.0.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <version>2.23.4</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <!--Required to get circleCI working-->
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

数据:

<?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">
    <parent>
        <artifactId>xguru</artifactId>
        <groupId>com.alikaan</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>data</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <version>2.23.4</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
    <build>
        <plugins>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <!--Note your version numbe might be different-->
                    <version>2.22.1</version>
                    <configuration>
                        <useSystemClassLoader>false</useSystemClassLoader>
                    </configuration>
                </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <skip>true</skip>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

我通过将这个

添加到数据 pom 来弄清楚
   <properties>
    <spring-boot.repackage.skip>true</spring-boot.repackage.skip>
   </properties>

项目结构有问题。因为你的反应器 parent 的 parent 神器是

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

所有工件,包括 data 模块工件将执行此插件执行:

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <executions>
    <execution>
      <id>repackage</id>
      <goals>
        <goal>repackage</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <mainClass>${start-class}</mainClass>
  </configuration>
</plugin>

不应将 data 模块重新打包到 fat jar 中,因为您不想将其作为 Spring 引导应用程序执行。您想将其用作依赖工件。

不要将spring-boot-starter-parent用作反应堆parent!相反,将 Spring 引导 BOM 导入您的 parent 的 dependencyManagement(更多 here):

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.1.0.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

将此添加到 parent 的 pluginManagement:

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <version>2.1.0.RELEASE</version>
</plugin>

spring-boot-maven-plugin 调用从 parent 移动到 web 模块,这样只有 web 会被重新打包,data 不会。

最后,从 parent 目录调用 mvn clean installinstall 是 reactor 工作所必需的,因为必须将依赖项工件 (data-0.0.1-SNAPSHOT.jar) 复制到本地 Maven 存储库(通常是 ~/.m2/repositories/...),然后才能将其用作依赖项web 模块。