英文:
Issues with converting Eclipse Groovy project to Gradle project
问题
我有一个现有的Groovy项目,是在Eclipse IDE中创建的,项目结构如下:
SposZohoMergeScript
-- src
-- com.xxx.sposZohoMergeScript
-- com.xxx.sposZohoMergeScript.constants
-- com.xxx.sposZohoMergeScript.factories
-- com.xxx.sposZohoMergeScript.io
-- com.xxx.sposZohoMergeScript.mergers
-- com.xxx.sposZohoMergeScript.models
-- com.xxx.sposZohoMergeScript.utils
我一直在手动下载、审核并将各个库和依赖的JAR文件添加到项目的构建路径中...更不用说没有内置的测试系统...我应该如何继续工作呢?!
是时候引入Gradle了!
我尝试使用Gradle插件,但它几乎立刻出现了问题...
我运行构建设置初始化步骤,它运行了很长时间...即使我去睡觉,几个小时后醒来,再次检查,它还在运行...
我关闭Eclipse IDE,打开BASH,在SposZohoMergeScript文件夹中运行 gradle init
,它重新配置了我的项目。
我尝试将其设置为基本项目:
[![enter image description here][1]][1]
然后我创建了 build.gradle
文件:
/*
* This file was generated by the Gradle 'init' task.
*
* This is a general purpose Gradle build.
* Learn more about Gradle by exploring our samples at https://docs.gradle.org/7.2/samples
*/
plugins {
id 'java'
id 'groovy'
}
repositories {
mavenCentral()
}
dependencies {
// https://mvnrepository.com/artifact/org.apache.poi/poi
implementation 'org.apache.poi:poi:5.2.3'
// https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml
implementation 'org.apache.poi:poi-ooxml:5.2.3'
}
然后我运行 gradle build
,运行成功。之后,我打开Eclipse,不仅看到我的文件夹结构混乱...
[![enter image description here][2]][2]
但当我尝试使用键盘快捷键创建构造函数时,出现错误,代码突出显示/IntelliSense无法理解我以前编写的代码:
[![enter image description here][4]][4]
我运行 gradle dependencies
用于项目,这是我得到的:
> Task :dependencies
------------------------------------------------------------
Root project 'SposZohoMergeScript'
------------------------------------------------------------
annotationProcessor - Annotation processors and their dependencies for source set 'main'.
No dependencies
apiElements - API elements for main. (n)
No dependencies
archives - Configuration for archive artifacts. (n)
No dependencies
compileClasspath - Compile classpath for source set 'main'.
+--- org.apache.poi:poi:5.2.3
| +--- commons-codec:commons-codec:1.15
| +--- org.apache.commons:commons-collections4:4.4
| +--- org.apache.commons:commons-math3:3.6.1
| +--- commons-io:commons-io:2.11.0
| +--- com.zaxxer:SparseBitSet:1.2
| \--- org.apache.logging.log4j:log4j-api:2.18.0
\--- org.apache.poi:poi-ooxml:5.2.3
+--- org.apache.poi:poi:5.2.3 (*)
+--- org.apache.poi:poi-ooxml-lite:5.2.3
| \--- org.apache.xmlbeans:xmlbeans:5.1.1
| \--- org.apache.logging.log4j:log4j-api:2.18.0
+--- org.apache.xmlbeans:xmlbeans:5.1.1 (*)
+--- org.apache.commons:commons-compress:1.21
+--- commons-io:commons-io:2.11.0
+--- com.github.virtuald:curvesapi:1.07
+--- org.apache.logging.log4j:log4j-api:2.18.0
\--- org.apache.commons:commons-collections4:4.4
compileOnly - Compile only dependencies for source set 'main'. (n)
No dependencies
default - Configuration for default artifacts. (n)
No dependencies
implementation - Implementation only dependencies for source set 'main'. (n)
+--- org.apache.poi:poi:5.2.3 (n)
\--- org.apache.poi:poi-ooxml:5.2.3 (n)
runtimeClasspath - Runtime classpath of source set 'main'.
+--- org.apache.poi:poi:5.2.3
| +--- commons-codec:commons-codec:1.15
| +--- org.apache.commons:commons-collections4:4.4
| +--- org.apache.commons:commons-math3:3.6.1
| +--- commons-io:commons-io:2.11.0
| +--- com.zaxxer:SparseBitSet:1.2
| \--- org.apache.logging.log4j:log4j-api:2.18.0
\--- org.apache.poi:poi-ooxml:5.2.3
+--- org.apache.poi:poi:5.2.3 (*)
+--- org.apache.poi:poi-ooxml-lite:5.2.3
| \--- org.apache.xmlbeans:xmlbeans:5.1.1
| \--- org.apache.logging.log4j:log4j-api:2.18.0
+--- org.apache.xmlbeans:xmlbeans:5.1.1 (*)
+--- org.apache.commons:commons-compress:1.21
+--- commons-io:commons-io:2.11.0
+--- com.github.virtuald:curvesapi:1.07
+--- org.apache.logging.log4j:log4j-api:2.18.0
\--- org.apache.commons:commons-collections4:4.4
runtimeElements - Elements of runtime for main. (n)
No dependencies
runtimeOnly - Runtime only dependencies for source set 'main'. (n)
No dependencies
testAnnotationProcessor - Annotation processors and their dependencies for source set 'test'.
No dependencies
testCompileClasspath
<details>
<summary>英文:</summary>
I have an existing Groovy project, created with the Eclipse IDE, defined to be:
SposZohoMergeScript
-- src
-- com.xxx.sposZohoMergeScript
-- com.xxx.sposZohoMergeScript.constants
-- com.xxx.sposZohoMergeScript.factories
-- com.xxx.sposZohoMergeScript.io
-- com.xxx.sposZohoMergeScript.mergers
-- com.xxx.sposZohoMergeScript.models
-- com.xxx.sposZohoMergeScript.utils
I have been *manually* downloading, vetting, and bringing into the project build path the individual JAR files per each library and dependency... Not to mention there's no built-in system for testing... How am I supposed to keep working like this?!
It's time to bring in Gradle!
I try the Gradle plugin, and it goes sideways pretty much immediately...
I run the build setup init step, and it runs forever...even if I go to sleep, wake up a few hours later, and check back on it, it's still there...
I close the Eclipse IDE, open up the BASH, on the SposZohoMergeScript folder, run `gradle init` myself, and it re-fucktors my project.
I try to set it up as basic project:
[![enter image description here][1]][1]
I then create the `build.gradle`:
/*
- This file was generated by the Gradle 'init' task.
* - This is a general purpose Gradle build.
- Learn more about Gradle by exploring our samples at https://docs.gradle.org/7.2/samples
*/
plugins {
id 'java'
id 'groovy'
}
repositories {
mavenCentral()
}
dependencies {
// https://mvnrepository.com/artifact/org.apache.poi/poi
implementation 'org.apache.poi:poi:5.2.3'
// https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml
implementation 'org.apache.poi:poi-ooxml:5.2.3'
}
I then do a `gradle build`, which runs successfully. After that, I open up the Eclipse, and not only do I see my folder structure all messed up...
[![enter image description here][2]][2]
but when I go to, for example, use the keyboard shortcuts to create a constructor, it gives me error, and the highlighter/IntelliSense can't make sense of previously-working code I have written:
[![enter image description here][4]][4]
I run `gradle dependencies` for the project, and this is what I get:
> Task :dependencies
Root project 'SposZohoMergeScript'
annotationProcessor - Annotation processors and their dependencies for source set 'main'.
No dependencies
apiElements - API elements for main. (n)
No dependencies
archives - Configuration for archive artifacts. (n)
No dependencies
compileClasspath - Compile classpath for source set 'main'.
+--- org.apache.poi:poi:5.2.3
| +--- commons-codec:commons-codec:1.15
| +--- org.apache.commons:commons-collections4:4.4
| +--- org.apache.commons:commons-math3:3.6.1
| +--- commons-io:commons-io:2.11.0
| +--- com.zaxxer:SparseBitSet:1.2
| --- org.apache.logging.log4j:log4j-api:2.18.0
--- org.apache.poi:poi-ooxml:5.2.3
+--- org.apache.poi:poi:5.2.3 ()
+--- org.apache.poi:poi-ooxml-lite:5.2.3
| --- org.apache.xmlbeans:xmlbeans:5.1.1
| --- org.apache.logging.log4j:log4j-api:2.18.0
+--- org.apache.xmlbeans:xmlbeans:5.1.1 ()
+--- org.apache.commons:commons-compress:1.21
+--- commons-io:commons-io:2.11.0
+--- com.github.virtuald:curvesapi:1.07
+--- org.apache.logging.log4j:log4j-api:2.18.0
--- org.apache.commons:commons-collections4:4.4
compileOnly - Compile only dependencies for source set 'main'. (n)
No dependencies
default - Configuration for default artifacts. (n)
No dependencies
implementation - Implementation only dependencies for source set 'main'. (n)
+--- org.apache.poi:poi:5.2.3 (n)
--- org.apache.poi:poi-ooxml:5.2.3 (n)
runtimeClasspath - Runtime classpath of source set 'main'.
+--- org.apache.poi:poi:5.2.3
| +--- commons-codec:commons-codec:1.15
| +--- org.apache.commons:commons-collections4:4.4
| +--- org.apache.commons:commons-math3:3.6.1
| +--- commons-io:commons-io:2.11.0
| +--- com.zaxxer:SparseBitSet:1.2
| --- org.apache.logging.log4j:log4j-api:2.18.0
--- org.apache.poi:poi-ooxml:5.2.3
+--- org.apache.poi:poi:5.2.3 ()
+--- org.apache.poi:poi-ooxml-lite:5.2.3
| --- org.apache.xmlbeans:xmlbeans:5.1.1
| --- org.apache.logging.log4j:log4j-api:2.18.0
+--- org.apache.xmlbeans:xmlbeans:5.1.1 ()
+--- org.apache.commons:commons-compress:1.21
+--- commons-io:commons-io:2.11.0
+--- com.github.virtuald:curvesapi:1.07
+--- org.apache.logging.log4j:log4j-api:2.18.0
--- org.apache.commons:commons-collections4:4.4
runtimeElements - Elements of runtime for main. (n)
No dependencies
runtimeOnly - Runtime only dependencies for source set 'main'. (n)
No dependencies
testAnnotationProcessor - Annotation processors and their dependencies for source set 'test'.
No dependencies
testCompileClasspath - Compile classpath for source set 'test'.
+--- org.apache.poi:poi:5.2.3
| +--- commons-codec:commons-codec:1.15
| +--- org.apache.commons:commons-collections4:4.4
| +--- org.apache.commons:commons-math3:3.6.1
| +--- commons-io:commons-io:2.11.0
| +--- com.zaxxer:SparseBitSet:1.2
| --- org.apache.logging.log4j:log4j-api:2.18.0
--- org.apache.poi:poi-ooxml:5.2.3
+--- org.apache.poi:poi:5.2.3 ()
+--- org.apache.poi:poi-ooxml-lite:5.2.3
| --- org.apache.xmlbeans:xmlbeans:5.1.1
| --- org.apache.logging.log4j:log4j-api:2.18.0
+--- org.apache.xmlbeans:xmlbeans:5.1.1 ()
+--- org.apache.commons:commons-compress:1.21
+--- commons-io:commons-io:2.11.0
+--- com.github.virtuald:curvesapi:1.07
+--- org.apache.logging.log4j:log4j-api:2.18.0
--- org.apache.commons:commons-collections4:4.4
testCompileOnly - Compile only dependencies for source set 'test'. (n)
No dependencies
testImplementation - Implementation only dependencies for source set 'test'. (n)
No dependencies
testRuntimeClasspath - Runtime classpath of source set 'test'.
+--- org.apache.poi:poi:5.2.3
| +--- commons-codec:commons-codec:1.15
| +--- org.apache.commons:commons-collections4:4.4
| +--- org.apache.commons:commons-math3:3.6.1
| +--- commons-io:commons-io:2.11.0
| +--- com.zaxxer:SparseBitSet:1.2
| --- org.apache.logging.log4j:log4j-api:2.18.0
--- org.apache.poi:poi-ooxml:5.2.3
+--- org.apache.poi:poi:5.2.3 ()
+--- org.apache.poi:poi-ooxml-lite:5.2.3
| --- org.apache.xmlbeans:xmlbeans:5.1.1
| --- org.apache.logging.log4j:log4j-api:2.18.0
+--- org.apache.xmlbeans:xmlbeans:5.1.1 ()
+--- org.apache.commons:commons-compress:1.21
+--- commons-io:commons-io:2.11.0
+--- com.github.virtuald:curvesapi:1.07
+--- org.apache.logging.log4j:log4j-api:2.18.0
--- org.apache.commons:commons-collections4:4.4
testRuntimeOnly - Runtime only dependencies for source set 'test'. (n)
No dependencies
(*) - dependencies omitted (listed previously)
(n) - Not resolved (configuration is not meant to be resolved)
A web-based, searchable dependency report is available by adding the --scan option.
BUILD SUCCESSFUL in 2s
1 actionable task: 1 executed
I am using Eclipse IDE Version: 2022-09 (4.25.0), Build id: 20220908-1902 , and Gradle 7.2, on Windows 10 64-bit computer .
What can I do to get Gradle working with the Eclipse?
[1]: https://i.stack.imgur.com/pvjOO.png
[2]: https://i.stack.imgur.com/OwjfX.png
[4]: https://i.stack.imgur.com/BvAlo.png
</details>
# 答案1
**得分**: 1
根据您的屏幕截图来看,Gradle的目录结构不正确。
按照约定,Java和Groovy插件配置其源集为`src/main/java`和`src/main/groovy`:
* Java插件:https://docs.gradle.org/current/userguide/java_plugin.html#sec:java_project_layout
* Groovy插件:https://docs.gradle.org/current/userguide/groovy_plugin.html#sec:groovy_project_layout
您的截图显示只有`src/`,然后是您的代码/包。将您的代码移动到`src/main/groovy/`(对于Groovy代码)和`src/main/java`(对于Java代码)下。
要在Eclipse中更正项目结构,请执行以下操作:
1. **右键单击**项目,选择***New > Folder***,创建文件夹**`src/main/java`**。
2. **将文件夹** **`src/com`** 移动到**`src/main/java`**。
3. **右键单击 `src/main/java`**,选择***Build Path > Use As Source Folder***。
<details>
<summary>英文:</summary>
Based on your screenshot alone, the directory structure is not correct for Gradle.
By convention, the Java and Groovy plugin configure their source sets to be `src/main/java` and `src/main/groovy`:
* https://docs.gradle.org/current/userguide/java_plugin.html#sec:java_project_layout
* https://docs.gradle.org/current/userguide/groovy_plugin.html#sec:groovy_project_layout
Your screenshot shows that you only have `src/` followed by your code/package. Move your code to be under `src/main/groovy/` for Groovy code and `src/main/java` for Java code.
To correct your project structure in Eclipse do the following:
1. **Right-click** the **project** and choose ***New > Folder*** and create the folder **`src/main/java`**
2. **Move** the folder **`src/com` into `src/main/java`**
3. **Right-click `src/main/java`** and choose ***Build Path > Use As Source Folder***
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论