Importing a Gradle project into IntelliJ
Well, this should be easy, but as usual in practical software
engineering it often is not. So, here is my recipe for
importing gradle projects into IntelliJ.
- First, ensure that the project itself is well behaved:
issue gradle test in a shell, and ensure that all
On the welcome screen, choose 'Open or Import'
... and browse to the root folder of the project, that
you want to import (Here it is the 'paystation' project.) There
should be small black box overlaid the folder, showing that
IntelliJ knows it is gradle project.
and click 'OK'
IntelliJ will now import it - as this involves retriving
all dependencies and possibly gradle itself from
repositories on the internet, this can take quite a while,
a minute or so.
IntelliJ can build and run Junit tests either using Gradle or
its own built-in runner. The former is the default, but I
prefer using IntelliJ's. As much in SE, it is a matter of
taste, but you can change the setting by clicking the gradle
and then click the wrench icon -> Gradle Settings ...
and finally change the 'build and run' settings:
Litmus test: In the Project pane, browse to the
src/test folder (below it is barely visible), right-click it,
and choose 'Run All Tests'
Now, all tests should be run correctly.
Unable to find package
java.lang in classpath?
On a newly installed IntelliJ, I sometimes run into a problem,
namely that IntelliJ cannot compile anything, complaining that the
core java.lang library is missing.
If you run into the same problem, do this:
Click 'File' and then 'Project Structure...'
In the dialog appearing, click 'SDK' under 'Platform
The list of file in the classpath is likely much shorter than
the one shown above. Therefore, click the '...' button at the
end of the 'JDK Home Path:'.
A 'Select Home Directory for JDK' props up, just hit the 'OK'
button, as this will read the directory into IntelliJ again,
and thus fix the missing dependencies.
The standard answer in software engineering when systems
behave weird is reboot. I have had to reboot intellij
gradle projects quite a few times. So here is a recipe.
Close the project in IntelliJ.
On the Welcome screen, click the 'X' / delete icon, next to
the project name. Ensure that the project is gone from the
Open a shell, go to the project root folder, and remove folder
'.idea' and possible file '(project).iml', to ensure all
IntelliJ files are deleted.
Now, repeat the import procedure for the project.
Henrik Bærbak Christensen - Aarhus University