How to Add Copyright License Header for Java Source Files in Eclipse
- Details
- Written by Nam Ha Minh
- Last Updated on 07 August 2019   |   Print Email
In programming, copyright license is usually put at the beginning of every source file. This article shows you how to manage copyright license with ease in Eclipse. You will be able to generate (and update or rewrite whenever you want) a nice copyright license like the following screenshot:
There are two different ways to add/update copyright license to Java source files. We look at the simplest way first.
1. Modifying Java Code Templates
In this way, we use a built-in feature of Eclipse. Click Window > Preferences to launch the Preferences dialog. Then expand the tree on the left to the branch Java > Code Style > Code Templates:
Then expand the branch Code > New Javafiles under the section ‘Configure generated code and comments’. We are going to modify code template of every new Java file created afterward, so click Edit button. In the Edit Template dialog, insert some sentences for your copyright license into the Pattern text area:
NOTE: You can insert some predefined variables by using the button Insert Variable.
Click OK when you are done editing. Then we get back to the Preferences dialog with the newly updated pattern:
From now on, Eclipse will insert the above copyright license into every new Java file you created afterward.
But what if you already have dozens of files in your project and you want to update the copyright statements for all these files? Search and replace the entire project maybe a good choice, but it’s not the best. Let’s move on the second way to explore the best solution.
2. Using Eclipse’s Releng Tools
Eclipse has a very nice tool called Releng which is intended to solve all headaches regarding copyright license update in Java projects. Releng has a unique function called “Fix Copyrights” which is very useful. Because Eclipse doesn’t include this tool by default, so we have to install it.
Go to Help > Install New Software… to open the Install dialog:
Select “The Eclipse Project Updates - http://download.eclipse.org/eclipse/update/4.4” from the Work with dropdown list. The number is different depending on your Eclipse version, here I use Luna, hence the number 4.4.
Eclipse then lists all updates in the below table. Scroll down to the bottom of the list, check Releng Tools and click Next.
The next screen is for review purpose:
Click Next to proceed to the Review Licenses screen:
Choose “I accept the terms of the license agreement” then click Finish. Wait a while for Eclipse to install the RelengTools:
After the installation completed, you have to re-start Eclipse when asked:
Now go to Window > Preferences, and select Copyright Tool:
You can see there is a default copyright template, edit the template as you want and then click OK to save changes. Here what we typed:
NOTE: You should check the option “Replace all existing copyright comments with this copyright template” so Eclipse will re-write copyright statements in all files.
Now we can use the Fix Copyrights context menu to apply copyright template to the whole project, a package or a single source file:
Notice Eclipse reports status of update in the Console view as shown in the following screenshot:
NOTE: Our test shows that the Releng Tools only works with projects shared using Git repository. If your project doesn’t have source control like Git, it doesn’t work.
Conclusion:
So far we have explored the two ways of generate and updating copyright license for source files in Eclipse. The modification of code templates is quick and easy but limited. And the Releng Tools is the ultimate solution which is more powerful and flexible. But one limitation of Releng is that it works with Git-based projects only. Hope Eclipse will make it to works with all projects soon.
References:
Other Eclipse Tutorials:
- How to use Eclipse IDE for Java EE Developers
- How to create, build and run a Java Hello World program with Eclipse
- How to change font for Java code in Eclipse
- How to generate Javadoc in Eclipse
- How to create WAR file for Java web application in Eclipse
- How to create JAR file in Eclipse
- How to pass arguments when running a Java program in Eclipse
- How to create Java web project with Maven in Eclipse
- 25 Eclipse Shortcut Keys for Code Editing
- How to add custom Ant build script to Eclipse project
Comments
NOTE: Our test shows that the Releng Tools only works with projects shared using Git repository. If your project doesn’t have source control like Git, it doesn’t work.