Eclipse does not recognize gradle dependencies

3.7k views Asked by At

I am relatively new to Eclipse, I am an IntelliJ guy :) So to practice, I have made a dummy Gradle project in Eclipse, and it is not even recognizing the automatically inserted JUnit dependencies.

The stack I am using is the following:

  • Gradle 6.6.1
  • Java 13
  • Eclipse 2019-09 R (4.13.0) --> updated to 2020-09 (4.17.0) as per the suggestions below.

The things I have already done:

Everything from here and here, namely:

  1. Doing the prerequisites to be able to use Lombok (see the code below) according to this guide.

  2. Installing Buildship Gradle.

  3. Inserting the following scripts in my build.gradle:

    apply plugin: "eclipse"

    and afterwards running

    gradlew cleanEclipse eclipse

  4. Setting automatic project synchronization in Preferences and playing with other options on that tab.

  5. Refreshing dependencies and right-clicking. ... and possibly some other things I can not recall properly.

My actual code is the following (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;
    }
}

LibraryTest:

/*
 * 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());
    }
}

Animal:

package gradleproject;

import lombok.Getter;
import lombok.Setter;

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

Neither the JUnit, nor the Lombok dependencies are recognized after building. Without the lombok dependency, my code actually compiles, even my test runs, but the test class itself (and the code inside) is still underlined and says that it can't resolve the dependencies.

If I try some other libraries, the build fails.

Could you have any suggestions?

Thanks in advance.

PS: I have updated to the latest version of Eclipse and recreated the project. Lamentably, it did not solve the issue.

2

There are 2 answers

0
lyancsie On BEST ANSWER

Eventually, I could solve the problem. The issue was that Gradle somehow did not download the referred projects in the external dependencies folder from the outside repo.

The things I have done:

  • Updated Gradle version (as you might have seen in the edited question)
  • Went to Window/Preferences and to Gradle wrapper instead of the local one
  • Turned on project synchronization (also at Window/Preferences)

Subsequently, during the next build process, Gradle downloaded all the referred jars and everything works properly.

Thanks for your help.

1
Thomas K. On

The first thing to note is that you're using a year old version of Eclipse. Eclipse 2020-09 was just recently released. I highly recommend to upgrade to that first to get the latest improvements. Second, there's no need to install Buildship since it's included in Eclipse out of the box.

On the Gradle side, there's also no need to include the Eclipse Plugin. Buildship doesn't use the Eclipse Plugin at all. Instead, it uses the Gradle Tooling API. Importing the project with Buildship and then running gradlew cleanEclipse eclipse actually defeats the purpose of Buildship and overwrites project settings populated by Buildship.

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

  1. junit:junit:4.13 is defined in testCompile and testImplementation scopes. Use testImplementation only.

  2. Lombok is not configured at all. While you installed Lombok support in Eclipse, you still need to define a dependency in your 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'
    

    Documented here: Project Lombok

One last hint: In Eclipse, open the Console view and select Gradle Operations from the view menu. This way you see the Gradle output. This might come in handy should there be issues with your build script.

Gradle Operations Console View