Java – Eclipse does not recognize gradle dependencies

Eclipse does not recognize gradle dependencies… here is a solution to the problem.

Eclipse does not recognize gradle dependencies

I’m new to Eclipse and I’m from IntelliJ 🙂 So to practice, I created a dummy Gradle project in Eclipse that doesn’t even recognize the automatically inserted JUnit dependencies.

The stack I used is as follows:

  • Gradle 6.6.1
  • Java 13
  • Eclipse 2019-09 R (4.13.0) –> Updated to 2020-09 (4.17.0) based on the following recommendations.

Things I’ve already done :

Everything from here and here, i.e.:

  1. According to this complete the prerequisites for being able to use Lombok (see code below) guidance.

  2. Installing Buildship Gradle.

  3. Insert the following script in my build.gradle:

    Application plugin: “Eclipse”

    Then run

    gradlew cleanEclipse eclipse

  4. Set automatic project synchronization in Preferences and use the other options on that tab.

  5. Refresh the dependencies and right-click.
    … There may be other things I don’t remember exactly.

My actual code is as follows (mostly auto-generated):

build.gradle:

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java Library project to get you started.
 * For more details take a look at the Java Libraries chapter in the Gradle
 * User Manual available at https://docs.gradle.org/6.3/userguide/java_library_plugin.html
 */

plugins {
     Apply the java-library plugin to add support for Java Library
    id 'java-library'
    id "io.freefair.lombok" version "5.2.1"
}

repositories {
     Use jcenter for resolving dependencies.
     You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {

compileOnly 'org.projectlombok:lombok:1.18.12'
 annotationProcessor 'org.projectlombok:lombok:1.18.12'

 only required if Lombok annotation are present in test code
 testCompileOnly 'org.projectlombok:lombok:1.18.12'
 testAnnotationProcessor 'org.projectlombok:lombok:1.18.12'
   
 This dependency is exported to consumers, that is to say found on their compile classpath.
    api 'org.apache.commons:commons-math3:3.6.1'

 This dependency is used internally, and not exposed to consumers on their own compile classpath.
    implementation 'com.google.guava:guava:28.2-jre'
    
implementation 'com.google.code.gson:gson:2.8.6'
    
 Use JUnit test framework
    testImplementation 'junit:junit:4.12'
}

Library:

package gradleproject;

public class Library {
    public boolean someLibraryMethod() {
        return true;
    }
}

Library Testing:

/*
 * This Java source file was generated by the Gradle 'init' task.
 */
package gradleproject;

import static org.junit.Assert.*;

import org.junit.Test;

public class LibraryTest {
    @Test
    public void testSomeLibraryMethod() {
        Library classUnderTest = new Library();
        assertTrue("someLibraryMethod should return 'true'", classUnderTest.someLibraryMethod());
    }
}

Animals:

package gradleproject;

import lombok. Getter;
import lombok. Setter;

@Getter
@Setter
public class Animal {
    private String name;
    private int age;
}

Neither JUnit nor Lombok dependencies are recognized after build. Without lombok dependencies, my code actually compiles, and even my tests run, but the test class itself (and the code inside) is still underscored and indicates that it can’t resolve dependencies.

If I try some other libraries, the build fails.

Do you have any suggestions?

Thanks in advance.

PS: I have updated to the latest version of Eclipse and recreated the project. Unfortunately, it didn’t solve the problem.

Solution

The first thing to note is that you are using a version of Eclipse from a year ago. Eclipse 2020-09 has just been released. I highly recommend upgrading to that one first to get the latest improvements. Second, there’s no need to install Buildship because it’s included in Eclipse out of the box.

On the Gradle side, there is no need to include Eclipse plugins either. Buildship does not use the Eclipse plugin at all. Instead, it uses the Gradle Tooling API. Using Buildship to import a project and then running gradlew cleanEclipse eclipse actually defeats the purpose of Buildship and overrides the project settings populated by Buildship.

Looking at the build.gradle provided, I can see the following issue:

  1. junit:junit:4.13 is defined in the scope of testCompile and testImplementation. Use only testImplementation.

  2. Lombok is not configured at all. When installing Lombok support in Eclipse, you still need to define dependencies in build.gradle:

     compileOnly 'org.projectlombok:lombok:1.18.12'
     annotationProcessor 'org.projectlombok:lombok:1.18.12'
    
     only required if Lombok annotation are present in test code
     testCompileOnly 'org.projectlombok:lombok:1.18.12'
     testAnnotationProcessor 'org.projectlombok:lombok:1.18.12'
    

    Recorded here: Project Lombok

One final tip: In Eclipse, open Console View and select Gradle Operations from the View menu. So that you can see the Gradle output. This might come in handy if you’re having issues with your build script.

Gradle Operations Console View

Related Problems and Solutions