How IntelliJ IDEA File Templates made my life easier as a developer.

Fazlizekiqi
3 min readApr 21, 2021

How often do you find yourself writing boilerplate code over and over again in IntelliJ?

Maybe you just have started with programming, or maybe you are a senior developer who didn’t care enough to explore IntelliJ deeper, or just maybe you weren't aware that IntelliJ has this kinda functionality.

In this article, I am going to show how File Templates in IntelliJ IDEA made my life easier as a software engineer.

Keep in mind, that you can use these instructions on other JetBrains IDE’s
“WebStorm, PyCharm, RubyMine etc.”

Let’s start from the basic example:
If you ever have developed in Java you may have encountered:
“What were the Lombok annotations to create an entity ” problem or
“What was the value of @Type in Hibernate for the UUID” or something similar.
Let’s just say that you already have an awesome java entity that you don't want to write from scratch again.
An entity that looks something like this:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
public class MyAwesomeEntity {

@Id
@GeneratedValue
@Column(length = 36, columnDefinition = "varchar(36)", updatable = false, nullable = false)
@Type(type = "org.hibernate.type.UUIDCharType")
private UUID id;

@CreationTimestamp
@Column(updatable = false)
private Timestamp createdDate;
@UpdateTimestamp
private Timestamp lastModifiedDate;

}

Now, to stop reproducing this code from scratch over and over again we can just press :
- Double SHIFT (Search everywhere)
- Search: “Save File as template”
- Give it the name “MyAwesomeEntity”
And then hit
OK

Double “Shift” in IntelliJ IDEA
Creating a File Template

Now that we have created our own File Template, we can use it in a different project or just maybe share it with another colleague that loves to code in IntelliJ.

Here’s how we use the file template:
- Open Project tool Window
(Mac -> cmd+1 or Windows -> alt+1)
- Right click on the package that you want to create the entity
- Choose Java Class
- Give it a name “MyAwesomeEntityReused”
-
Choose the template “MyAwesomeEntity”
- Press
Enter

Using a File Template

File Templates can be created for every language that IntelliJ IDEA has support.
We can also go in and modify our “File Templates” later with the help of Apache Velocity to have some code executed before the template is created.
Or maybe include some of the Live Templates inside our File templates.

Give it a try and let me know in the comments below if this article made your life easier.

--

--