Eclipse不识别gradle依赖

huangapple go评论79阅读模式
英文:

Eclipse does not recognize gradle dependencies

问题

我对Eclipse相对较新,我是一个IntelliJ的用户 Eclipse不识别gradle依赖 为了练习,我在Eclipse中创建了一个虚拟的Gradle项目,但它甚至不能识别自动插入的JUnit依赖项。

我使用的堆栈如下:

  • Gradle 6.6.1
  • Java 13
  • Eclipse 2019-09 R (4.13.0) --> 根据下面的建议已更新至2020-09 (4.17.0) 版本。

我已经完成的步骤:

这里这里中的所有步骤,即:

  1. 根据这篇指南,执行使用Lombok的前提条件(请参考下面的代码)。

  2. 安装Buildship Gradle插件。

  3. 在我的build.gradle中插入以下脚本:

    apply plugin: "eclipse"

然后运行:

gradlew cleanEclipse eclipse
  1. 在首选项中设置自动项目同步,并尝试该选项卡上的其他选项。
  2. 刷新依赖项并右键单击。
    ... 还可能有一些我无法正确回忆的其他事项。

我的实际代码如下(大部分是自动生成的):

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

在构建后,无法识别JUnit或Lombok依赖项。没有lombok依赖项,我的代码实际上可以编译,甚至我的测试可以运行,但测试类本身(以及内部的代码)仍然被下划线标记,并显示无法解析依赖项。

如果尝试其他库,构建将失败。

你是否有任何建议?

提前感谢。

PS:我已经升级到最新版本的Eclipse并重新创建了项目。遗憾的是,这没有解决问题。

英文:

I am relatively new to Eclipse, I am an IntelliJ guy Eclipse不识别gradle依赖 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.

答案1

得分: 3

首先要注意的是您正在使用一年前的 Eclipse 版本。Eclipse 2020-09 最近刚发布。我强烈建议您先升级到该版本,以获取最新的改进。其次,无需安装 Buildship,因为它已经包含在 Eclipse 中。

在 Gradle 方面,也无需包含 Eclipse 插件。Buildship 根本不使用 Eclipse 插件。相反,它使用 Gradle Tooling API。使用 Buildship 导入项目,然后运行 gradlew cleanEclipse eclipse 实际上会破坏 Buildship 的目的,并覆盖由 Buildship 填充的项目设置。

从提供的 build.gradle 文件来看,我可以看到以下问题:

  1. junit:junit:4.13testCompiletestImplementation 范围内都有定义。只使用 testImplementation

  2. Lombok 根本没有配置。尽管您在 Eclipse 中安装了 Lombok 支持,但仍需要在您的 build.gradle 中定义依赖关系:

     compileOnly 'org.projectlombok:lombok:1.18.12'
     annotationProcessor 'org.projectlombok:lombok:1.18.12'
    
     // 仅在测试代码中存在 Lombok 注解时才需要
     testCompileOnly 'org.projectlombok:lombok:1.18.12'
     testAnnotationProcessor 'org.projectlombok:lombok:1.18.12'
    

    在此处记录:Project Lombok

最后一个提示:在 Eclipse 中,打开 Console 视图,并从视图菜单中选择 Gradle 操作。这样您就可以看到 Gradle 的输出。如果构建脚本存在问题,这可能会很有用。

Eclipse不识别gradle依赖

英文:

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.

Eclipse不识别gradle依赖

答案2

得分: 1

最终,我解决了这个问题。问题在于Gradle未能从外部仓库下载所引用项目到外部依赖文件夹中。

我所做的事情:

  • 更新了Gradle版本(就像你在编辑的问题中看到的那样)
  • 转到Window/Preferences并切换到Gradle包装器而不是本地包装器
  • 启用了项目同步(同样在Window/Preferences中)

随后,在下一次构建过程中,Gradle下载了所有引用的jar包,一切都正常工作了。

感谢你的帮助。

英文:

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.

huangapple
  • 本文由 发表于 2020年9月25日 09:27:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64056528.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定