如何在Eclipse插件项目的源文件中管理许可证横幅

时间:2023-01-15 12:50:01

I'm about to release a set of Eclipse plug-ins as Open Source and noticed that most source code released under the LGPL/EPL contains a header banner in each file that refers to the license or contains the license itself.

我即将发布一组Eclipse插件作为开源,并注意到LGPL / EPL下发布的大多数源代码在每个文件中都包含一个引用该许可证或包含许可证本身的标题横幅。

Since adding these banners to each file manually seems to be a daunting and error-prone task. How can I go about automating the insertion of these banners?

由于手动将这些横幅添加到每个文件似乎是一项令人生畏且容易出错的任务。如何自动插入这些横幅?

2 个解决方案

#1


6  

Concerning best practises, I believe you should have your license text in a separate file and have a build tool (ie ant) to add it at the beginning of all other files. Since you are talking about an open source project you would need a build process anyway for thinks like generating the javadocs, publishing releases etc.

关于最佳实践,我相信您应该将许可文本放在单独的文件中,并使用构建工具(即ant)将其添加到所有其他文件的开头。既然你在谈论一个开源项目,那么无论如何你都需要一个构建过程来思考如生成javadoc,发布版本等。

BTW,ant tasks are simple Java classes so it should be easy to write one yourself if you don't find an ant plugin that does exactly that.

顺便说一句,ant任务是简单的Java类,所以如果你没有找到一个完全相同的ant插件,你应该很容易自己编写一个。

Coming to eclipse, to my knowledge, it cannot do something like this. The quickest way I can think of to do it is with bash (if you are using Linux). Assume the file msg contains the text you want to add at the beginning of every file.

根据我的知识,它会发生变态,它无法做到这样的事情。我能想到的最快的方法是使用bash(如果你使用的是Linux)。假设文件msg包含要在每个文件开头添加的文本。

  1. Create a new directory to store the files:

    创建一个新目录来存储文件:

    mkdir ~/outdir

  2. Add the msg at the beginning of every file and put the result at the outdir

    在每个文件的开头添加msg,并将结果放在outdir

    for i in ls "*.java"; do cat msg $i > ~/outdir/$i ; done

    我在ls“* .java”;做cat msg $ i>〜/ outdir / $ i; DONE

Similarly you can write a command that does the same recursively, with an extra step to create the directory strucutre:

类似地,您可以编写一个递归执行相同操作的命令,并使用额外的步骤来创建目录结构:

mkdir ~/outdir
for i in `find -type d | sed 's/\.//' | grep -v "^$"`; do mkdir ~/outdir$i; done
for i in `find -name "*.java"`; do cat msg $i > ~/outdir/$i ; done

#2


2  

A more Eclipse-like approach than the manual addition is the following, done via GUI in Eclipse. Note that these are the Linux / Windows menus; Mac is a bit different.

与手动添加类似的Eclipse方法如下,通过Eclipse中的GUI完成。请注意,这些是Linux / Windows菜单; Mac有点不同。

  1. Open Windows->Preferences
  2. Navigate to Java->Code Style->Code Templates
  3. 导航到Java->代码样式 - >代码模板

  4. Edit the Comments->Files comment template to include your boilerplate.
    There are variables for the current year, file name, etc...
  5. 编辑评论 - >文件评论模板以包含您的样板。当年有变量,文件名等......

Note, also, that this is a solution for new files only; it's not going to help you with old files; for that, I would use something like idrosid's solution for your existing code

另请注意,这只是新文件的解决方案;它不会帮助你处理旧文件;为此,我会使用像idrosid的解决方案来为您的现有代码

#1


6  

Concerning best practises, I believe you should have your license text in a separate file and have a build tool (ie ant) to add it at the beginning of all other files. Since you are talking about an open source project you would need a build process anyway for thinks like generating the javadocs, publishing releases etc.

关于最佳实践,我相信您应该将许可文本放在单独的文件中,并使用构建工具(即ant)将其添加到所有其他文件的开头。既然你在谈论一个开源项目,那么无论如何你都需要一个构建过程来思考如生成javadoc,发布版本等。

BTW,ant tasks are simple Java classes so it should be easy to write one yourself if you don't find an ant plugin that does exactly that.

顺便说一句,ant任务是简单的Java类,所以如果你没有找到一个完全相同的ant插件,你应该很容易自己编写一个。

Coming to eclipse, to my knowledge, it cannot do something like this. The quickest way I can think of to do it is with bash (if you are using Linux). Assume the file msg contains the text you want to add at the beginning of every file.

根据我的知识,它会发生变态,它无法做到这样的事情。我能想到的最快的方法是使用bash(如果你使用的是Linux)。假设文件msg包含要在每个文件开头添加的文本。

  1. Create a new directory to store the files:

    创建一个新目录来存储文件:

    mkdir ~/outdir

  2. Add the msg at the beginning of every file and put the result at the outdir

    在每个文件的开头添加msg,并将结果放在outdir

    for i in ls "*.java"; do cat msg $i > ~/outdir/$i ; done

    我在ls“* .java”;做cat msg $ i>〜/ outdir / $ i; DONE

Similarly you can write a command that does the same recursively, with an extra step to create the directory strucutre:

类似地,您可以编写一个递归执行相同操作的命令,并使用额外的步骤来创建目录结构:

mkdir ~/outdir
for i in `find -type d | sed 's/\.//' | grep -v "^$"`; do mkdir ~/outdir$i; done
for i in `find -name "*.java"`; do cat msg $i > ~/outdir/$i ; done

#2


2  

A more Eclipse-like approach than the manual addition is the following, done via GUI in Eclipse. Note that these are the Linux / Windows menus; Mac is a bit different.

与手动添加类似的Eclipse方法如下,通过Eclipse中的GUI完成。请注意,这些是Linux / Windows菜单; Mac有点不同。

  1. Open Windows->Preferences
  2. Navigate to Java->Code Style->Code Templates
  3. 导航到Java->代码样式 - >代码模板

  4. Edit the Comments->Files comment template to include your boilerplate.
    There are variables for the current year, file name, etc...
  5. 编辑评论 - >文件评论模板以包含您的样板。当年有变量,文件名等......

Note, also, that this is a solution for new files only; it's not going to help you with old files; for that, I would use something like idrosid's solution for your existing code

另请注意,这只是新文件的解决方案;它不会帮助你处理旧文件;为此,我会使用像idrosid的解决方案来为您的现有代码