Maven转Gradle构建报错:依赖缺失排查与编译配置方案
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 compileClasspathScan 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
dependencyInsightto zoom in on the specific artifact:gradle :***-war:dependencyInsight --dependency org.mockito:mockito-core --configuration compileClasspathThis 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:
- Identify the parent dependency: Use the
dependencyInsightcommand above to find which library is requesting mockito-core:2.8.9. - 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 } - 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' }
- If version 2.8.9 isn’t available in your repos, force a compatible version that exists:
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
platformkeyword 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

