How do you set up Gradle properly and create a new Gradle project for IntelliJ IDEA? - intellij-idea

So, I downloaded Gradle but I don't know how to set it up correctly. After I unzipped the zip file, what do I do next? I want to use Gradle in IntelliJ IDEA. From a livestream I've seen I know that there's a settings.gradle and a build.gradle file in the project folder in IntelliJ.
Also, I've seen that they used the windows console.
First of all, how do I access gradle through the windows console and tell it to generate a new project for IntelliJ?
As you can see, I don't have any experience with Gradle. Unfortunately I can't really find out how to use it.
I want the IntelliJ project then to have LWJGL and Slick.
Additionally, I know what it says in the build.gradle file I've seen on the livestream (I only want to create my project with the same structure like theirs).
Can anybody give a detailed description of what to do to achieve all this?

Lets' say you unpacked gradle to d:\tools\gradle.
To add its bin directory it to the PATH in a console window, execute the following command:
set PATH=d:\tools\gradle\bin;%PATH%
The above prepends the bin path to the current value of the PATH environment variable. This will only change the path for this specific command window.
If you want to add it to the PATH globally, then go to your control panel and choose System, then Advanced parameters (it might be something a little bit different: I'm translating from my French version of Windows). Then in the Advanced system parameters tab, click the button Environment variables.... Find the Path environment variable in the list, and add the directory, separated from the others using a semicolon (;).
Note: you can also define a new environment variable called GRADLE_HOME:
set GRADLE_HOME=d:\tools\gradle
or globally, as explained above, and reference it inside the PATH variable:
set PATH=%GRADLE_HOME%\bin;%PATH%

Just intall homebrew.
Then you can just open the terminal and install easy like:
brew install gradle
Done! It's installed!
For test just type in your terminal:
gradle -v
And you'll have something like this:
------------------------------------------------------------
Gradle 2.7
------------------------------------------------------------
Build time: 2015-09-14 07:26:16 UTC
Build number: none
Revision: xxxxxxxxxxxxxxxxxxxxxxxxxx
Groovy: 2.3.10
Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM: 1.8.0_60 (Oracle Corporation 25.60-b23)
OS: Mac OS X 10.11.1 x86_64
Then, open your IntelliJ and create a new gradle project as normal, just setting the gradle's path when necessary (normally in /usr/local/Cellar when installed with homebrew or /opt when installed other ways).
Good luck!

For linux Users,
User SDK Man to easily manage your gradle installation and path settings for development as well as gradle update
GET SDK MAN HERE

(for mac os users) Let's assume, you unpacked zip file into /Users/onuryilmaz/gradle-3.3 folder. Then open terminal and define a new environment variable called GRADLE_HOME:
export GRADLE_HOME=/Users/onuryilmaz/gradle-3.3
After that reference it inside the PATH variable:
export PATH=$PATH:$GRADLE_HOME/bin

just update the brew and install and set path that's all
brew update && brew install gradle
export PATH=$PATH:/opt/gradle/gradle-5.1.1/bin
https://gradle.org/install/

Related

Can't render component diagram with PlantUML in IntelliJ

I am trying to create a component diagram using the PlantUML plugin for IntelliJ.
I have installed the plugin and worked with it before. However, when I try to render a component diagram, I get an error:
Cannot find Graphviz
I've also checked with other diagrams and a sequence diagram is still working just fine.
What is the problem here?
On a mac using Intellij, if Intellij cannot find graphviz, testdot doesn't work and graphviz was installed with brew install graphviz:
In Intellij, click the wrench icon on the far right of the menu panel that contains the plantuml view and editor arrangement.
Click 'Open settings'
For 'Graphviz dot executable' add /opt/homebrew/bin/dot.
Click OK and restart Intellij.
This fixes the issue for me.
You can check first that you really are missing the /opt/local/bin/dot (or whatever path you're shown is broken) and the /opt/homebrew/bin/dot exists, but likely the reason is that brew put dot under its own path.
Sequence diagrams and activity diagrams work out of the box.
For other types of diagrams, you need to have Graphviz installed (version > 2.26.3).
If you don't have Graphviz installed - just install it.
If you have it installed but its version is improper - reinstall it.
If you have it installed in proper version but location is other than default - point to that location. You can manage that either by specifying env variable GRAPHVIZ_DOT containing a path to Graphviz executable or pointing it out directly from IntelliJ's PlantUML plugin settings.
Here, you can read more on how to install Graphviz to get along with PlantUML.
Btw. I solved the same problem by simply typing sudo apt-get install graphviz in my console - maybe would work for you as well.
As mentioned here https://plantuml.com/graphviz-dot
Since version 1.2021.5, you can experimentally use PlantUML without installing Graphviz
if you add !pragma layout smetana to your diagrams for the supported types. In that case, the "Smetana" engine is used instead of Graphviz
Therefore this worked for me;
#startuml
!pragma layout smetana
...
#enduml
I hope it helps!
If you:
1.installed Graphviz
2.configured GRAPHVIZ_DOT env var to your dot.exe file
(example D:\Program Files (x86)\Graphviz2.38\bin\dot.exe)
And you're still getting the same error: Cannot find Graphviz1.
You probably need to configure the path to the dot.exe inside the Intelij settings.
Follow this tutorial to do so.
In case it helps someone, these are the steps to resolve the same error (Can not find GraphViz...) from Eclipse editor.
Install GraphViz on your system. Depending on your system, installation instructions are here: https://graphviz.org/download/ . For Mac (v11.6) users, it is as simple as brew install graphviz
After installation is completed, add an environment variable: GRAPHVIZ_DOT to point to the path of the dot executable. On my machine, I added this:
export GRAPHVIZ_DOT=/opt/homebrew/bin/dot
Add path of the dot executable to Eclipse PlantUml preferences (Preferences --> Plant UML --> Path to the dot executable of Graph Viz). Screen shot given below:
First install graphviz
Find dot executable and put it where IntelliJ can find it
Windows: Add the dot executable path in the environment variables path settings
Linux: find the location of dot using where dot and if it's not in the /opt/local/bin you can just create a symbolink link from lets say /usr/local/bin by running ln -s /usr/local/bin/dot /opt/local/bin/dot
Restart IntelliJ
I had trouble too...the easiest way is to install the VsCode plugin 'Markdown Preview Enhanced' and then...magically...IntelliJ works fine too!!!

IntelliJ shows decompiled .class file instead of source code

I am having issues with IntelliJ - it shows me a "decompiled" version of the class instead of its source code if I ctrl+click the class.
This is what I see:
I am trying to open regular JDK source files.
The most relevant issue I found is explained here: Java sources replaced by decompiled files in Intellij
The most detailed answer I've found so far is there as well.
Yet it doesn't work for me:
Bytecode Viewer plugin is off.
Java Bytecode Decompiler plugin is off.
Platform, Project and Module all have same JDK version setup.
I have two JDKs available in IntelliJ: jdk1.8.0_161 and jbsdk8u112b287.2_windows_x86 (bundled with IntelliJ, but comes without src.zip). Tried with both (had to specify src.zip of jdk1.8.0_161 for jbsdk8u112b287.2_windows_x86) - no luck
src.zip is not corrupt - I can open it via WinRAR
When added jdk1.8.0_161 to IntelliJ, it automatically found src.zip. When manually adding src.zip to jbsdk8u112b287.2_windows_x86 Sourcepath tab - it started "Scanning for roots", but warned me that it "cannot determine what kind of files the chosen items contain" and offered me to "attach them as 'Sources'" so I agreed.
Tried File > Invalidate Caches / Restart... - same result.
Same issue in 2017.3 and fresh 2018.1 versions of IntelliJ.
Providing path for Documentation makes IntelliJ look for the documentation in the path specified. So it works as it should for both of JDKs. But not for sources.
Everything else works/compiles properly.
My settings:
1. Get the source files
Make sure you have downloaded and installed the source files of the JDK.
I'm on Arch Linux and installed them with pacman -S openjdk8-src which put a src.zip into /usr/lib/jvm/java-8-openjdk/. We'll use that zip file in the next step.
To list source packages of different JDK versions, do pacman -Ss "openjdk.*-src".
See this question on how to get the source files for your specific platform.
2. Point IntelliJ to the source files
In your module settings (open them with F4) under "Platform Settings" → "SDKs" → "Sourcepath", add the zip file containing the JDK source to your project JDK (be sure to pick the right JDK if IntelliJ lists more than one JDK) using the button with the plus sign:
3. Jump into the source
Select a JDK class, like Comparator, using IntelliJ's Search Everywhere or Ctrl+b with the cursor on the name of the class.
I also have the same problem. The problem for my case is that Project SDK is set to 13 [Invalid]. After I can Project SDK to a valid SDK. My problem is solved.
I tried all the things mentioned at other places for this but at the end I was doing a silly mistake. Intellij was pointing to JDK 11 for which there was no source code (I had both the SDKs 8 & 11). I removed JDK 11 and pointed to JDK 8. JDK had the source code, thus now I can see the source code on ctrl click. Also, I had disabled the bytecode viewer and decompiler plugins. Hope this helps some one.
This also happens for large files - IDEA decides to decompile them instead if indexing sources, which can take ages.
If you're willing to accept the drawdown, adding the following in Help | Edit Custom Properties might fix the problem:
idea.max.intellisense.filesize=4096
On ubuntu there was java version “OpenJDK” when I changed to “Oracle Java”, the error went away and javadoc returned
I meet same problem and finally it gone after I copy JDK to another folder and change config in IntelliJ. I think maybe cause there is a space in path before, because it is installed under "Program Files" folder.
For downloading the source code of class files belong to jar you can use download source option present on top right corner in the de-compiled class file.
On my Windows 10, expand maven:
For me going to File -> Project Structure -> Project -> SDK -> Add SDK -> Download JDK and selecting Oracle OpenJDK solved the problem. Prior to that I had a different JDK selected and apparently it didn't include source code
If you are in a maven project make sure in the project structure all the complied folders are in the excluded section.
I suggest to use Linux Instead of windows. It's easy to find and download Java.
If you are using Linux, you can download SDKMAN
Install Java by SDKMAN
# find version
$ sdk list java
# download
$ sdk install java x.y.z-amzn
ctrl+alt+shift+s add JDK
Switch project JDK
If you are using maven , Right click on Project explorer -> choose Maven -> chick on "reload projects"
This did the trick for me:
Make sure you select the option "Project Files" (instead e.g. "Project").

Invalid JDK configuration found, while importing a project via Gradle

I have installed IntelliJ and I need to import a Gradle project.
I have build the gradle project using command prompt with the gradlew build command.
At the IntelliJ welcome page, I have proceeded with proper instructions, and when I choose "Finish". I get the following error:
Invalid Gradle JDK configuration found. Open Gradle Settings JAVA_HOME ennvironment variable not set.
When I click on "Open Gradle Settings" it pop up with error of Not found with a path under IntelliJ directory in Program files and searching in jre/jre/bin/....etc.
Deleting .gradle and .idea will likely solve the problem.
So:
Close the project
Go to the project dir and delete .gradle and .idea
Get back and re-open the project using the IDE
These two must be generated locally on your PC (Some content of .idea might be version controlled though) and not pulled from a remote or somewhere else (Also they should be in .gitignore).
In my case the reason was that these two folders were generated on another computer and I had opened a project with these two folders existing before.
Just found the solution :
Create an empty Gradle project, then go to "Project Structure" and check the path to JDK (it should be valid, if it isn't you can add your own path).
Then build this empty project, wait and once done, close IntelliJ.
Relaunch it and try to import/open your Gradle project, now it should work.
You don't need to create a new project to fix this. You can do it from the main window (Configure -> Project Defaults -> Project Structure):
Then, on SDKs, set the appropriate JDK home path:
If you are on a Mac, click on the button with 3 dots and select the folder /Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home.
I've found this here:
https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000266650-invalid-gradle-jdk-configuration-found
Mac OS X Solution:
I had the same issue and fixed it by setting the JAVA_HOME environment variable using the command:
launchctl setenv JAVA_HOME /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/
Refer to this answer on how to set environment variables in Mac OS X:
Setting environment variables in OS X?
Close the project
Go to the project dir and delete
.gradle
.idea
Get back and re-open the project using the IDE
I recently had the same problem while importing a Gradle project. The trick was the remove the quotes from the JAVA_HOME variable. So instead of "C:\Program Files\Java\jdk1.8.0_66" my path now contains only the plain path C:\Program Files\Java\jdk1.8.0_66.
To add to the previous responses, if you want to prevent this problem when cloning a repository in Git, you can simply remove .idea/misc.xml from your .gitignore file. This contains information about the project jar. For example:
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_10" default="false" project-jdk-name="1.8" project-jdk-type="JavaSDK" />
</project>
For my case, I just restart the IDE and it works. It automatically download Gradle to suit the project version.
I had the same problem on the fresh installed Windows OS.
I did not have a JDK at all and forgot to check it invalid JDK configuration .
By default, you can check the Project configuration. If it is empty NO_SDK_ProjectStructure try to download JDK from Oracle web site and configure your project structure
I have faced same problem for tomcat 9 with my project based on Gradle.
You can easily rectify the problem by configuring the application.properties file with the following code.
location - src/main/resources/application.properties
server.port = 9090
spring.security.user.name= admin
spring.security.user.password= password
My issue was not addressed by the above solution, instead root cause was that I've imported settings from my old system and internal Intellij configuration was invalid because first jdk that it had in the list in jdk.table.xml pointed to a wrong path.
To fix this you should find this file in the intellij config folder and then simply open it with an editor and remove whole block related to the bad jdk version.
Close the project you are working on and then create another new project and build it and then close it and go back to your old project and it will work.
Comment this code on gradle.properties, the Issue was gone.
#org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home
I was getting:
Invalid JDK: /home/sz97/idea-IC-223.7571.182/jbr/bin/jlink is not a file!
Ensure JAVA_HOME or buildSettings.javaHome is set to JDK 15 or newer
As mentioned by Mahdi-Malv, delete .gradle & .idea folders from the project directory.
Then delete other SDKs from File > Project Structure > Platform Settings > SDKs keeping the only required one.
Finally change the SDK version from Project Structure > Project Settings > Project.
This may solve the problem.

How to define Gradle's home in IDEA?

I am trying to import a Gradle project into IntelliJ, and when I get to the Gradle Home textbox, it is not automatically populated, nor will typing in the path of Gradle Home result in a valid location - I have the GRADLE_USER_HOME environment variable set (to what I think is!) the correct path, and I have been able to successfully import this same project into Eclipse. Any suggestions?
You can write a simple gradle script to print your GRADLE_HOME directory.
task getHomeDir {
doLast {
println gradle.gradleHomeDir
}
}
and name it build.gradle.
Then run it with:
gradle getHomeDir
If you installed with homebrew, use brew info gradle to find the base path (i.e. /usr/local/Cellar/gradle/1.10/), and just append libexec.
The same task in Kotlin in case you use build.gradle.kts:
tasks.register("getHomeDir") {
println("Gradle home dir: ${gradle.gradleHomeDir}")
}
Installed on a Mac via Homebrew, the path
/usr/local/opt/gradle/libexec
is preferable to
/usr/local/Cellar/gradle/X.X/libexec
since the former will survive version upgrades.
If you installed gradle with homebrew, then the path is:
/usr/local/Cellar/gradle/X.X/libexec
Where X.X is the version of gradle (currently 2.1)
If you are using IntelliJ, just do the following.
Close the project
(re)Open the project
you will see "Import gradle project" message on the right bottom. click.
select "Use default gradle wrapper". not "Use local gradle distribution"
That's all.
This is what helped me solve the problem of not having Gradle home set for the IDEA when importing a Gradle project.
THREE OPTIONS -- (A) Default Wrapper (B) "gradle 'wrapper' task configuration" OR (C) "local gradle distribution" defined by jetbrains: https://www.jetbrains.com/help/idea/gradle-settings.html
A. Default Wrapper (recommended)
If you are able, select this recommended option. If it is grayed out, see option C, which should then set your default for all subsequent projects.
B. Gradle 'Wrapper' Task Configuration
If you want IDEA to define your gradle version for you from your build script
Set this option if you define your gradle build versions as a task within your actual gradle build.
Example below from jetbrains: https://www.jetbrains.com/help/idea/gradle-settings.html
(useful if you do not want to share gradle builds between projects)
C. Local Gradle Distribution
1. Run the following command to get gradle location:
brew info gradle (if gradle was installed with homebrew)
2. You are looking for something like this:
/usr/local/Cellar/gradle/4.8.1
3. Next, append 'libexec' to the gradle location you just found:
/usr/local/Cellar/gradle/4.8.1/libexec
This is because "libexec is to be used by other daemons and system utilities executed by other programs" (i.e. IDEA). Please see https://unix.stackexchange.com/questions/312146/what-is-the-purpose-of-usr-libexec
4. Finally, put that new path in the Gradle home input box if IDEA prompts you.
IDEA should now have allowed you to hit OK
C:\Users\<_username>\.gradle\wrapper\dists\gradle-<_version>-all\<_number_random_maybe>\gradle-<_version>
\Android studio\gradle didn't worked for me.
And "Default gradle wrapper" wasn't configured while importing (cloning) the project from bitbucket
If it causes problem to figure out the path, here is my path :
C:\Users\prabs\.gradle\wrapper\dists\gradle-5.4.1-all\3221gyojl5jsh0helicew7rwx\gradle-5.4.1
This is instruction for MAC only.
I had the same problem. I solved it by configuring $GRADLE_HOME in .bash_profile. Here's how you do it:
Open .bash_profile (usually it's located in the user’s home directory).
Add the following lines to update $PATH variable:
export GRADLE_HOME=/usr/local/opt/gradle/libexec
export PATH=$GRADLE_HOME/bin:$PATH
Save it.
Apply your changes by running
source .bash_profile
I wrote my own article with instruction in a case if somebody will encounter the same problem.
On a mac it should ideally be at : /Applications/Android Studio.app/Contents/gradle/gradle-2.14.1
(Replace the version string with the latest)
AFAIK it is GRADLE_HOME not GRADLE_USER_HOME (see gradle installation http://www.gradle.org/installation).
On the other hand I played a bit with Gradle support in Idea 13 Cardea and I think the gradle home is not automatically discover by Idea. If so you can file a issue in youtrack.
Also, if you use gradle 1.6+ you can use the Graldle support for setting the build and wrapper. I think idea automatically discover the wrapper based gradle project.
$ gradle setupBuild --type java-library
$ gradle wrapper
Note: Supported library types: basic, maven, java
Regards
If you're using MacPorts, the path is
/opt/local/share/java/gradle
I had to setup the Project SDK before selecting gradle path. Once that was set correctly, I had to choose "Use default gradle wrapper (recommended) in "Import Project from Gradle" dialog.
Still works if I remove gradle using brew:
$ brew remove gradle
This is where my gradle home is (Arch Linux):
/usr/share/java/gradle/
I had some weird errors where it could not find my class, I had to right click on my src folder (was red) to "Make Directory as" -> Source Folder Root
Click New -> Project from existing sources -> Import gradle project...
Then Idea recognized gradle automatically.
I couldn't get it to accept my Gradle JVM selection until I deleted a broken JDK
Th window below is from File -> Other Settings -> Structure For New Projects...
I had a red 1.8 JDK SDK entry here, once I deleted that Gradle JVM error below disappeared and I could move on to the next step
In case you are using Mac, most probably your gradle home should be /usr/local/gradle-2.0 for example.
In preference of IDEA search for gradle and set gradle home as given above.
It should work

IntelliJ bad plugin how to start

Hi I have just started using IntelliJ again and have version 9. I just installed the Mercurial plugin and now the ide won't start anymore.
Has an error of
Fatal error initializing class com.intellij.openapi.actionSystem.ActionManager:
java.lang.VerifyError: class com.dcx.hg.MercurialVcs overrides final method getName.()Ljava/lang/String;
I now know that I should be using the plugin hg4idea
Is there a way I can remove this plugin so I can start the ide, I am sure there must be..
Thanks in advance.
I am running on a Mac, the Plugin directory is located in ~/Library/Application Support/IntelliJIDEA60/ You can just delete the offending plugin. Note, depending on your edition of the IDE, the directory may be different. For example, IdeaIC2016.3
Version 11 for Windows stores plugins also in c:\Users\<username>\.IntelliJIdea11\config\plugins\ so delete plugins from that location as suggested by #duffymo.
If you look in the directory where you installed IntelliJ, you'll see a /plugins directory. Perhaps if you delete the Mercurial plugin folder you'll be able to restart the UI. Worth a try.
NOTE: Check here for OS/version related plugin path.
Ran into the same problem with another plugin. The solution was to remove the offending plugin folder or jar from the Plugins folder - see the exact location below for your OS.
The locations of the user plugins folder for different platforms are listed at https://intellij-support.jetbrains.com/entries/23358108-Directories-used-by-the-IDE-to-store-settings-caches-plugins-and-logs:
OSX
Configuration:
~/Library/Preferences/<PRODUCT><VERSION>
# e.g.:
~/Library/Preferences/IntelliJIdea14
Caches:
~/Library/Caches/<PRODUCT><VERSION>
Plugins:
~/Library/Application Support/<PRODUCT><VERSION>
Logs:
~/Library/Logs/<PRODUCT><VERSION>
Linux/Unix
~/.<PRODUCT><VERSION>
Windows
Windows Vista, 7, 8:
<SYSTEM DRIVE>\Users\<USER ACCOUNT NAME>\.<PRODUCT><VERSION>
Windows XP:
<SYSTEM DRIVE>\Documents and Settings\<USER ACCOUNT NAME>\.<PRODUCT><VERSION>
Version 15 on Ubuntu:
cd ~/.IntelliJIdea15/config/plugins
rm -r <offending_plugin>
Just delete the plugin. In my MacOS, the path to the plugins folder is:
/Users/<username>/Library/Application Support/JetBrains/<your version>/plugins