Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use gradle 'api' dependency

Tags:

java

gradle

I tried using the 'api' dependency keyword in my project , but I got this error saying it cannot find method api()

I tried it on a new project. this is the build.gradle file:

plugins {
    id 'java'
}

group 'com.test'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    api group: 'com.google.guava', name: 'guava', version: '27.0.1-jre'
}

I am using gradle V4.9. when I run gradle build I get this:

Could not find method api() for arguments [{group=com.google.guava, name=guava, version=27.0.1-jre}] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler

If I replace 'api' with 'implementation' everything works fine

What am I missing here? Is there any setting that needs to be done?

like image 782
Nir Brachel Avatar asked Dec 11 '18 14:12

Nir Brachel


1 Answers

The api configuration comes from the java-library plugin, in your build script you have just applied java plugin. See https://docs.gradle.org/current/userguide/java_library_plugin.html

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. A library is a Java component meant to be consumed by other components. It’s a very common use case in multi-project builds, but also as soon as you have external dependencies.

Just apply the java-library plugin (which extends java plugin) and it should work:

plugins {
    id 'java-library'
}
like image 128
M.Ricciuti Avatar answered Sep 23 '22 06:09

M.Ricciuti