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

JavaGradleDependencies

Java Problem Overview


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

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

Also, should this be in my applications build.gradle or in the module specific build.gradle?

Current build.gradle (at application level):

apply plugin: 'java'

repositories {
    mavenCentral()
}

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

Java Solutions


Solution 1 - Java

If buildscript itself needs something to run, use classpath.

If your project needs something to run, use compile.

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

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

Solution 2 - Java

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

> A configuration is simply a named set of dependencies.

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

Solution 3 - Java

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

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

I think you should be already familiar with the Project.dependencies script 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:

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.)

In Gradle Tutorial: Organizing Build Logic it says:

> 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.

I hope things are getting clear to you now.

With classpath "com.android.tools.build:gradle:${Versions.android_gradle_plugin}" we're setting classpath method 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.

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.

tl;dr: The classpath, compile, and implementation are 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 configuration you may want to declare.

Solution 4 - Java

Android:

classpath in project build.gradle —— the implementation after classpath is only used by gradle it self, used in build script. So if i add the implementation (such as retrofit) in the project build.gradle classpath 'retrofit...', i can't get retrofit in my code!! Because —— my code can't see it, only the buildscript can see it.

implementation in app build.gradle —— add the implementation your code can use!!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionjava123999View Question on Stackoverflow
Solution 1 - Javaq...View Answer on Stackoverflow
Solution 2 - JavaEric WendelinView Answer on Stackoverflow
Solution 3 - JavaTeng-pao YuView Answer on Stackoverflow
Solution 4 - Javajunyan xieView Answer on Stackoverflow