如何将包和 类 导出到 Java 应用程序中的测试模块?

How to export packages and classes to a test module in a Java app?

我想用以下模块构建一个多模块Spring启动应用程序:

我的问题是:

我实际上可以 运行 对 ModelTests 模块中的对象进行单元测试,但是做一个 mvn clean installApp 模块上失败,因为 Tests 模块对 Model 的对象一无所知。

好的,Spring 和 Maven 是两种不同的工具,但我应该在 App 的 POM 中做什么来明确声明模型模块中的所有对象都应该导出到测试模块以便进行测试?

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

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

    <groupId>com.humanresources.game</groupId>
    <artifactId>hrg-app</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>Application</name>
    <description>Human Resources game engine</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <modules>
        <module>../hrg-model</module>
        <module>../hrg-tests</module>
    </modules>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </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>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                    <mainClass>HrgAppApplication</mainClass>
                    <goal>package</goal>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Tests' 模块 POM 仅在 Model 模块上声明了一个 <dependency> 块。

Tests 模块和 Model 模块在各自的 POM 中都没有任何 <build> 块。

正如所见here,一切都应该只由 App 的 POM 管理。

所以我的问题是:我应该在 App 的 POM <build> 块中添加什么以有效导出所有 Model 的包和 classes 到 Tests 模块,这样就可以 mvn clean install 整个项目 ?

提前致谢。

编辑:POM.xml 用于测试和模型模块

测试/POM

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.humanresources.game</groupId>
        <artifactId>hrg-app</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../hrg-app/pom.xml</relativePath>
    </parent>

    <artifactId>hrg-tests</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>Tests</name>
    <description>Tests project</description>

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

    <dependencies>
        <dependency>
            <groupId>com.humanresources.game</groupId>
            <artifactId>hrg-model</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

型号/POM

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.humanresources.game</groupId>
        <artifactId>hrg-app</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../hrg-app/pom.xml</relativePath>
    </parent>

    <artifactId>hrg-model</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>Model</name>
    <description>Tests project for Human Resources game</description>

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

</project>

