Configure IntelliJ project when importing when importing a Gradle project - intellij-idea

Is there a way to import a Gradle Java project into IntelliJ 2017.2.x and apply some settings like
code style
vcs settings
from the Gradle build.gradle on project level?
I noticed that e.g. the Grails Framework uses the idea-gradle-plugin but this only works in combination with the Gradle task idea.
gradlew idea
I don't want to generate IntelliJ project files using a Gradle task. I would like IntelliJ to fetch the settings from the build file whenever I import the build.gradle file.
Is this possible?

Afaik no. You can only set in build.gradle what is supported by the DSL that then also is understood by the IDEA integration. There is no way to set arbitrary settings this way.
To do this, as you already found out, you need to hook into the IDEA project file generation and generate the settings you want like code style settings and VCS settings (I do both for our projects and more) and then use the idea task to generate the project files.
You can also configure IDEA to automatically run some Gradle task before refreshing the project, so you can make IDEA automatically run the idea task before refreshing and this should work then. The initial IDEA setup to do this though has either to be generated with idea, set-up manually by all devs or configured once and then checked in. But if you check-in the IDEA files, you can of course also simply check-in the code-style and VCS settings. Most of the IDEA project files are meant to be checked in anyway. Just some like the workspace file are meant to be developer-specific and thus excluded from being checked in.

Related

How to create a project from the commandline?

Is it possible to create an IntelliJ project purely from the command line?
We are looking a streamlining the on-boarding of new hires and off-shore resources and to minimise the amount of project setup they have to perform. As such, it would be nice if we had the IDE already configured for them to get started.
There is a legacy plug-in for Maven to generate IntelliJ IDEA project files, but we don't recommend using it.
Instead, it would be easier to instruct users to open pom.xml file in the IDE so that the import is performed automatically.

Separate IntelliJ settings for projects

I'm working on multiple projects at the moment and the fact that IntelliJ automatically synchronizes all settings makes life quite difficult for me.
For example, for project A I need to use a specific version of Maven with specific settings, for project B another version with other settings and for project C another version. This however isn't possible, when I set the Maven folder to something, it automatically syncs when I open a new IntelliJ.
This was just an example, there are more settings that I would like not to be shared.
Is there a way to resolve this?

IntelliJ IDEA: Unable to create a Gradle project

I'm facing difficulties in setting up a gradle project. During setting up of the Gradle settings in the wizard,
I am not able to use the default recommended gradle wrapper because it downloads the Gradle and the network connectivity is very bad. So I go with the local gradle distribution which I have installed in my Ubuntu. On refreshing of the gradle project after creation, I always get stuck to this error. When I run the gradle build command through the terminal on the empty project, It works fine.
I have also tried the troubleshooting solutions given by the IDE. But, that made no improvements. Need assistance to solve this problem.
Comment if any additional info required.
As no-one else has chipped in, an approach that should work is to use the Gradle Idea Plugin instead of relying on the build in support Gradle in Idea. In my experience the plugin supports a wider range of Gradle project structures than Ideasupports directly. So you would
Create a Gradle project outside Idea and confirm it all works on from the command line.
Add the Idea plugin to the project
Run 'gradle cleanIdea idea' to generate the Idea project files.
Open up the newly generated project files from Idea and off you go.

How to use an existing Gradle project in IntelliJ IDEA 13/14

I am using Gradle and IntelliJ IDEA. Normally I use apply plugin: 'idea' to generate the IDEA project files. With IDEA 12 I was used to work with the JetGradle tool view.
Now I updated to IDEA 13 and the JetGradle tool view is gone. When I use the old run configuration gradle:run, IDEA tells me:
Error running gradle:run:
Module 'X' is not backed by gradle.
How can I activate the gradle build in IDEA 13 without the tool view? I found this tweet from Cédric Champeau, but was still not able to solve it. Do I have to turn my simple gradle project into a multi module project or what?
Update
With File - Import Project ... I can import the build.gradle file and than I can choose View - Tool Windows - Gradle, which was hidden before. Is this the way to go?
What you state in your update is the correct way to go. The Gradle Plug-in received a lot of love in the IDEA 13 update and has changed a bit. An import of the Gradle build file is necessary. And as you have noted, the JetGradle tool window is now named simple Gradle
Tested with Gradle 1.9. In my opinion there are 2 issues:
Issue 1: when using gradlew idea I expect to receive a valid IDEA project without the need to import it. There are discussions on the gradle forum, see this Gradle forum post. In this Jetbrains forum post it is told that there is no backward compatibility with IntelliJ 12 gradle projects at the moment.
Issue 2: With IDEA 12 it was possible to open the JetGradle View even if the current project was not a gradle projcet. But the view displayed the message "There is no linked Gradle project. You can Add one" and offered you the possibility to convert the project to an gradle project with a simple click. In IDEA 13 it is not possible to open the Gradle View when the current project is not a gradle project. I asked a question here.
In the Gradle forum post mentioned above Peter Niederweiser stated:
The preferred way of integrating with IDEA 13 is to use IDEA's Gradle import, without running gradle idea. (You should still apply the idea plugin though, and it's still important to apply it to allprojects {} rather than subprojects {} when dealing with multi-project builds.)
So the answer from Mark Vedder is correct, altough I would have liked to have more information.
I have found that when you initially import a gradle project into Idea, if you don't have all of your directories created yet, the gradle tool window vanishes while you are importing into Idea. (It is there when the import starts, but at the end it disappears.) However, if I manually build the project first (on a mac, >gradle clean build), and then go back to IDEA and import the project, the gradle tool window stays active. Hopefully this will help someone else.
you can go to Project Structure (Ctrl + Alt + Shift +S) and then under the Modules, click "Import Module" and choose your build.gradle from the project file repo. It will make the module gradle aware and then you will also see the Gradle window
The simplest way to do this is to use the Import Project option if you cannot use the gradlew idea to produce the idea project

IntelliJ IDEA with multiple gradle subprojects

I'm working on multiple Gradle projects with internal and external dependencies, and so far I am happy that thanks to Gradle's dependency management I can modify a library project without affecting every application that uses the library.
When I need to modify a library project and test it using an application project that uses it, I need to do the following,
Modify the library project and commit to SCM
Trigger CI to build the library project and push it to my Gradle repository
Update the application project's build.properties to refer to the new version of library project
Iterate the above steps until everything works and there is no bug
So it became quite combersome now. Can I configure IntelliJ IDEA so that
All my Gradle projects are in one window, like the screenshot below, which is Twitter's Finagle imported using its pom.xml. Sadly IntelliJ's JetGradle plugin doesn't seem to understand Gradle subprojects.
When build.properties's dependencies are my subprojects, read dependency from local snapshot, otherwise download them from the Gradle repository
Thanks.
If you want to open all projects in a single IDEA window, you'll have to aggregate them into a multi-project build, at least until IDEA 13 hits the market. Before IDEA 13, it's better to use Gradle's IDEA integration. Once you have a multi-project build, all you need to do is to add allprojects { apply plugin: "idea" } to the root build script, run gradle (cleanIdea) idea, then open the generated IDEA project.
Currently in IntelliJ IDEA 2019.2 you can add the gradle subprojects like so
Open Gradle Tool Window via View > Tool Windows > Gradle menu
Click on "Link Gradle Project" button (the plus sign)
Select the build.gradle file corresponding to the subproject
Go to File > Project Structure > Modules > NameOfSubproject
Navigate to main/java and click on Mark as: Sources
Mark the main/resources as Resources
Restart IntelliJ IDEA
The sources of the subproject will be recognized by IntelliJ and you can use Navigate Class action for the classes in the subproject