【问题标题】:gradle does not fetch dependencies after upgrade from 5.1.1 to 6.4.1从 5.1.1 升级到 6.4.1 后,gradle 不获取依赖项
【发布时间】:2020-05-20 05:33:34
【问题描述】:

我有几个使用 gradle 5.1.1 和 java 8 的服务。

由于我们要升级到Java 13,所以我们首先需要升级到gradle 6,这样做之后,有些依赖没有获取。

这些依赖项在依赖项下以compile() 列出,该依赖项是我们的 jar 库,仍然使用 gradle 5.1.1 构建

我们的库存储在 S3 存储桶中,我们使用 shadowjar 生成最终 jar。


所以,例如:

我有想要升级的项目 A。 项目 A 将项目 B 作为依赖项(编译) 项目 B 有 google guava 作为依赖项(也有编译)

现在,项目 A,在 gradle 5.1.1 下获取 guava 没有问题,提醒我升级到 gradle 6 后它缺少 guava。

我使用本地计算机安装的 gradle(不是包装器)。

这里是重要的 build.gradle 部分:

buildscript {

    repositories {
        jcenter()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }

    ext.ver = [
            'springboot': '2.2.0.RELEASE',
            'slf4j'     : '1.7.12'
    ]

    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:${ver.springboot}"
        classpath 'io.spring.gradle:dependency-management-plugin:1.0.7.BUILD-SNAPSHOT'
        classpath 'com.github.jengelman.gradle.plugins:shadow:5.2.0'
        classpath 'com.amazonaws:aws-java-sdk-core:1.11.5'
    }
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'maven'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'maven-publish'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

compileJava {
    sourceCompatibility = JavaVersion.VERSION_1_8
}

configurations {
    compile.exclude module: 'spring-boot-starter-logging'
    testCompile.exclude module: 'spring-boot-starter-logging'
    runtime.exclude module: 'spring-boot-starter-logging'

    compile.exclude group: 'ch.qos.logback'
}

configurations.all {
    resolutionStrategy.cacheDynamicVersionsFor 10, 'seconds'
    resolutionStrategy.cacheChangingModulesFor 10, 'seconds'
}

dependencyManagement {
    applyMavenExclusions = false
}

repositories {
    mavenLocal()
    maven {
        url "s3://bucket"
        credentials(AwsCredentials) {
            accessKey = awsCredentials.AWSAccessKeyId
            secretKey = awsCredentials.AWSSecretKey
        }
        metadataSources {
            artifact()
        }
    }
    mavenCentral()
}


dependencies {
    compile("com.test:projectB:1.0.0")
     ...
}

import com.github.jengelman.gradle.plugins.shadow.transformers.PropertiesFileTransformer

shadowJar {
    classifier = ''
    baseName = 'project-A'
    manifest {
        attributes 'Main-Class': 'com.test.projectA.Starter'
    }
    mergeServiceFiles()
    append 'META-INF/spring.handlers'
    append 'META-INF/spring.schemas'
    append 'META-INF/spring.tooling'
    transform(PropertiesFileTransformer) {
        paths = ['META-INF/spring.factories']
        mergeStrategy = "append"
    }
}

这可能是因为项目 B 不是用新的 gradle 构建的吗?

不幸的是,我无法创建真正的复制器,因为这些库是我所在公司的真实代码。

感谢和问候, 伊多

【问题讨论】:

    标签: gradle


    【解决方案1】:

    s3bucket Maven 存储库的 metadataSources 声明很可能是无法解析 projectB 的传递依赖关系的根本原因。 documentation 在这里有点模糊,但我怀疑 artifact() 只查找实际的 jar 文件,而不是 POM 文件,因此不执行传递依赖解析。在使用开关 --info--refresh-dependencies 运行构建时,您应该能够看到此行为。

    谢天谢地,这很容易解决。添加mavenPom(),Gradle 将首先尝试解析 POM,然后依赖解析应该恢复正常。

    当您使用它时,您可能希望阅读upgrading from Gradle 5 guide 并摆脱compile 配置以支持implementation。使用--warning-mode all 运行构建时,您应该能够看到类似于此的警告:

    The compile configuration has been deprecated for dependency declaration. This will fail with an error in Gradle 7.0. Please use the implementation or api configuration instead. Consult the upgrading guide for further information: https://docs.gradle.org/6.4.1/userguide/upgrading_version_5.html#dependencies_should_no_longer_be_declared_using_the_compile_and_runtime_configurations
    

    【讨论】:

    • 照你说的做了,效果很好。谢谢。现在我有另一个问题:
    • 项目 B 有 jedis 依赖。在运行时我得到:java.lang.NoClassDefFoundError: redis/clients/jedis/params/geo/GeoRadiusParam 我可以在 intellij 的依赖项中看到 jedis,我也可以看到 GeoRadiusParam 类。有任何想法吗?这可以连接到我的 pubblish 到 S3 吗?它只有 S3 凭据。没有mavenPom() 也没有artifact()
    • @IdoBarash 从提供的信息中无法判断。可能是项目 B 的问题。您可以发布一个包含更多详细信息的新问题。
    • 找到了。。谢谢。是关于实现和api函数的。
    猜你喜欢
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 2017-12-23
    • 1970-01-01
    • 2014-12-25
    • 2016-10-26
    相关资源
    最近更新 更多