What option to select when cloning Git project in IntelliJ? - intellij-idea

I have cloned Gradle project from GitHub in IntelliJ and then got the following dialog:
Should I select "existing sources" because my sources exist and not external, or should I select "external model" because it has Gradle specification?

You should choose Gradle becouse only this way the IDE will properly find and use build.gradle files.

You should choose external model. You want IntelliJ to look at your Gradle file and decide how to set itself up based on what is in your Gradle file.
You use the existing sources option when you don't have Gradle file or Maven pom and you need IntelliJ to look at the sources and figure out what to do without a file to guide it.
There is some documentation here: https://www.jetbrains.com/help/idea/2016.3/creating-a-project-by-importing-existing-sources.html

Related

Intellij switch from sbt to maven

I have an SBT project opened in Intellij.
I want to convert it to Maven.
I created a pom.xml file, I removed build.sbt and now I want Intellij to see it as a Maven project.
If I do:
File -> New -> Project From Existing Sources
Then Intellij won't offer me the option to open it as a Maven project.
If I select Add Maven Project then I am unable to choose the current directory.
So how can I make Intellij see the project as a Maven project?
Use File | Open..., select the pom.xml file, choose "Open as Project" in the message box that appears.

How to get IntelliJ to associate Gradle sources with build.gradle?

