Daniil Shevelev
Daniil Shevelev

Reputation: 12027

IntelliJ IDEA Gradle + Groovy

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:

What I am looking for:

Upvotes: 6

Views: 15473

Answers (3)

jeremyjjbrown
jeremyjjbrown

Reputation: 8009

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!

Upvotes: 4

Daniil Shevelev
Daniil Shevelev

Reputation: 12027

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.

Upvotes: 1

Andrei Tomashpolskiy
Andrei Tomashpolskiy

Reputation: 170

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.

Upvotes: 1

Related Questions