Ten Tips and Tricks in IntelliJ IDEA you might not have known
If you’re new to Java development, you’re probably still getting used to your IDE. Chances
are, you’re still probably using some old software like NetBeans or Eclipse.
IntelliJ IDEA, however, is probably the most useful and modern Java IDE there is out there.
And if you think the free version doesn’t have the features you need, you’re probably
not looking hard enough.
Here are some of the tricks I have found using IntelliJ that you might not have known.
The tips in this video will progressively get more advanced.
1. Code Generation It’s very easy to generate snippets of code.
This is helpful for when you need to quickly create a getter or setter method, or a constructor.
Simply type Control-N on a Mac or Alt-Insert on Windows, select the type of method you
want to generate, select the fields for which you want to generate the code, then hit enter.
If you want to generate code for multiple fields, hold down shift to select multiple.
2. Comments Commenting out code is useful for removing
code without completely deleting the code in case you need it later. To quickly comment
out lines of code, select the lines you want, and press Command-Forward slash on Mac or
Control-Forward Slash on Windows, and the lines will be commented out. To uncomment
the lines, select the lines again, type Command-Forward slash or Windows equivalent and they will
no longer be commented out.
3. Refactoring It is said that the two hardest things in
programming are naming things, memory management, and off-by-one errors, and the first one is
no exception. Often you will find yourself having to rename identifiers. This is easy
to do in IntelliJ. Just right click on the identifier, go to Refactor > Rename and type
a new name for the identifier. You can do this to class names, field names, method names,
and variables. You can do a lot of tasks easily with refactoring. Just play around with the
options under the refactor menu to see what each one does.
4. Declaration/Usages Need to quickly navigate to a certain method
or field? Hold down command on Mac or control on Windows and click the method or field to
jump to its declaration. To view all usages of the field or method, do the same thing
but on the declaration. This navigational tip is helpful in large projects where multiple
classes are stored throughout multiple packages.
5. Artifacts In IntelliJ, building a JAR file requires
you to create an artifact. Go to your project structure. On Mac this is Command-Semicolon.
On Windows this is Control-Shift-Alt-S. Then click on artifacts, click the plus sign, hover
over JAR, and click From Modules with Dependencies. Select your main class if any and click OK,
then check the box that says "Build on make". Now click apply, and you're good to go. To
build the JAR, click the Make button on the top right, and after the process is done,
the JAR file will show up in your project directory under out/artifacts/Project-Name_jar/.
6. Adding External Libraries/JARs to your project
Adding dependencies is crucial to me as a Bukkit developer. I often need to add JARs
like a build of spigot.jar or an API. To do this in IntelliJ, open your project structure,
click on Libraries, click on the plus symbol, then click on Java. Navigate to your JAR file,
select it or multiple, then click OK. Click OK again and your dependencies are added.
7. Bundling Dependencies with your JAR files If you’ve added dependencies to your project,
these libraries will need to be available during runtime. If you create your JAR file
artifact after you add your dependencies, they will already be extracted into your JAR
file. But if you add a dependency later on, it will not be extracted into your JAR file,
you can add it by going to your artifact in your project structure, clicking the plus
symbol under Output Layout, selecting “Extracted Directory”, and selecting the JAR file of
your library. But be careful with this. Most APIs are licensed under an open-source license
that requires your code to also be under the same license, so if your software is proprietary,
you cannot extract a library like Apache Commons directly into your JAR.
8. Adding Dependencies with Maven Maven is growing to be a popular software
project management system. Every once in a while you’ll find yourself needing to depend
on a library that is only available from Maven. To add this kind of library, go to your project
structure. Go Libraries, and click on the plus symbol, and click From Maven. Here you
can do two things: Enter the Maven coordinates or search. Once you’ve selected the right
library, make sure “Download to:” is checked, then click OK. The JARs from Maven will be
automatically downloaded and added to your library.
9. Git Git is a version control system that allows
you to keep track of changes to your code. IntelliJ has built in support for Git among
other version control systems. To initialize Git in your project, Go to VCS > Import Into
Version Control > Create Git Repository. Select your source folder and click OK. Then open
the Version Control window by going to View > Tool Windows > Version Control. Right click
on Unversioned Files and click Add to VCS. You can now start committing changes by pressing
Command-K on Mac or Control-K on Windows. If you want to put the project on GitHub,
go to VCS > Import into Version Control > Share Project on GitHub. Enter a project name and
description and click Share. Your project is now on GitHub. To upload changes to GitHub
in the future, hover over Commit and select “Commit and Push…” instead of clicking
Commit.
10. Quickly Testing your Bukkit Plugin In IntelliJ you can customize Run Configurations.
You can create one that will automatically build your JAR file, move it to the plugins
folder of your test server, and start your server. This will easily improve the efficiency
of development. First, create a Run configuration by going to Run > Edit Configurations…,
clicking the plus symbol, and clicking JAR Application. Enter a name for the configuration.
Next to path to JAR, navigate to and select the spigot server JAR. Next to working directory,
navigate to the directory in which your test server is located. Scroll down, and under
Before launch: Activate tool window, click the plus symbol, and click Build Artifacts.
Check your JAR file, and click OK. Then in your project structure, go to Artifacts and
select your JAR file. Change the output directory to the plugins folder of your test server.
Now you can press the Run button or Shift-F10 to build the artifacts and start the server.
That’s it for Ten Tips and Tricks for using IntelliJ IDEA. I do want to mention before
I leave that the keyboard shortcuts mentioned in this video may not be entirely accurate,
for they can be different across systems and depending on the way you set up IntelliJ initially.
For Mac I use the “Mac OS X” Keymap, and for Windows I used the Windows/Linux Keymap.
To change this, go to the Preferences of IntelliJ, go to Keymap, and next to Keymaps select the
desired map.
If you found this video helpful please share it or at the very least leave a like. Also,
critical feedback is welcomed. I’m always trying to improve my skills, and if you think
I’m doing something wrong, please let me know. Thank you for watching.