When writing Gradle scripts for my Java project, specifically, when writing build.gradle files, IntelliJ does not recognize the Gradle API.
For instance, Gradle methods calls like apply, dependencies configure appear with a black line under them and it is not possible to navigate to method declarations, there is no auto-completion etc.
I managed to work around this by adding compile gradleApi() to the build's dependencies block. However, I don't want to have this explicit dependency in my code.
I tried editing IntelliJ's project structure and add a dependency on a Gradle library (tried gradle-core and gradle-all) to my modules, but that seems to have no effect.
Is there a way to make IntelliJ associate all build.gradle files with the Gadle sources?
I solved this problem as follows:
As mention in already posted answers, configure gradle
update gradle/wrapper/gradle-wrapper.properties file
change bin to all in distributionUrl i.e.
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
to
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-all.zip
OR
[optional] If you are using old version of gradle wrapper and wanted to upgrade, then execute
./gradlew wrapper --gradle-version 6.8.3 --distribution-type all
Update gradle task (if present in build file)
wrapper {
gradleVersion = '6.8.3'
distributionType = Wrapper.DistributionType.ALL
}
Before importing the project to IntelliJ-Idea IDE, update build.gradle and add java and idea plugin to the plugins list
plugins {
id "java-library"
id "idea"
}
From a terminal, execute ./gradlew clean build idea or simply ./gradlew idea
Import project to IntelliJ idea.
Go to Preferences --> build,Execution,Deployment --> BuildTools --> Gradle
You can see
Restart IntelliJ idea IDE.
So above we have configured both of the options so choose either of them, except the specified location option. That's it.
Before
After
Autocomplete functionality as mentioned in this answer.
I had similar frustrations with Grails 3, which defines and runs a wrapper task when an app is created. Changing to the "all" zip in the wrapper properties file did not work because this kept getting changed back to the "bin" zip.
This was solved when it was understood that the "gradle-wrapper.properties" file simply stores the values from the "wrapper" task, and if this task is run after the properties are changed, they get changed right back.
This is easily fixed by setting some properties on the wrapper task:
wrapper.gradleVersion='3.2.1'
wrapper.distributionType=Wrapper.DistributionType.ALL
Now importing the project into IDEA gives you smart editing of your build.gradle.
when I choose build.gradle in IDEA and open it, IDE prompts
You can configure Gradle wrapper to use distribution with sources. It will provide IDE with Gradle API/DSL documentation.
I choose Ok, apply suggestion!
after project refreshing I am able to use code completion
before you import your project, configure it to use the customizable gradle wrapper as per the instructions here :-
https://docs.gradle.org/current/userguide/gradle_wrapper.html
add a task to your top level project like this:-
task wrapper(type: Wrapper) {
println "Wrapper gradleVersion = '2.12'"
gradleVersion = '2.12'
}
or whatever the latest version is.
make sure you can build the project from the gradle command line before you try importing into intelliJ, using the ./gradlew command, which will download and install a gradle distribution for you the first time you build.
set your java home, intelliJ home and gradle home variables in your machine and in intelliJ (mine look like this, yours may be different depending on your setup and your history of hacking around your machine...:-
(from .bashrc
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home
)
When you do import, choose the customisable gradle wrapper. if all is well, when you open the top level build.gradle for your project, you will be asked to configure sources for the gradle dsl, which will also update your gradle wrapper properties file to this:-
#Thu Mar 31 14:04:00 BST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.12-all.zip
.. the change being from ... bin.zip to all.zip. and that's it. This had been giving me lots of grief for a long time, but that's the way to do it. (on IntelliJ IDEA 2016.1 CE at least...)
most of this was in
Dimitry's answer too, but I couldn't get it to work using the default wrapper , it had to be the customisable wrapper.

How to use gradle in intellij idea plugin project?

I am developing an idea plugin, and it is an intellij idea project.
I want to use gradle to manage the dependency.
How to config?
There is now a Gradle plugin for building IntelliJ Platform Plugins. In order to use it, you will need to add the following snippet to your build.gradle file.
plugins {
id "org.jetbrains.intellij" version "0.0.31"
}
apply plugin: 'org.jetbrains.intellij'
For more information, please see this guide to help you get started.
Ok, there are multiple ways to create an IntelliJ project, "templates" if you like, and unfortunately you can only pick one of them (IntelliJ plugin or gradle).
Thankfully, it's easy to configure a project for gradle in IntelliJ.
First, create a new project from the IntelliJ Platform Plugin template. You don't need to choose any Additional Libraries and Frameworks. This will give you a project structure including META-INF/plugin.xml and the Project SDK should be something like IDEA IU-129.451.
From here, simply create a new file named build.gradle at the top level of your project, including for example this line:
apply plugin: 'java'
Now, close the project. You can now use File -> Import Project..., choose the build.gradle file that you just created, and import the project. Accept the defaults for importing and hit OK.
The project is now opened with both gradle and intellij plugin enabled!
Notice that the source root src has disappeared and you will need to right click on src in the Project pane and select Mark Directory As -> Source Root.
To prepare the plugin for deployment, there is still the menu option in the Build menu for that - if you want to automate that part via gradle, good luck and please let us know how it's done ;)

Add source repositories for "Search in internet" in IDEA

When the source of an artifact can't be found, IDEA proposes to "Search in internet" or "Attach sources". Is there a way to add a new location to the repositories IDEA checks when choosing the "search in internet" option?
Essentially, you need to add repositories to IntelliJ Idea's "Index Maven Repositories" which are indexed for these types of proposals. Depending on the type of project (maven, gradle, ant, lein, sbt), the best answer will be different. In maven, you would normally edit your pom.xml file. In gradle, you would normally edit your build.gradle file. I don't know what build system you want to use (or if you want one).
If your projects are Maven projects, you can look at maven documentation to see how to add another maven repository in your settings.xml file. On a project by project basis, you can add the repository to the pom.xml file instead.
Similarly, if you're using gradle then look at gradle's documentation (Section 8.5) to add a maven repo to your projects build.gradle file.
Once IntelliJ Idea updates its indices, it will check the repositories that your project is configured to use for artifacts when it makes its proposals.

Avoid javadoc and sources downloading, when using Maven ant Tasks

I'm developing with Maven Ant Tasks support. When asking the repository to download desired libraries, it also downloads javadoc and source for each library. Is there a way to only download library jars?
Actual build.xml:
<artifact:dependencies settingsFile="${maven.settingsFile}" pomRefId="projectPom"
filesetId="dependency.fileset"
sourcesFilesetId="sources.dependency.fileset"
javadocFilesetId="javadoc.dependency.fileset"
versionsId="dependency.versions">
<remoteRepository refid="remote.repository" />
</artifact:dependencies>
As far as I know the src and javadoc downloads are off by default. There should be an option in your IDE or whatever tool you use for Maven.
Are your working with Eclipse?
In Eclipse you can find the option here:
Window > Preferences > Maven
Then tick the checkboxes (or rather untick them) 'Download Artifact Sources/JavaDoc'.
That's strange. Most open source Maven modules don't have source and javadoc on the default "compile" Maven scope. Have you tried specifying a scope?
<artifact:dependencies filesetId="dependency.fileset" useScope="compile">
..
Alternatively you can also specify a "scope" attribute on each dependency.
Personally I use the ivy plugin for my Maven downloads. The same problem is solved by specifying the "default" or "compile" configuration mapping: ivy: prevent downloading sources and .txt files
Finally solved: just remove sourcesFilesetId and javadocFilesetId attributes if you don't need sources and javadoc jar libraries.