3

我对 Eclipse 比较陌生,我是 IntelliJ 人 :) 所以为了练习,我在 Eclipse 中创建了一个虚拟 Gradle 项目,它甚至无法识别自动插入的 JUnit 依赖项。

我正在使用的堆栈如下:

  • 摇篮 6.6.1
  • 爪哇 13
  • Eclipse 2019-09 R (4.13.0) -->根据以下建议更新到 2020-09 (4.17.0)

我已经做过的事情:

来自这里这里的一切,即:

  1. 根据本指南完成能够使用 Lombok 的先决条件(请参阅下面的代码) 。

  2. 安装 Buildship Gradle。

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

    应用插件:“日食”

    然后运行

    gradlew cleanEclipse 日食

  4. 在首选项中设置自动项目同步并在该选项卡上使用其他选项。

  5. 刷新依赖并右键单击。...可能还有其他一些我无法正确回忆的事情。

我的实际代码如下(主要是自动生成的):

构建.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'
}

图书馆:

package gradleproject;

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

图书馆测试:

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

动物:

package gradleproject;

import lombok.Getter;
import lombok.Setter;

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

JUnit 和 Lombok 依赖项在构建后都不会被识别。如果没有 lombok 依赖项,我的代码实际上会编译,甚至我的测试也会运行,但测试类本身(和里面的代码)仍然带有下划线,并表示它无法解析依赖项。

如果我尝试其他一些库,构建会失败。

你有什么建议吗?

提前致谢。

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

4

2 回答 2

2

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

在 Gradle 方面,也不需要包含Eclipse Plugin。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'
    
     // only required if Lombok annotation are present in test code
     testCompileOnly 'org.projectlombok:lombok:1.18.12'
     testAnnotationProcessor 'org.projectlombok:lombok:1.18.12'
    

    记录在这里:Project Lombok

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

Gradle 操作控制台视图

于 2020-09-25T11:56:22.597 回答
1

最终,我可以解决问题。问题是 Gradle 不知何故没有从外部 repo 下载外部依赖项文件夹中的引用项目。

我做过的事情:

  • 更新的 Gradle 版本(正如您在编辑后的问题中看到的那样)
  • 转到 Window/Preferences 和 Gradle 包装器而不是本地包装器
  • 打开项目同步(也在窗口/首选项)

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

谢谢你的帮助。

于 2020-09-25T14:14:58.350 回答