mvn clean install 后的控制台输出(尽管单元测试成功):

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] Application                                                        [pom]
[INFO] Model                                                              [jar]
[INFO] Tests                                                              [jar]
[INFO] 
[INFO] ------------------< com.humanresources.game:hrg-app >-------------------
[INFO] Building Application 1.0-SNAPSHOT                                  [1/3]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ hrg-app ---
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.4.3:repackage (repackage) @ hrg-app ---
[INFO] 
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ hrg-app ---
[INFO] Installing C:\Users\Fred\IdeaProjects\HumanResources\hrg-app\pom.xml to C:\Users\Fred\.m2\repository\com\humanresources\game\hrg-app.0-SNAPSHOT\hrg-app-1.0-SNAPSHOT.pom
[INFO] 
[INFO] -----------------< com.humanresources.game:hrg-model >------------------
[INFO] Building Model 0.0.1-SNAPSHOT                                      [2/3]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ hrg-model ---
[INFO] Deleting C:\Users\Fred\IdeaProjects\HumanResources\hrg-model\target
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ hrg-model ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ hrg-model ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 11 source files to C:\Users\Fred\IdeaProjects\HumanResources\hrg-model\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ hrg-model ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory C:\Users\Fred\IdeaProjects\HumanResources\hrg-model\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ hrg-model ---
[INFO] Changes detected - recompiling the module!
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ hrg-model ---
[INFO] 
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ hrg-model ---
[INFO] Building jar: C:\Users\Fred\IdeaProjects\HumanResources\hrg-model\target\hrg-model-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.4.3:repackage (repackage) @ hrg-model ---
[INFO] Replacing main artifact with repackaged archive
[INFO] 
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ hrg-model ---
[INFO] Installing C:\Users\Fred\IdeaProjects\HumanResources\hrg-model\target\hrg-model-0.0.1-SNAPSHOT.jar to C:\Users\Fred\.m2\repository\com\humanresources\game\hrg-model[=15=].0.1-SNAPSHOT\hrg-model-0.0.1-SNAPSHOT.jar
[INFO] Installing C:\Users\Fred\IdeaProjects\HumanResources\hrg-model\pom.xml to C:\Users\Fred\.m2\repository\com\humanresources\game\hrg-model[=15=].0.1-SNAPSHOT\hrg-model-0.0.1-SNAPSHOT.pom
[INFO] 
[INFO] -----------------< com.humanresources.game:hrg-tests >------------------
[INFO] Building Tests 0.0.1-SNAPSHOT                                      [3/3]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ hrg-tests ---
[INFO] Deleting C:\Users\Fred\IdeaProjects\HumanResources\hrg-tests\target
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ hrg-tests ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ hrg-tests ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Users\Fred\IdeaProjects\HumanResources\hrg-tests\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ hrg-tests ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory C:\Users\Fred\IdeaProjects\HumanResources\hrg-tests\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ hrg-tests ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Users\Fred\IdeaProjects\HumanResources\hrg-tests\target\test-classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /C:/Users/Fred/IdeaProjects/HumanResources/hrg-tests/src/test/java/com/humanresources/game/hrgtests/HrgTestsApplicationTests.java:[3,42] package com.humanresources.game.model.card does not exist
[ERROR] /C:/Users/Fred/IdeaProjects/HumanResources/hrg-tests/src/test/java/com/humanresources/game/hrgtests/HrgTestsApplicationTests.java:[4,47] package com.humanresources.game.model.territory does not exist
[ERROR] /C:/Users/Fred/IdeaProjects/HumanResources/hrg-tests/src/test/java/com/humanresources/game/hrgtests/HrgTestsApplicationTests.java:[5,47] package com.humanresources.game.model.territory does not exist
[INFO] 3 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] Application 1.0-SNAPSHOT ........................... SUCCESS [  1.533 s]
[INFO] Model 0.0.1-SNAPSHOT ............................... SUCCESS [  5.104 s]
[INFO] Tests 0.0.1-SNAPSHOT ............................... FAILURE [  3.715 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  10.771 s
[INFO] Finished at: 2021-02-21T18:33:11+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:testCompile (default-testCompile) on project hrg-tests: Compilation failure: Compilation failure: 
[ERROR] /C:/Users/Fred/IdeaProjects/HumanResources/hrg-tests/src/test/java/com/humanresources/game/hrgtests/HrgTestsApplicationTests.java:[3,42] package com.humanresources.game.model.card does not exist
[ERROR] /C:/Users/Fred/IdeaProjects/HumanResources/hrg-tests/src/test/java/com/humanresources/game/hrgtests/HrgTestsApplicationTests.java:[4,47] package com.humanresources.game.model.territory does not exist
[ERROR] /C:/Users/Fred/IdeaProjects/HumanResources/hrg-tests/src/test/java/com/humanresources/game/hrgtests/HrgTestsApplicationTests.java:[5,47] package com.humanresources.game.model.territory does not exist

查看 mvn clean install 后的屏幕截图:

尝试在您的应用程序 pom.xml XML 构建元素的插件部分下添加此插件。

       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version><--your maven version--></version>
        </plugin>
    

问题诊断

Maven 执行的输出包含 hrg-model 的以下行:

[INFO] --- spring-boot-maven-plugin:2.4.3:repackage (repackage) @ hrg-model ---
[INFO] Replacing main artifact with repackaged archive

hrg-model 之后的 JAR 工件不是通常的库,而是按照 Spring Boot documentation

中所述打包的可执行 JAR

该可执行 JAR 不适合 Java 编译器使用 - 或任何其他基于 JVM 的语言的编译器。


解决方案

只有代表项目最终可执行工件的 Maven 模块才应包括 spring-boot-maven-plugin.

的调用

请注意,您的项目目前没有此类模块。

我建议:

  • 添加新模块,hrg-app 作为父模块,hrg-model 作为依赖项;
  • spring-boot-maven-plugin 调用从 hrg-app 模块移动到新模块。

您可以从 Spring Boot - Creating a Multi Module Project 查看官方指南 - 以获得分步指南和解释。