28

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?

Honza Zidek
  • 9,204
  • 4
  • 72
  • 118
ashish
  • 303
  • 1
  • 3
  • 7

2 Answers2

36

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.

vikingsteve
  • 38,481
  • 23
  • 112
  • 156
  • Thanks for the reply. I am able to create a java file under Templates tab, that's fine and i can access that under the location C:\Users\{USER}\.IntelliJIdea12\config\fileTemplates\ but what i want is that the default template gets override by my FooClass template so that when anybody creates a new class the FooClass template get picked up not the default one, which i am not able to do because when i create a new Java class it still picks up from the default template not my FooClass Template. and i want to do similarly for all other things also like: FooInteface FooEnum FooAnnotation – ashish Oct 31 '13 at 23:16
  • 1
    Ok, so you want to modify the default class template? In File and Code Templates, just modify the `Class` template, look in C:\Users\{User}\.IntelliJIdea12\config\fileTemplates\internal\ and share `Class.java` with your colleagues. – vikingsteve Nov 01 '13 at 14:57
  • 1
    Thanks a lot Steve! that's i was exactly looking for. \n Now i want to do it for default Interface, Enum and Annotation but in my intelliJ installation i am not able to see interface.java file similar to class.java at the same path: C:\Users\{User}\.IntelliJIdea12\config\fileTemplates\internal\. \n I assumed that they should be same way as Class.java but seems not the case. So are they stored at some other location in intelliJ ? – ashish Nov 01 '13 at 17:39
  • They are indeed stored in the same place, you just need to modify the templates for Interface, Enum and Annotation within IntelliJ first - and then they will appear in the file system. Have fun with this! ;) – vikingsteve Nov 01 '13 at 18:23
  • Thanks for sharing this info...that's quite strange though!.. oh that explains also, why i wasn't able to see any class.java file at the same path on my team members' machine. But that's a drawback again as i can not give my class.java file to my team members and ask them to (re)place their class.java under that location since they are not able to see any file there ? This was my whole purpose! – ashish Nov 02 '13 at 01:48
  • I would guess you can indeed just ask them to put it there, even if it didn't exist already. This sort of functionality is designed to minimize the configuration in xml files - only configuration that differs from the default seems to be configured on the file system (thus giving a faster startup when you open intellij since less files need to be read?) – vikingsteve Nov 02 '13 at 10:10
1

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.
 */
ranma2913
  • 1,058
  • 10
  • 8