我对 Eclipse 比较陌生,我是 IntelliJ 人 :) 所以为了练习,我在 Eclipse 中创建了一个虚拟 Gradle 项目,它甚至无法识别自动插入的 JUnit 依赖项。
我正在使用的堆栈如下:
- 摇篮 6.6.1
- 爪哇 13
- Eclipse 2019-09 R (4.13.0) -->根据以下建议更新到 2020-09 (4.17.0)。
我已经做过的事情:
根据本指南完成能够使用 Lombok 的先决条件(请参阅下面的代码) 。
安装 Buildship Gradle。
在我的 build.gradle 中插入以下脚本:
应用插件:“日食”
然后运行
gradlew cleanEclipse 日食
在首选项中设置自动项目同步并在该选项卡上使用其他选项。
刷新依赖并右键单击。...可能还有其他一些我无法正确回忆的事情。
我的实际代码如下(主要是自动生成的):
构建.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 并重新创建了项目。遗憾的是,它并没有解决问题。