Ashok Koyi
Ashok Koyi

Reputation: 5397

Multi module project in gradle fails compile when

I am using gradle 5.1.1 & have the following configuration in my multi module project

settings.gradle

rootProject.name = 'multi-module-test'

include 'mock-api', 'mock-impl'

build.gradle

group 'com.acme'
version '1.0.0-SNAPSHOT'

subprojects {
  apply plugin: 'java'
  sourceCompatibility = 1.8

  repositories {
    mavenCentral()
  }
}

project(':mock-impl') {
  dependencies {
    // this fails
    // api project(':mock-api')

    // this succeeds
    implementation project(':mock-api')
  }
}

For some strange reason if I use api configuration, the build fails with this reason

Could not find method api() for arguments [project ':mock-api'] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

But it does not fail when I use implementation configuration

Any idea why this is the case?

Upvotes: 1

Views: 801

Answers (1)

Laksitha Ranasingha
Laksitha Ranasingha

Reputation: 4507

You face this problem because you don't use java-library plugin. Have a look at the gradle docs, it says;

The key difference between the standard Java plugin and the Java Library plugin is that the latter introduces the concept of an API exposed to consumers.

So try including;

 plugins {
    id 'java-library'
}

Reference: https://docs.gradle.org/5.1.1/userguide/java_library_plugin.html#header

Upvotes: 2

Related Questions