You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

IntelliJ中SpringBoot Gradle项目执行./gradlew build依赖导入失败求助

问题描述

项目此前依赖导入正常,执行./gradlew build时所有导入语句报错,例如:

error: package com.fasterxml.jackson.core does not exist import com.fasterxml.jackson.core.JsonGenerationException;

已尝试删除并刷新缓存,问题仍未解决。

build.gradle配置

plugins {
    id 'org.springframework.boot' version '2.7.0'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id 'org.unbroken-dome.test-sets' version '4.0.0'
}


group = 'com.example'
version = '0.0.1-SNAPSHOT'
//sourceCompatibility = '17'

repositories {
    mavenCentral()
}


sourceSets {
    intTest {
        java {
            srcDirs = ['src/integrationTest/java/com/example/latlong/location/LocationTest.java']
            srcDirs = ['src/main/java']

        }
        resources {
            srcDirs = ['src/integrationTest/resources']
        }
        compileClasspath += sourceSets.main.output
        runtimeClasspath += sourceSets.main.output
    }
}
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-test'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.3'
    implementation 'com.fasterxml.jackson.core:jackson-core:2.13.3'
    implementation 'com.google.code.gson:gson:2.9.0'
    implementation 'org.json:json:20220320'
    runtimeOnly 'com.h2database:h2'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    intTestImplementation 'junit:junit:4.13.2'
}


configurations {
    integrationTestImplementation.extendsFrom implementation
    integrationTestRuntimeOnly.extendsFrom runtimeOnly
}


tasks.register('integrationTest', Test) {
    description = 'Runs integration tests.'
    group = 'verification'

    testClassesDirs = sourceSets.intTest.output.classesDirs
    classpath = sourceSets.intTest.runtimeClasspath
    shouldRunAfter test
}

check.dependsOn integrationTest

相关Java类(LocationResourceQA.java)

// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.example.latlong.location;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.context.annotation.Profile;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@Profile({"qa"})
@RestController
public class LocationResourceQA {
    public LocationResourceQA() {
    }

    @GetMapping(
        path = {"/night-time-temperature"}
    )
    @ResponseBody
    public String tempStatsMOCK(@RequestParam(name = "time") @DateTimeFormat(iso = ISO.DATE_TIME) String fromDate) {
        String url = "http://localhost:8082/sunriseSunset.json";
        RestTemplate restTemplate = new RestTemplate();
        String result = (String)restTemplate.getForObject(url, String.class, new Object[0]);
        Location location = new Location(result);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        Date testDate = null;

        try {
            testDate = sdf.parse(fromDate);
        } catch (ParseException var9) {
            var9.printStackTrace();
        }

        location.tempGradient(testDate);
        return location.getTempurature();
    }
}

具体错误信息

> Task :compileIntTestJava FAILED
/home/pliakhov/Desktop/springstuff/latlong/src/main/java/com/example/latlong/location/Location.java:3: error: package com.fasterxml.jackson.core does not exist
import com.fasterxml.jackson.core.JsonGenerationException;
                                 ^
/home/pliakhov/Desktop/springstuff/latlong/src/main/java/com/example/latlong/location/Location.java:4: error: package com.fasterxml.jackson.databind does not exist
import com.fasterxml.jackson.databind.JsonMappingException;
                                     ^
/home/pliakhov/Desktop/springstuff/latlong/src/main/java/com/example/latlong/location/Location.java:5: error: package com.fasterxml.jackson.databind does not exist
import com.fasterxml.jackson.databind.JsonNode;
                                     ^
/home/pliakhov/Desktop/springstuff/latlong/src/main/java/com/example/latlong/location/Location.java:6: error: package com.fasterxml.jackson.databind does not exist
import com.fasterxml.jackson.databind.ObjectMapper;
                                     ^
/home/pliakhov/Desktop/springstuff/latlong/src/main/java/com/example/latlong/location/Location.java:7: error: package org.springframework.stereotype does not exist
import org.springframework.stereotype.Component;
                                     ^
/home/pliakhov/Desktop/springstuff/latlong/src/main/java/com/example/latlong/location/Location.java:19: error: cannot find symbol
@Component
 ^
  symbol: class Component
/home/pliakhov/Desktop/springstuff/latlong/src/main/java/com/example/latlong/location/Location.java:25: error: cannot find symbol
    private static final ObjectMapper mapper = new ObjectMapper();

Location.java代码

