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

Maven转Gradle构建报错:依赖缺失排查与编译配置方案

Troubleshooting Undeclared Gradle Dependencies & Missing Artifacts

Issue 1: Gradle referencing JARs not declared in POM or code

Those "mystery" JARs are almost certainly transitive dependencies—pulled in indirectly by one of your direct dependencies. Here’s how to track them down:

  • Generate a full dependency tree: Run this command to visualize every dependency (direct and transitive) for your compile classpath:

    gradle :***-war:dependencies --configuration compileClasspath
    

    Scan the output for the artifact in question; you’ll see exactly which parent dependency is bringing it in.

  • Pinpoint the exact source: If the tree is too cluttered, use dependencyInsight to zoom in on the specific artifact:

    gradle :***-war:dependencyInsight --dependency org.mockito:mockito-core --configuration compileClasspath
    

    This will show the full chain of dependencies leading to the unexpected JAR.

  • Exclude unwanted transitive dependencies: If the JAR is unnecessary, exclude it from its parent dependency. Example:

    implementation 'com.example:some-dependency:1.0.0' {
        exclude group: 'org.mockito', module: 'mockito-core'
    }
    

Issue 2: Missing mockito-core.jar after Maven-to-Gradle migration

Even if you don’t reference it directly, mockito-core is a transitive dependency required by another library. Here’s how to fix the resolution error:

  1. Identify the parent dependency: Use the dependencyInsight command above to find which library is requesting mockito-core:2.8.9.
  2. Verify your repositories: Ensure your Gradle build script includes repositories that host this artifact (like Maven Central):
    repositories {
        mavenCentral()
        // Add any private repos your team uses
    }
    
  3. Resolve version conflicts or unavailability:
    • If version 2.8.9 isn’t available in your repos, force a compatible version that exists:
      configurations.all {
          resolutionStrategy.force 'org.mockito:mockito-core:3.12.4'
      }
      
    • Or exclude the problematic transitive dependency and add a valid version explicitly:
      implementation 'org.mockito:mockito-core:3.12.4'
      implementation 'com.example:problematic-dependency:1.0.0' {
          exclude group: 'org.mockito', module: 'mockito-core'
      }
      

Handling POM-only artifacts (no JAR available)

If a module only has a POM file (common for BOMs, parent POMs, or dependency aggregators), configure Gradle to process it correctly:

  • For BOMs (Bill of Materials): Use Gradle’s platform keyword to import the POM and inherit standardized dependency versions:
    implementation platform('com.example:my-bom:1.0.0')
    
  • For regular POM-only modules: Declare the dependency with type: 'pom' to ensure Gradle resolves its transitive dependencies:
    implementation 'com.example:pom-only-module:1.0.0', {
        type = 'pom'
        transitive = true // Default behavior, explicit here for clarity
    }
    
  • Local POM files: If the POM lives on your filesystem, add a flat directory repository to your build script:
    repositories {
        flatDir {
            dirs '/path/to/pom-directory'
        }
    }
    

内容的提问来源于stack exchange,提问作者Laxmanrao Potadar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:17:29