首页 > 解决方案 > Eclipse Spring Boot 需要显式声明 Maven 依赖项

问题描述

我有 2 台开发 PC,均具有以下规格:

正在开发的应用程序是一个带有以下 Maven 文件的 Spring Boot 项目:

<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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.my_company.example</groupId>
    <artifactId>test</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>

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

    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-websocket -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/net.sf.jasperreports/jasperreports -->
        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>6.7.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.powermock/powermock-module-junit4 -->
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>2.0.7</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.powermock/powermock-api-mockito2 -->
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito2</artifactId>
            <version>2.0.7</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.powermock/powermock-module-junit4-rule-agent -->
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4-rule-agent</artifactId>
            <version>2.0.7</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.powermock/powermock-module-javaagent -->
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-javaagent</artifactId>
            <version>2.0.7</version>
            <scope>test</scope>
        </dependency>
        
    </dependencies>

    <modules>
        <module>core</module>
        <module>module1</module>
        <module>module2</module>
    </modules>
</project>

将项目导入 Eclipse 时,其中一台开发 PC(称为 PC1)无法构建并出现以下错误:

在此处输入图像描述

在另一端PC(称为PC2)上,错误没有发生,它可以构建并成功运行。

我检查了两台 PC,Maven 依赖项仍然可用: 在此处输入图像描述

为了解决该错误,我们在 POM.xml 文件中添加了以下依赖项:

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
</dependency>

但是,我想问为什么在PC1中需要显式添加依赖,而在PC2中则不需要?

Run As > Maven Build... > select "package" in Goals field后来,我还从 Eclipse ( )构建了 JAR+WAR 文件。然后将 JAR+WAR 文件部署到 Docker 容器(基础镜像为tomcat:9.0.11-jre8)。发生了类似的问题,从 PC1 构建的 JAR 文件无法运行,而从 PC2 构建的 JAR 文件仍然能够运行。错误信息:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.cfg.beanvalidation.IntegrationException: Error activating Bean Validation integration

我也认为问题来自上面的PC1。任何人都可以帮助检查这些问题是否有任何可能的原因?

更新

根据@M.Deinumand的回答@OneCricketeer,我解决了这个问题。
在 PC1 中,我安装了 JDK8,添加到 PATH。
Eclipse IDE 从以下链接下载:

https://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/2021-03/R/eclipse-java-2021-03-R-win32-x86_64.zip

检查配置时,它仍然使用默认的 Java 15,如下所示: 在此处输入图像描述

所以,解决方法是点击Add...上图,然后选择安装好的JDK(C:\Program Files\Java\jdk1.8.0_291)。

更新 2

这是对 的反馈@OneCricketeer。从计算机上卸载了 JDK,但 Eclipse 仍然能够运行 Java 项目。

在此处输入图像描述

标签: javamavenpom.xml

解决方案


安装 Oracle JDK 1.8.0_281-b09

一些事情

  1. 其中一台计算机未使用 Java 8 编译代码,否则 jaxb 将可用

  2. 除非您要维护旧代码,否则您至少应该使用 Java 11

  3. 如果你绝对需要 Java 8,最好迁移到 OpenJDK 而不是 Oracle 的发行版

如果是全新的项目,你可以使用任何更新的Java版本,但值得指出的是Spring Boot版本也应该更新


推荐阅读