Maven 找不到从另一个模块导入的类,但 Intellij 找到了

And*_*yan 10 java compilation intellij-idea maven multi-module

我有多模块 Maven 项目。Acceptance -tests模块依赖于pom.xml中的api模块(为了保密,用xxx替换真实的公司名称)。我正在尝试在验收测试中从api模块导入一些类。

这是我对验收测试模块的pom.xml依赖项:

        <dependency>
            <artifactId>xxx-api</artifactId>
            <groupId>com.xxx</groupId>
            <version>${xxx.api.version}</version>
        </dependency>
Run Code Online (Sandbox Code Playgroud)

api模块由 maven单独安装和打包(mvn installmvn package),没有任何问题。jar文件正在我的本地.m2创建。

但是,当我尝试编译验收测试模块时,出现编译错误,指出无法导入类,因为找不到包。

这是实际的错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project xxx-acceptance-tests: Compilation failure: Compilation failure: 
[ERROR] /Users/andranik_chorokhyan/mydisk/Projects/XXX/automation/xxx-project-test-automation/xxx-acceptance-tests/src/main/java/com/xxx/xxx/utilities/api/ApiPayloadUtils.java:[17,38] package com.xxx.domain.dto does not exist
[ERROR] /Users/andranik_chorokhyan/mydisk/Projects/XXX/automation/xxx-project-test-automation/xxx-acceptance-tests/src/main/java/com/xxx/xxx/utilities/api/ApiPayloadUtils.java:[18,38] package com.xxx.domain.dto does not exist
[ERROR]   symbol:   class MappingData
[ERROR]   location: class com.xxx.utilities.api.ApiPayloadUtils
Run Code Online (Sandbox Code Playgroud)

一个更有趣的事实是 Intellij IDEA 中没有可见的错误。没有红色下划线,没有编译错误,导航到适当的导入文件没有问题。实际上,com.xxx.domain.dto包和MappingData类确实存在。

我从本地.m2存储库中删除了整个xxx目录并执行了命令。它也成功了。mvn clean dependency:resolve

有谁知道这里有什么问题以及如何解决?提前致谢!

özk*_*dil 9

我在使用 maven 编译时也遇到了符号未找到错误,解决方案是对于 spring boot 2,您需要按如下方式配置插件,classifier exec

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <classifier>exec</classifier>
        </configuration>
    </plugin>
Run Code Online (Sandbox Code Playgroud)

如果您正在使用 Spring Boot 1

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
                <configuration>
                    <classifier>exec</classifier>
                </configuration>
            </execution>
        </executions>
    </plugin>
Run Code Online (Sandbox Code Playgroud)


And*_*yan 7

最后我找到了解决方案。感谢JF Meierkhmarbaise的提示。

看来 Maven 不允许依赖可执行 jar。这就是我的情况。我的api模块是一个可执行的Spring Boot应用程序,而不是可重用的库。

所以,解决方案如下:

  1. 有必要Application.java在模块中找到该文件api
  2. 添加maven-jar-plugin排除Application.java某些分类器的文件和规范
  3. acceptance-tests从上面指定的分类器而不是标准 jar 在模块中建立依赖关系

api模块中的插件规范如下:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <classifier>qa</classifier>
                            <excludes>
                                <exclude>**/Application*</exclude>
                            </excludes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
Run Code Online (Sandbox Code Playgroud)

下面的验收测试模块的依赖性:

        <dependency>
            <artifactId>xxx-api</artifactId>
            <groupId>com.xxx</groupId>
            <version>${api.version}</version>
            <classifier>qa</classifier>
        </dependency>
Run Code Online (Sandbox Code Playgroud)