Java Gradle:类路径和编译依赖有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34286407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:30:23  来源:igfitidea点击:

Gradle: What is the difference between classpath and compile dependencies?

javagradledependencies

提问by java123999

When adding dependencies to my project I am never sure what prefix I should give them, e.g. "classpath"or "compile".

在向我的项目添加依赖项时,我永远不确定我应该给它们什么前缀,例如"classpath""compile".

For example, should my dependencies below be compile time or classpath?

例如,我下面的依赖项应该是编译时还是类路径?

Also, should this be in my applicationsbuild.gradle or in the modulespecific build.gradle?

另外,这应该在我的应用程序build.gradle 中还是在模块特定的 build.gradle 中?

Current build.gradle (at application level):

当前 build.gradle(在应用程序级别):

apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.hibernate:hibernate-core:5.0.5.Final'
    compile 'mysql:mysql-connector-java:5.1.38'
} 

采纳答案by Eric Wendelin

I'm going to guess that you're referencing compileand classpathwithin the dependencies {}block. If that is so, those are dependency Configurations.

我猜你是在引用compileclasspathdependencies {}块内。如果是这样,那些是依赖配置

A configuration is simply a named set of dependencies.

配置只是一组命名的依赖项。

The compileconfiguration is created by the Java plugin. The classpathconfiguration is commonly seen in the buildSrc {}block where one needs to declare dependencies for the build.gradle, itself(for plugins, perhaps).

compile配置由 Java 插件创建。该classpath配置常见于buildSrc {}需要声明build.gradle 本身的依赖项的块中(可能是插件)。

回答by q...

If buildscriptitself needs something to run, use classpath.

如果buildscript本身需要运行某些东西,请使用classpath

If your project needs something to run, use compile.

如果您的项目需要运行某些内容,请使用compile

The buildscript{}block is for the build.gradle itself.

buildscript{}块用于 build.gradle 本身。

For multi-project building, the top-level build file is for the root project, the specific build file is for sub-project (module).

对于多项目构建,顶层构建文件针对根项目,具体构建文件针对子项目(模块)。

Top-level build file where you can add configuration options common to all sub-projects/modules.

顶级构建文件,您可以在其中添加所有子项目/模块通用的配置选项。

Do not place your application dependencies in top-level build file, they belong in the individual module build.gradle files

不要将您的应用程序依赖项放在顶级构建文件中,它们属于单个模块 build.gradle 文件

回答by Teng-pao Yu

If I understand correctly, you're confusing Project.dependenciesscript block with the Project.buildscript.dependenciesscript block (just like I did when I reached this question).

如果我理解正确,您会将Project.dependencies脚本块与Project.buildscript.dependencies脚本块混淆(就像我遇到这个问题时所做的那样)。

I'll try to answer this with what I found.

我会尝试用我发现的来回答这个问题。

I think you should be already familiar with the Project.dependenciesscript block. In this block, we declare dependencies that are required by our source code. There are several ways to declare a dependency that we need for the project. See Gradle Tutorial: Dependency Types. I'll only mention the part that is the most relevant to this problem:

我想你应该已经熟悉Project.dependencies脚本块了。在此块中,我们声明了源代码所需的依赖项。有几种方法可以声明项目所需的依赖项。请参阅Gradle 教程:依赖类型。我只会提到与这个问题最相关的部分:

compile 'org.hibernate:hibernate-core:5.0.5.Final'is a module dependency declaration. The compile configuration (which is now deprecated by the implementation configuration.) is merely a keyword for Implementation only dependencies.It is not a keyword describing which type of dependency it is (by type here I'm following the three types defined in the tutorial, i.e. module, file, and project.)

compile 'org.hibernate:hibernate-core:5.0.5.Final'是一个模块依赖声明。编译配置(现在已被实现配置弃用。)只是一个关键字,Implementation only dependencies.它不是描述它是哪种类型的依赖项的关键字(这里的类型我遵循教程中定义的三种类型,即模块,文件和项目。)

In Gradle Tutorial: Organizing Build Logicit says:

Gradle 教程:组织构建逻辑中,它说:

If your build script needs to use external libraries, you can add them to the script's classpath in the build script itself. You do this using the buildscript() method, passing in a closure which declares the build script classpath.

This is the same way you declare, for example, the Java compilation classpath. You can use any of the dependency types described in Dependency Types, except project dependencies.

Having declared the build script classpath, you can use the classes in your build script as you would any other classes on the classpath.

如果您的构建脚本需要使用外部库,您可以将它们添加到构建脚本本身的脚本类路径中。您可以使用 buildscript() 方法执行此操作,传入一个声明构建脚本类路径的闭包。

这与您声明 Java 编译类路径的方式相同。您可以使用依赖类型中描述的任何依赖类型,但项目依赖除外。

声明了构建脚本类路径后,您可以像使用类路径上的任何其他类一样使用构建脚本中的类。

I hope things are getting clear to you now.

我希望你现在清楚了。

With classpath "com.android.tools.build:gradle:${Versions.android_gradle_plugin}"we're setting classpathmethod with com.android.tools.build:gradle:${Versions.android_gradle_plugin}which is a module dependency that is used by the build script itself rather than the source in your project.

使用classpath "com.android.tools.build:gradle:${Versions.android_gradle_plugin}"我们设置的classpath方法,com.android.tools.build:gradle:${Versions.android_gradle_plugin}它是由构建脚本本身而不是项目中的源使用的模块依赖项。

On the other hand, with compile 'org.hibernate:hibernate-core:5.0.5.Final'we're declaring a module dependency required for your project with the compile configuration.

另一方面,compile 'org.hibernate:hibernate-core:5.0.5.Final'我们使用 compile configuration声明您的项目所需的模块依赖项。

tl;dr: The classpath, compile, and implementationare all keywords that can be used against dependencies under different circumstances. The former is used when you want to pass in a dependency to the build script, and the latter is one of the configurationyou may want to declare.

TL;博士:本classpathcompile以及implementation是可以针对不同的情况下依赖使用的所有关键字。前者用于将依赖项传递给构建脚本时使用,后者是您可能想要声明的配置之一。