IntelliJ IDEA Gradle + Groovy

14,279

Solution 1

I would suggest not creating the project from inside Intellij.

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

For example $GRADLE_HOME/samples/groovy/mixedJavaAndGroovy has 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!

Solution 2

Solution to the particular problem

0) Turn on Gradle plugin in Preferences -> Plugins

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

2) Create build.gradle file in base directory

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

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)

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

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?

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.

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.

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

Solution 3

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.
Share:
14,279
Daniil Shevelev
Author by

Daniil Shevelev

I am an aspiring software engineer with interests in web development, systems design, distributed systems and artificial intelligence.

Updated on July 11, 2022

Comments

  • Daniil Shevelev
    Daniil Shevelev almost 2 years

    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.

    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.

    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?
  • Daniil Shevelev
    Daniil Shevelev about 10 years
    I am using community edition and I do not have "JetGradle" view in the View->Window Tools menu.
  • Daniil Shevelev
    Daniil Shevelev about 10 years
    After installing the "Gradle GUI" plugin I get this error: "Plugin 'Gradle GUI' failed to initialize and will be disabled. Please restart IntelliJ IDEA. java.lang.NoClassDefFoundError: org/jetbrains/plugins/gradle/util/GradleLibraryManager"
  • eduardohl
    eduardohl about 10 years
    I wrote a quick walkthrough on how to work with IntelliJ 12 and Gradle javamoody.blogspot.ca/2014/02/… maybe it helps.