IntelliJ IDEA with multiple gradle subprojects - intellij-idea

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

Related

Configure IntelliJ project when importing when importing a Gradle project

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.

Intellij IDEA With Multiple Gradle Projects

I am working on two Gradle projects. One of these projects is a supporting library that will be used by other projects in the future so project A depends on project B but not as a 'multi module project'. The dependency is to be resolved through the artifact repo so project a declares it as a compile dependency using it's maven coordinates.
My problem is when working on these two projects in IntelliJ changes to project B aren't made available to project A until I install it (using the Gradle Maven plugin) in my local repo. This is kind of annoying and slows down my workflow. Is there a way to get IntelliJ to automatically update the dependency internally?
This is supported in the latest versions of Gradle and IntelliJ. It is known as a Composite Build.
Composite builds can be declared in the project's settings.gradle file as follows:
includeBuild '../my-app'
or by using the --include-build command line argument:
$ gradle --include-build ../my-utils run
Take a look at the Composite Builds with Gradle and IntelliJ IDEA Webinar for instructions on how to configure the integration.
In IDEA 2017 you can right-click on the gradle module and use Composite Build Configuration to link the current module to one or more gradle module already opened in the current workspace.

Visualizing gradle dependencies in Intellij

Is there a way to visualize project dependencies for a gradle project in Intellij? For a maven project, if you right click in the pom.xml you get a Show dependencies option but there I don't see such an option for a gradle project.
Does this feature exist for gradle project?
I did a small search in IDEA and Google and looks like there is not way to see Gradle dependency but I have found the plugin "Gradle View" which does what you need
http://plugins.jetbrains.com/plugin/7150
You can open a ticket and maybe they will add a better Gradle support in IDEA 15

Getting contents and natures of all projects in a Maven build

Currently I'm working on a Maven plugin that should generate files in all projects (OSGi bundles) that have a certain Eclipse project nature.
How can I access the contents of the projects included in the build and the project natures by using the Maven API?
Maven is a standalone build tool, not an Eclipse plugin. You cannot access Eclipse project settings from core Maven API.
Eclipse supports Maven with the M2E Eclipse plugin. It is possible to write M2E extensions and in the extension you can query the project natures via the functions of AbstractProjectConfigurator class.
However, M2E extensions will not run when you compile your code in the command line. I suggest that you choose one of the followings:
Write an Eclipse plugin that generates the source code into the src folder of the maven project. Code generation should be started by the user manually (selecting a context menu in the project or something).
Avoid using Eclipse project natures and solve your questions based on analyzing the source and pom of your project.
If you need to react on certain aspects in the source code like it looks from the thread with Balazs then you can simply write an ordinary maven plugin and include it in the parent pom. It will then run in every project and can analyze the code and react based on it.

Maven2_Builder vs. Maven Project Builder

I inherited a project that relies on Maven 2 to build it. Via project's Properties > Builders I can see that it has two builders, in this order:
Maven2_Builder
Maven Project Builder
I thought that there should be only one Maven builder...
Why two?
What the relation between the two?
Using the m2e plugin, I can clean the project at any given time via the project's context menu Run As > 6 Maven Clean. Does this mean Eclipse's Project > Clean... is no longer relevant?
Looks like you are using Eclipse with M2E. These days that works very well.
Before one would run
mvn eclipse:eclipse
Which would generate the typical Eclipse configuration files.
These days, just use the Eclipse Import as Maven Project and all necessary files are created.