【问题标题】:Maven not picking up the classes from dependency modulesMaven 没有从依赖模块中获取类
【发布时间】:2020-09-11 09:33:14
【问题描述】:

在我的spring-boot 项目中,有两个微服务,一个配置服务器和一个命名服务器。它还有两个模块,我将它们添加为微服务中的依赖项。

下面是commons-new 模块的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.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.highpeak.tlp</groupId>
    <artifactId>commons-new</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>commons-new</name>
    <description>Contains common modules needed by all the microservices</description>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </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>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

我将它作为依赖项添加到我的微服务中。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         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.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.highpeak.tlp</groupId>
    <artifactId>ybanq-auth-manager</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ybanq-auth-manager</name>
    <description>Manages the authentication process with ybanq</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR4</spring-cloud.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.testSource>1.8</maven.compiler.testSource>
        <maven.compiler.testTarget>1.8</maven.compiler.testTarget>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.highpeak.tlp</groupId>
            <artifactId>commons-new</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.highpeak.tlp</groupId>
            <artifactId>redis-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </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>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

当我尝试使用命令mvn clean install 构建微服务时,它正在抛出compiler-error 消息。

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project ybanq-auth-manager: Compilation failure: Compilation failure: 
[ERROR] /Users/sandeshaj/Documents/Bitbucket/lawshram-payments/ybanq-auth-manager/src/main/java/com/highpeak/tlp/ybanqauthmanager/controller/AuthController.java:[3,42] package com.highpeak.tlp.commons.exception does not exist
[ERROR] /Users/sandeshaj/Documents/Bitbucket/lawshram-payments/ybanq-auth-manager/src/main/java/com/highpeak/tlp/ybanqauthmanager/controller/AuthController.java:[4,37] package com.highpeak.tlp.commons.util does not exist
[ERROR] /Users/sandeshaj/Documents/Bitbucket/lawshram-payments/ybanq-auth-manager/src/main/java/com/highpeak/tlp/ybanqauthmanager/service/AuthManagerService.java:[3,42] package com.highpeak.tlp.commons.exception does not exist
[ERROR] /Users/sandeshaj/Documents/Bitbucket/lawshram-payments/ybanq-auth-manager/src/main/java/com/highpeak/tlp/ybanqauthmanager/service/AuthManagerService.java:[11,51] cannot find symbol
[ERROR]   symbol:   class DataException
[ERROR]   location: interface com.highpeak.tlp.ybanqauthmanager.service.AuthManagerService
[ERROR] /Users/sandeshaj/Documents/Bitbucket/lawshram-payments/ybanq-auth-manager/src/main/java/com/highpeak/tlp/ybanqauthmanager/service/AuthManagerServiceImpl.java:[19,42] package com.highpeak.tlp.commons.exception does not exist

但是,在 IntelliJ 编辑器中,这些类是可访问的,并且 Intellij 没有显示编译器错误。但是当我在 IntelliJ 中运行应用程序时,它会显示相同的错误。

我犯了什么错误?由于我将模块添加为依赖项,因此它们的类应该可用,对吧?

我的系统maven版本是3.5.4

【问题讨论】:

  • 您应该对commons-new 再次使用命令mvn clean install。我认为它是您的 .m2 存储库中以前的库。然后,用你的微服务模块重试mvn clean install
  • 我多次这样做。没有运气
  • 你能打开你的.m2的commons-new jar 并检查是否有需要的包吗?
  • 是的。没错
  • 你可以尝试从 common-new 中删除 spring boot 插件吗?

标签: java spring-boot maven microservices


【解决方案1】:

Spring Boot 插件打包所有依赖 jar 并将它们拉入您的模块构建的 jar 中。这个 jar 的结构是专门为 Spring Boot 可运行应用程序设计的,与普通的 jar 结构不兼容,所以不能使用 Spring Boot Application jar 作为 maven 依赖 jar。

【讨论】:

    【解决方案2】:

    从子模块中移除 spring boot 插件(common-new)。 我有类似的问题,但使用 gradle 并找到了这个Unresolved Dependency on package in subproject

    不幸的是,我不知道在子模块/子项目中“使用”spring boot 插件的确切问题。

    【讨论】:

    • Spring Boot 插件创建了一个更像耳朵或战争的可运行 jar。如果你解压 spring boot jar 你会看到它和一个简单的库 jar 有什么不同。
    • 谢谢。我是这么想的
    猜你喜欢
    • 2020-11-29
    • 2010-10-22
    • 1970-01-01
    • 2015-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多