如何使用gradle'api'依赖 [英] How to use gradle 'api' dependency

查看:530
本文介绍了如何使用gradle'api'依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在项目中使用'api'依赖关键字,但出现此错误,提示它找不到方法api()

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

我在一个新项目上尝试过.这是build.gradle文件:

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'
}

我正在使用gradle V4.9. 当我运行gradle build时,我得到了:

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

如果我将"api"替换为"implementation",则一切正常

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

我在这里想念什么? 有什么需要做的设置吗?

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

推荐答案

api配置来自java-library插件,在您的构建脚本中您刚刚应用了java插件.参见 https://docs.gradle.org/current/userguide/java_library_plugin.html

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

标准Java插件和Java Library插件之间的主要区别在于,后者引入了向消费者公开的API的概念.库是一个Java组件,打算由其他组件使用.在多项目构建中,这是一个非常常见的用例,但在您具有外部依赖关系时也是如此.

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.

只需应用java-library插件(扩展了java插件),它便会起作用:

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

plugins {
    id 'java-library'
}

这篇关于如何使用gradle'api'依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