package com.example.latlong.location;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalTime;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
//import org.json*;

@Component
public class Location {

    public Location() {
    }

    private static final ObjectMapper mapper = new ObjectMapper();


    private String sunrise;

    private String sunset;

    private String noon;

    private String civilBegin;
    private String civilEnd;
    private String nautBegin;
    private String nautEnd;
    private String astroBegin;
    private String astroEnd;

    private String tempurature;

    public String getTempurature() {
        return tempurature;
    }

    public void setTempurature(String tempurature) {
        this.tempurature = tempurature;
    }

    public String getCivilBegin() {
        return civilBegin;
    }

    public void setCivilBegin(String civilBegin) {
        this.civilBegin = civilBegin;
    }

    public String getCivilEnd() {
        return civilEnd;
    }

    public void setCivilEnd(String civilEnd) {
        this.civilEnd = civilEnd;
    }

    public String getNautBegin() {
        return nautBegin;
    }

    public void setNautBegin(String nautBegin) {
        this.nautBegin = nautBegin;
    }

    public String getNautEnd() {
        return nautEnd;
    }

    public void setNautEnd(String nautEnd) {
        this.nautEnd = nautEnd;
    }

    public String getAstroBegin() {
        return astroBegin;
    }

    public void setAstroBegin(String astroBegin) {
        this.astroBegin = astroBegin;
    }

    public String getAstroEnd() {
        return astroEnd;
    }

    public void setAstroEnd(String astroEnd) {
        this.astroEnd = astroEnd;
    }

    public String getSunrise() {
        return sunrise;
    }

    public void setSunrise(String sunrise) {
        this.sunrise = sunrise;
    }

    public String getSunset() {
        return sunset;
    }

    public void setSunset(String sunset) {
        this.sunset = sunset;
    }

    public String getNoon() {
        return noon;
    }

    public void setNoon(String noon) {
        this.noon = noon;
    }

    public Location(String sunrise, String sunset, String noon) {
        this.sunrise = sunrise;
        this.sunset = sunset;
        this.noon = noon;
    }

