How to manage license banners in source files of Eclipse projects
Asked Answered
A

2

18

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.

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?

Edit: I've eventually found Copyright Wizard and Copyright Generator which are Eclipse plug-ins which also allow for updating existing license banners.

Auden answered 15/10, 2008 at 13:22 Comment(3)
You should probably change the question title since it's misleading: the question applies to any code managed using Eclipse (not just Eclipse plug-in projects). I will then +1 the question because it's a good one! (I thought it was a bit presumptuous to 'force' this directly as an edit.) Or have I missed something?Dubrovnik
...and tag it with eclipse and licensingDubrovnik
I was able to use Copyright Tool for Eclipse: codejava.net/ides/eclipse/… Any idea if this is possible in JDeveloper?Vishnu
E
5

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.

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.

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.

  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

    for i in ls "*.java"; do 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
Eachern answered 15/10, 2008 at 14:28 Comment(2)
Thanks for outlining your solution to the problem. Unfortunately, putting this into the build step would not affect code in SVN.Auden
@Auden But you could still run this as a one-off transformation of your source and then check this all back in. (I suspect you know that :-), but to be clear that this is still a potential solution.Dubrovnik
P
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.

  1. Open Windows->Preferences
  2. Navigate to Java->Code Style->Code Templates
  3. Edit the Comments->Files comment template to include your boilerplate.
    There are variables for the current year, file name, etc...

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

Pazit answered 15/10, 2008 at 21:37 Comment(3)
Thanks, but as you said this only affects new files. Additionally, changing existing banners is not possible with Eclipse's code templates.Auden
I know. You sort of spurred me to look into Eclipse's capabilities on the subject, and it is a bit sparse. Looks like something suitable for a plugin, actually. Perhaps when I have some time...Pazit
I've actually edited my question and included a link to a plug-in that looks very promising.Auden

© 2022 - 2024 — McMap. All rights reserved.