Java IntelliJ IDEA Gradle + Groovy

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21386371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:24:41  来源:igfitidea点击:

IntelliJ IDEA Gradle + Groovy

javamavengroovyintellij-ideagradle

提问by Daniil Shevelev

I am new to IDEA. What I would like to do is have a project (or module?) that has maven folder structure (aka src/main/java, src/main/groovy, src/test/groovy, etc), be managed by Gradle and support creation of Groovy classes, their compilation and execution.

我是 IDEA 的新手。我想做的是有一个项目(或模块?),它具有 maven 文件夹结构(又名 src/main/java、src/main/groovy、src/test/groovy 等),由 Gradle 管理并支持创建Groovy 类,它们的编译和执行。

What I tried:

我试过的:

  • Create a "Groovy" Project.
    • I can add "Maven" support, but not Gradle.
  • Create a "Gradle" project and add "Groovy" module to it
    • I can manage dependencies and plugins, but the file structure is screwed up.
      • The code goes into a sub-folder of the project (aka the name of the module)
      • I cannot directly add folders to the "src" of the module. When I copy them into the src folder they are considered package names.
  • 创建一个“Groovy”项目。
    • 我可以添加“Maven”支持,但不能添加 Gradle。
  • 创建一个“Gradle”项目并向其添加“Groovy”模块
    • 我可以管理依赖项和插件,但文件结构搞砸了。
      • 代码进入项目的子文件夹(也就是模块的名称)
      • 我无法直接将文件夹添加到模块的“src”。当我将它们复制到 src 文件夹时,它们被视为包名称。

What I am looking for:

我在找什么:

  • Solution to the particular problem
  • General workflow for creating multi-facet (aka Gradle+Groovy or Java+Maven or Web+YouNameIt) project/modules.
  • Explanation of what is the reason for this paradigm/structure shift in IDEA?
  • 特定问题的解决方案
  • 创建多方面(又名 Gradle+Groovy 或 Java+Maven 或 Web+YouNameIt)项目/模块的一般工作流程。
  • 解释 IDEA 中这种范式/结构转变的原因是什么?

回答by Andrei Tomashpolskiy

Solution to the particular problem

特定问题的解决方案

0) Turn on Gradle plugin in Preferences -> Plugins

0) 在 Preferences -> Plugins 中打开 Gradle 插件

1) Create any Java project (Groovy, Maven, plain Java)

1) 创建任何 Java 项目(Groovy、Maven、普通 Java)

2) Create build.gradlefile in base directory

2)在基目录下创建build.gradle文件

3) Open JetGradle view and click Add. Then select your build file

3) 打开 JetGradle 视图并单击添加。然后选择你的构建文件

4) When you do this first time, IDEA will prompt you to locate your local Gradle distribution (you may also change it later in Preferences -> Gradle settings)

4) 当你第一次这样做时,IDEA 会提示你定位你本地的 Gradle 发行版(你也可以稍后在 Preferences -> Gradle settings 中更改它)

As for project structure, Gradle follows Maven conventions, so in the build file you just write:

至于项目结构,Gradle 遵循 Maven 约定,所以在构建文件中你只需写:

apply plugin: 'java'

and everything just works.

一切正常。

Create a "Gradle" project and add "Groovy" module to it

I can manage dependencies and plugins, but the file structure is screwed up.

The code goes into a sub-folder of the project (aka the name of the module)

I cannot directly add folders to the "src" of the module. When I copy them into the src folder they are considered package names.

Explanation of what is the reason for this paradigm/structure shift in IDEA?

创建一个“Gradle”项目并向其添加“Groovy”模块

我可以管理依赖项和插件,但文件结构搞砸了。

代码进入项目的子文件夹(也就是模块的名称)

我无法直接将文件夹添加到模块的“src”。当我将它们复制到 src 文件夹时,它们被视为包名称。

解释 IDEA 中这种范式/结构转变的原因是什么?

The main reason is to provide possibility of logical decomposition of your application into separate modules, e.g. app-core, app-web, app-ear etc. Each of this modules produces an artifact: jar, war, ear.

主要原因是提供将应用程序逻辑分解为单独模块的可能性,例如 app-core、app-web、app-ear 等。这些模块中的每一个都产生一个工件:jar、war、ear。

Compare this with other IDEs, say Eclipse, where you would have several different projects (perhaps dependent on each other) to accomplish the same thing. So basically, you may think of IDEA module as of Eclipse project (roughly). Also this greatly simplifies usage of Maven multi-module projects.

将此与其他 IDE 进行比较,例如 Eclipse,在那里您将有几个不同的项目(可能相互依赖)来完成相同的事情。所以基本上,您可能会将 IDEA 模块视为 Eclipse 项目(大致)。这也大大简化了 Maven 多模块项目的使用。

As for the srcfolder: IDEA lets you mark directories inside the module "content root" (base directory of the module) as Source, Test Source or Excluded. If srcis marked as Source directory, then obviously everything inside is treated as packages and sources.

至于src文件夹:IDEA 允许您将模块“内容根”(模块的基本目录)内的目录标记为源、测试源或排除。如果src被标记为 Source 目录,那么显然里面的所有内容都被视为包和源。

回答by Daniil Shevelev

I followed the steps in this video:

我按照此视频中的步骤操作:

  1. Create "Gradle" project, with Gradle plugin enabled and Gradle API plugin disabled. Select "Create empty folders" option.
  2. Add "apply plugin: 'groovy'". This will create groovy folders.
  3. Add "" to IML project file in order to be able to create Groovy classes.
  1. 创建“Gradle”项目,启用 Gradle 插件并禁用 Gradle API 插件。选择“创建空文件夹”选项。
  2. 添加“应用插件:'groovy'”。这将创建 groovy 文件夹。
  3. 将 "" 添加到 IML 项目文件以便能够创建 Groovy 类。

回答by jeremyjjbrown

I would suggest not creating the project from inside Intellij.

我建议不要从 Intellij 内部创建项目。

Since Intellij imports Gradle projects quite nicely you can just import one of the pre-made Groovy Gradle Quick Startsyou can find at the root of your Gradle installation.

由于 Intellij 可以很好地导入 Gradle 项目,因此您只需导入预制的Groovy Gradle 快速入门之一,您可以在 Gradle 安装的根目录中找到。

For example $GRADLE_HOME/samples/groovy/mixedJavaAndGroovyhas the exact Java/Groovy layout you describe with the HelloWorld Classes and Tests in place. Just copy it, rename the root folder, import it into Intellij and code!

例如,$GRADLE_HOME/samples/groovy/mixedJavaAndGroovy具有您在 HelloWorld 类和测试中描述的确切 Java/Groovy 布局。只需复制它,重命名根文件夹,将其导入 Intellij 并编写代码!