I need to create a sample code template in my IntelliJ IDEA project so that everybody in the team can also kind of import it in their IDEs and use it.
I am able to do it on my own machine by changing the "class" template myself, but i just don't have any way to make it available to my team like exporting it to a file that can be used, in Eclipse it is possible to have one sampleCodeFormatter.xml file that everybody can import in their eclipse workspace.
How does the same thing works in IntelliJ IDEA?
Create a project from a templateClick New Project on the Welcome screen or select File | New Project from the main menu. In the dialog that opens, click the required template in the Templates section on the left.
Save a file as a template Open a file in the editor. From the main menu, select File | Save File as Template. In the Save File as Template dialog, specify the new template name and edit the body, if necessary. Apply the changes and close the dialog.
By default, IntelliJ IDEA displays the code completion popup automatically as you type. If automatic completion is disabled, press Ctrl+Shift+Space or choose Code | Code Completion | Type-Matching from the main menu. If necessary, press Ctrl+Shift+Space once again.
First, in IntelliJ, open Settings (ctrl-alt-s), under IDE Settings find File and Code Templates
and have a look at the Templates
tab.
You will see some templates there already, for example there is one called Class
:
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end #parse("File Header.java") public class ${NAME} { }
Note that it includes File Header
, which is found under the Includes
tab.
Now, you can easily create your own file templates here. Under the Templates
tab, just hit the green "Add" button, give your new template a name (Foo
?),ensure the extension is java and write a valid java class.
Save, and your newly configured template exists here:
C:\Users\{USER}\.IntelliJIdea12\config\fileTemplates\Foo.java
You should be able to share this file with your team.
A neat feature is that from within the Project pane of a java project, under a source root, hit alt-enter
, choose a New Java Class and under the Kind drop-down - voila! You can see your Foo
template as a valid option.
Here's the contents of my File Header.java
/** * Project: ${PROJECT_NAME} * Package: ${PACKAGE_NAME} * <p> * User: ${USER} * Date: ${DATE} * Time: ${TIME} * <p> * Created with IntelliJ IDEA * To change this template use File | Settings | File Templates. */
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With