    public Location(String JSONdata){
        // Deserialize the JSON data to set member variables

        try {
            JsonNode jsonNode = mapper.readTree(JSONdata);
            this.sunrise = jsonNode.get("results").get("sunrise").asText();
            this.sunset = jsonNode.get("results").get("sunset").asText();
            this.noon = jsonNode.get("results").get("solar_noon").asText();
            this.civilBegin = jsonNode.get("results").get("civil_twilight_begin").asText();
            this.civilEnd = jsonNode.get("results").get("civil_twilight_end").asText();
            this.nautBegin = jsonNode.get("results").get("nautical_twilight_begin").asText();
            this.nautEnd = jsonNode.get("results").get("nautical_twilight_end").asText();
            this.astroBegin = jsonNode.get("results").get("astronomical_twilight_begin").asText();
            this.astroEnd = jsonNode.get("results").get("astronomical_twilight_end").asText();

        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public long calcTwilightTemp (Date twiTime,Date twiTime2, Date nowTime, long initialKelvin){

            int kelvinGradient = 0;
            // Get the difference(minutes), of current & astro. rising twilight
            // time to get exact temp. gradient

            // Calculate the difference between the current and twilight boundary in mins
            int diffBet = (int) (nowTime.getTime() - twiTime.getTime());
            System.out.println("NowTime:" + nowTime.getTime()  + "twiTime: " + twiTime.getTime() + "DiffBet: " + diffBet);
            diffBet = (diffBet / 1000) / 60;
            System.out.println("diffBet: " + diffBet);

            int totalTwilightTime = (int)( twiTime2.getTime()-twiTime.getTime());
            totalTwilightTime = (totalTwilightTime /1000) / 60;

            // Calculate kelvin increase per minute
            //double kelvinPerMin = 1650 / (((twiTime2.getTime()-twiTime.getTime() / 10000) % 60));
            int kelvinPerMin = 1650 / totalTwilightTime;

            System.out.println("KelvinPermin: " + kelvinPerMin);
            int kelvinToAdd = diffBet * kelvinPerMin;
            System.out.println("kelvinToAdd: " +kelvinToAdd);
             kelvinGradient = (int) initialKelvin + kelvinToAdd;
            //System.out.println("kelvinGradient: " + kelvinGradient);
             return (long)kelvinGradient;

    }


    /*
        Function for determining tempurature gradient based off sunrise, noon and sunset respectively
     */
    public String tempGradient(Date timeToUse){

        // Parser needed for time conversion
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

        long tempGradient = 0;
        try {
            Date astroRise = sdf.parse(this.getAstroBegin());
            Date nautRise = sdf.parse(this.getNautBegin());
            Date civilRise = sdf.parse(this.getCivilBegin());
            Date civilEnd = sdf.parse(this.getCivilEnd());
            Date nautEnd = sdf.parse(this.getNautEnd());
            Date astroEnd = sdf.parse(this.getAstroEnd());

            Date currTime = timeToUse;
            System.out.println("Current Time: " + currTime);
            System.out.print(this.toString());
            // SUNRISE: Between Astronomical & Nautical twilight begin
            if (currTime.after(astroRise) && currTime.before(nautRise) ) {

                // Get the difference(minutes), of current & astro. rising twilight
                // time to get exact temp. gradient
                tempGradient = calcTwilightTemp(astroRise,nautRise,currTime,2700);

            }
            // SUNRISE: Between Nautical & Civil twilight begin
            else if(currTime.after(nautRise) && currTime.before(civilRise) ){
                tempGradient = calcTwilightTemp(nautRise,civilRise,currTime,4350);
            }
            //  NOON: Check Noon time (gradient: 6000k)
            else if (currTime.after(civilRise) && currTime.before(civilEnd)){
                    tempGradient = 6000;
            }
            // SUNSET: Check night time duration(gradient: 2700k)
            else if (currTime.after(civilEnd) && currTime.before(nautEnd)){
                tempGradient = 6000 - ( calcTwilightTemp(civilEnd,nautEnd,currTime,6000) - 6000);

            }
            else if (currTime.after(nautEnd) && currTime.before(astroEnd)){
                tempGradient = 4350 - (calcTwilightTemp(nautEnd,astroEnd,currTime,4350) - 4350);
            }
            // NIGHTTIME: Check between end of astronomical twilight, and beginning of astronomical twilight in AM
            else{
                tempGradient = 2700;
            }
//            System.out.println("Astronomical Begin: " + astroRise);
//            System.out.println("Nautical Begin: " + nautRise);
//            System.out.println("Civil End: " + civilEnd);
//            System.out.println("Nautical End: "+ nautEnd);
        }
        catch(ParseException e){
            e.printStackTrace();
        }

        String strGradient = Long.toString(tempGradient);

        System.out.println("Calculated Gradient: " + strGradient);
        this.setTempurature(strGradient);
        return strGradient;
    }

    @Override
    public String toString() {
        return "Location{" +
                "sunrise='" + sunrise + '\'' +
                ", sunset='" + sunset + '\'' +
                ", noon='" + noon + '\'' +
                ", astroBegin='" + astroBegin + '\'' +
                ", nautBegin='" + nautBegin + '\'' +
                ", civilBegin='" + civilBegin + '\'' +
                ", civilEnd='" + civilEnd + '\'' +
                ", nautEnd='" + nautEnd + '\'' +
                ", astroEnd='" + astroEnd + '\'' +
                '}';
    }
}
修复建议

1. 修正sourceSets中集成测试的源目录配置

你的intTest sourceSet配置里,将Java源目录错误地覆盖成了src/main/java,导致Gradle在编译集成测试时,会把主代码当成测试代码重新编译,且此时的依赖路径配置异常,无法找到主依赖包。

修改后的sourceSets配置:

sourceSets {
    intTest {
        java {
            // 指定集成测试的根目录,而非单个文件,避免覆盖主代码目录
            srcDir 'src/integrationTest/java'
        }
        resources {
            srcDir 'src/integrationTest/resources'
        }
        compileClasspath += sourceSets.main.output
        runtimeClasspath += sourceSets.main.output
    }
}

2. 修正依赖配置错误

将implementation 'org.springframework.boot:spring-boot-starter-test'改为testImplementation,因为spring-boot-starter-test是测试专用依赖,不应放到主依赖中:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    // 修正此处:将implementation改为testImplementation
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.3'
    implementation 'com.fasterxml.jackson.core:jackson-core:2.13.3'
    implementation 'com.google.code.gson:gson:2.9.0'
    implementation 'org.json:json:20220320'
    runtimeOnly 'com.h2database:h2'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    intTestImplementation 'junit:junit:4.13.2'
}

3. 彻底清理并重新

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 23:51:05