1. Overview
1.综述
Adding a copyright license header for source files in the Eclipse IDE is known to be a difficult and error-prone task.
众所周知,在Eclipse IDE中为源文件添加版权许可头是一项困难且容易出错的任务。
In this tutorial, we’ll learn two ways to make this task easy and error-free. The first uses the Eclipse IDE’s Code Template feature. The second uses the Copyright Generator Plugin.
在本教程中,我们将学习两种方法来使这项任务变得简单和无误。第一种是使用Eclipse IDE的代码模板功能。第二种是使用版权生成器插件。
2. Using the Code Template
2.使用代码模板
First, let’s see how to add a copyright license header using the Code Template. Let’s begin with the Eclipse setup:
首先,让我们看看如何使用代码模板添加一个版权许可头。让我们从Eclipse的设置开始:
2.1. Eclipse Setup
2.1.Eclipse设置
- From the main menu bar, navigate to Preferences
- Then, navigate to Java -> Code Style -> Code Templates
- From the right-hand side of the window, expand the Code section and select New Java files
- Then, we go to Edit Template by clicking on the Edit button
- In the Edit Template window, we add our copyright license header in the Pattern text area
- And, click on the OK button to update the template
- Finally, let’s click on the Apply button to finish the setup
2.2. Applying Copyright
2.2.应用版权
We now get a copyright header automatically applied to each new Java source file:
我们现在得到了一个版权标题自动应用于每个新的Java源文件。
However, this method has some disadvantages:
然而,这种方法有一些缺点。
- We cannot use this to add a copyright header to existing source files
- We cannot include custom variables in the license text, like our company name
- It’s not flexible enough to let us choose different copyright license headers
- We can use this only on Java and Javascript source files
Luckily, there is an alternative in the form of the copyright generator plugin.
幸运的是,有一个替代方案,即版权生成器插件的形式。
3. Using the Copyright Generator Plugin
3.使用版权生成器插件
Let’s start by setting up the Copyright Generator plugin.
让我们从设置版权生成器插件开始。
3.1. Installing the Plugin
3.1.安装该插件
- We install the plugin by navigating to Help -> Eclipse Marketplace
- Then we search for Eclipse Copyright Generator in the Find text box
- Finally, let’s click on the Install button and follow the instructions
3.2. Customizing Copyright License Headers
3.2.定制版权许可标头
All standard copyright license headers are preinstalled with the plugin.
所有标准的版权许可头都预先安装在插件中。
But what if we want to add a custom header, or edit one? Let’s see how to do that in Copyright Preferences:
但是,如果我们想添加一个自定义标题,或编辑一个标题,该怎么办?让我们看看如何在版权首选项中这样做。
- We first navigate to Preferences -> General -> Copyright
- Then, to add a new copyright header, we click on the Add button
- To modify an existing license, we select a license from the Licenses and then click on the Modify button
3.3. Applying Copyright
3.3.应用版权
After installing the plugin, applying a copyright license header to one or more source files is quite easy.
安装插件后,将版权许可头应用于一个或多个源文件是非常容易的。
Let’s see how to apply it on the selected source files:
让我们看看如何在选定的源文件上应用它:
- From the Project Explorer panel, we select the source files
- Then, right-click on the selected source files
- From the context menu, we select the Apply copyright option
Similarly, to apply this to one or more projects:
类似地,要将其应用于一个或多个项目。
- From the main menubar, select Project -> Apply copyright
And then we follow the instruction in the Apply copyright dialog box to add the copyright header to the files:
然后我们按照应用版权对话框中的指示,在文件中添加版权头:。
- We click on the Next button, which takes us to the Copyright settings
- Then, select a copyright license from the Copyright type select box
- And, click on the Next button, which takes us to the files window
- Finally, we click on the Finish button to apply the copyright header to the selected source files
Now unlike Code Template, the plugin will not add a copyright header automatically on the creation of a new file.
现在与代码模板不同,该插件不会在创建新文件时自动添加版权头。
However, the plugin has several advantages over the Code Template:
然而,该插件与代码模板相比有几个优势。
- It’s easy to add a copyright license header for the existing source files
- We can include custom variables in the license text, like company name
- The plugin supports many different copyright license headers
- We can add a copyright header to all type of source files and not limited to Java source files
4. Conclusion
4.结论
In this article, we’ve learned two different ways to add a copyright license header to our source files.
在这篇文章中,我们已经学习了两种不同的方法来为我们的源文件添加版权许可头。
The Eclipse Copyright Generator plugin is the easiest and the most flexible way to add copyright headers.
Eclipse Copyright Generator插件是添加版权头的最简单和最灵活的方式。
The only disadvantage is that it needs to be applied after the files are created, where the Code Template will add a copyright header for us when each file is created.
唯一的缺点是,它需要在文件创建后应用,而代码模板将在每个文件创建时为我们添加一个版权头。。