Adding author name in Eclipse automatically to existing files

142,628

Solution 1

Actually in Eclipse Indigo thru Oxygen, you have to go to the Types template Window -> Preferences -> Java -> Code Style -> Code templates -> (in right-hand pane) Comments -> double-click Types and make sure it has the following, which it should have by default:

/**
 * @author ${user}
 *
 * ${tags}
 */

and as far as I can tell, there is nothing in Eclipse to add the javadoc automatically to existing files in one batch. You could easily do it from the command line with sed & awk but that's another question.

If you are prepared to open each file individually, then selected the class / interface declaration line, e.g. public class AdamsClass { and then hit the key combo Shift + Alt + J and that will insert a new javadoc comment above, along with the author tag for your user. To experiment with other settings, go to Windows->Preferences->Java->Editor->Templates.

Solution 2

To old files I don't know how to do it... I think you will need a script to go thru all files and add the header.

To change the new ones you can do this.

Go to Eclipse menu bar

  1. Window menu.
  2. Preferences
  3. search for Templates
  4. go to Code templates
  5. click on +code
  6. Click on New Java files
  7. Click Edit
  8. add

/**
${user}
*/

And it's done every new File will have your name on it !

Solution 3

Shift + Alt + J will help you add author name in existing file.

To add author name automatically,
go to Preferences --> --> Code Style --> Code Templates

Preferences -- Java -- Code Style -- Code Templates

in case you don't find above option in new versions of Eclipse - install it from https://marketplace.eclipse.org/content/jautodoc

Solution 4

You can control select all customised classes and methods, and right-click, choose "Source", then select "Generate Element Comment". You should get what you want.

If you want to modify the Code Template then you can go to Preferences -- Java -- Code Style -- Code Templates, then do whatever you want.

Solution 5

Quick and in some cases error-prone solution:

Find Regexp: (?sm)(.*?)([^\n]*\b(class|interface|enum)\b.*)

Replace: $1/**\n * \n * @author <a href="mailto:[email protected]">John Smith</a>\n */\n$2

This will add the header to the first encountered class/interface/enum in the file. Class should have no existing header yet.

Share:
142,628

Related videos on Youtube

Franz Kafka
Author by

Franz Kafka

Czech beer is awesome

Updated on July 09, 2022

Comments

  • Franz Kafka
    Franz Kafka almost 2 years

    Is there a real easy to use tool (no monster tool) that I can plug into Eclipse, and press a "generate header" button and then the authors name appears in every file in that project?

    • Andy Thomas
      Andy Thomas over 12 years
      Note: Author reports that the files already exist. Eclipse Code Templates don't solve this problem.
  • Andy Thomas
    Andy Thomas over 12 years
    This question is for files that already exist.
  • Universe
    Universe almost 10 years
    ADDING HELP To find the Comments ->Types Go to Window -> Preferences -> Java -> Code Style -> Code templates in the window will appear the Comments drop-down
  • Adam
    Adam about 8 years
    thanks @Universe - now updated.
  • gonephishing
    gonephishing about 8 years
    shift+alt+j, that was what i was looking for :)
  • lordscales91
    lordscales91 over 6 years
    It's important to mention that if the class is annotated the JavaDoc will be placed after the annotations, which is still a valid syntax but probably will not pass PMD Checkstyles.
  • mannedear
    mannedear almost 6 years
    Thats right! Tick for Automatically add comments... is good for including author name in newly creating files.
  • Marcelo Rebouças
    Marcelo Rebouças over 5 years
    Very useful. There are any shortcut to sign the method?? Thanks!
  • Ripudaman Singh
    Ripudaman Singh about 4 years
    For Mac user's the key combination is: option+command+J