277

Instead of hardcoding the default @author template I would like Eclipse to use user's real name taken from account information (in Linux - but Windows solution is also welcome). Entering it somewhere into Eclipse configuration would be acceptable, too, alas I can't find the right place.

3

10 Answers 10

312

It seems that your best bet is to redefine the java user.name variable either at your command line, or using the eclipse.ini file in your eclipse install root directory.

This seems to work fine for me:

-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
256M
-vmargs
-Dosgi.requiredJavaVersion=1.5
-Duser.name=Davide Inglima
-Xms40m
-Xmx512m    

Update:

http://morlhon.net/blog/2005/09/07/eclipse-username/ is a dead link...

Here's a new one: https://web.archive.org/web/20111225025454/http://morlhon.net:80/blog/2005/09/07/eclipse-username/

10
  • 18
    The risk is that this will break any plug in that needs to know the user ID (which is the documented value of the user.name property). It would be nice if there was a better way to adjust the value Eclipse will use for that variable. May 5, 2010 at 23:42
  • 31
    For me the " caused a problem, I had to do: -Duser.name=Ron Kitay to have it behave properly
    – RonK
    Aug 1, 2011 at 5:11
  • 29
    An even BETTER solution would be for Eclipse to have another variable such as ${programmer} that one could define and then use in the templates (or even allow people to create their own variables!). But this IS Eclipse, sigh.
    – SMBiggs
    Nov 13, 2011 at 18:32
  • 3
    The -Duser.namemust come after the -vmargs flag (tested with Eclipse Mars). Mar 12, 2016 at 16:58
  • 2
    Dead link, here's an archive: web.archive.org/web/20111225025454/http://morlhon.net:80/blog/… Sep 8, 2018 at 13:50
107

Open Eclipse, navigate to Window -> Preferences -> Java -> Code Style -> Code Templates -> Comments -> Types and then press the 'Edit' button. There you can change your name in the generated comment from @Author ${user} to @Author Rajish.

7
  • 44
    That's not the answer to the question
    – Rajish
    Jul 12, 2011 at 8:07
  • 19
    @Rajish: you're absolutely right, but it may be the only reasonable workaround. After evaluating the options, it's what I did.
    – SMBiggs
    Nov 13, 2011 at 18:30
  • 16
    This is the best answer in my opinion - instead of messing with command line and eclipse.ini, which are platform dependent and arguably too cumbersome for the task they accomplish, one simply often wishes to do a per-project setting - and doing so in Project properties like suggested above may at present time be the only way to do it. Oct 9, 2012 at 9:23
  • 2
    I think is not the right answer because there is a lot of languages and editors using Eclipse, not only Java. So that only solve the problem for the ones using Eclipse to edit Java, not all other languages. Mar 13, 2013 at 1:58
  • 4
    This solution also is not appropriate if you are using shared template definitions with other users. Mar 3, 2014 at 16:26
41
Windows > Preferences > Java > Code Style > Code Templates > Comments

enter image description here

Or Open eclipse.ini file and add following.

-Duser.name=Sumit Singh // Your Name

enter image description here

21

EGit Solution

One would expect creating or changing template variables on a project-, workspace-, or environment-basis is a standard Eclipse feature. Sadly, it is not. More so, given that Eclipse plugins can define new variables and templates, there should be plugins out there providing a solution. If they are, they must be hard to find. mmm-TemplateVariable, which is available in the Eclipse Marketplace, is a step in the right direction for Maven users, giving the ability to include version, artifactId, etc. in templates.

Fortunately, EGit, which is an Eclipse tool for Git, provides very flexible means for including many different variables in code templates. The only requirement is that your project uses Git. If you don’t use Git, but are serious about software development, now is the time to learn (Pro Git book). If you are forced to use a legacy version control system, try changing some minds.

Thanks to the efforts of harmsk, EGit 4.0 and above includes the ability to use Git configuration key values in templates. This allows setting template values based on repository settings (project), user settings (account), and/or global settings (workstation).

The following example shows how to set up Eclipse and Git for a multi-user development workstation and use a custom Git configuration key in lieu of ${user} to provide more flexibility. Although the example is based on a Windows 10 installation of Eclipse Mars and Git for Windows, the example is applicable to Linux and OSX running Eclipse and Git using their respective command line tools.

To avoid possible confusion between Git’s user.name configuration key and Java’s user.name system property, a custom Git configuration key – user.author – will be used to provide an author’s name and/or credentials.

Configuring Templates

The format of a Git template variable is as follows

${<name>:git_config(<key>)}

where <name> is any arbitrary variable name and <key> is the Git configuration key whose value should be used. Given that, changing the Comments→Types template to

/**
 * @author ${author:git_config(user.author)}
 *
 * ${tags}
 */

will now attempt to resolve the author’s name from Git’s user.author configuration key. Without any further configuration, any newly created comments will not include a name after @author, since none has been defined yet.

Configuring Git

From the command line

Git System Configuration - This configuration step makes changes to Git’s system-wide configuration applicable to all accounts on the workstation unless overridden by user or repository settings. Because system-wide configurations are part the underlying Git application (e.g. Git for Windows), changes will require Administrator privileges. Run Git Bash, cmd, or PowerShell as Administrator. The following command will set the system-wide author.

git config --system user.author “SET ME IN GLOBAL(USER) or REPOSITORY(LOCAL) SETTINGS”

The purpose of this “author” is to serve as a reminder that it should be set elsewhere. This is particularly useful when new user accounts are being used on the workstation.

To verify this setting, create an empty Java project that uses Git or open an existing Git-based project. Create a class and use Source→Generate Element Comment from the context menu, ALT-SHIFT-J, or start a JavaDoc comment. The resulting @author tag should be followed by the warning.

The remaining configuration changes can be performed without Administrator privileges.

Git Global(User) Configuration - Global, or user, configurations are those associated with a specific user and will override system-wide configurations. These settings apply to all Git-based projects unless overridden by repository settings. If the author name is different due to various project types such as for work, open source contributions, or personal, set the most frequently used here.

git config --global user.author “Mr. John Smith”

Having configured the global value, return to the test project used early and apply a class comment. The@author tag should now show the global setting.

Git Repository(Local) Configuration - Lastly, a repository or local configuration can be used to configure an author for a specific project. Unlike the previous configurations, a repository configuration must be done from within the repository. Using Git Bash, PowerShell, etc. navigate into the test project’s repository.

git config --local user.author “smithy”

Given this, new comments in the test project will use the locally defined author name. Other Git-based projects, will still use the global author name.

From Within Eclipse

The configuration changes above can also be set from within Eclipse through its Preferences: Team→Git-Configuration. Eclipse must be run as Administrator to change system-wide Git configurations.

In Sum

Although this example dealt specifically with the most common issue, that of changing ${user}, this approach can be used for more. However, caution should be exercised not to use Git-defined configuration keys, unless it is specifically intended.

2
  • 1
    This is nice, but why not just use ${author:git_config(user.name)} as it is already standard? Jan 8, 2019 at 16:52
  • @RobD -- As mentioned in the post (5th paragraph from top) -- to avoid confusion. You are free to use whatever you want. Though in a production environment it is better to separate project configuration items from system configuration items.
    – Frelling
    Jan 9, 2019 at 14:54
16

Rather than changing ${user} in eclipse, it is advisable to introduce

-Duser.name=Whateverpleaseyou

in eclipse.ini which is present in your eclipse folder.

5
  • 5
    -1 No, eclipse.ini even saved as UTF-8 doesn't handle correctly French accent like àôéù...
    – Aubin
    Jan 14, 2013 at 21:26
  • 27
    What is the point of posting, four years later, the same solution than the accepted one?
    – PhiLho
    Feb 14, 2013 at 15:08
  • 2
    @Aubin actually right now I'm running an eclipse that supports accents like áóë.. etc with no problems. So this is a correct and helpfull answer. Mar 15, 2013 at 13:40
  • 1
    @PhiLho This answer is compact and concise, only 3-sentences. Also, the link in the selected answer is broken.
    – johnnieb
    Sep 1, 2017 at 18:38
  • "It is advisable" by whom? You? A generally respected authority? This option clearly has some issues.
    – Kröw
    Dec 17, 2022 at 18:44
3

dovescrywolf gave tip as a comment on article linked by Davide Inglima

It was was very useful for me on MacOS.

  • Close Eclipse if it's opened.
  • Open Termnal (bash console) and do below things:

    $ pwd /Users/You/YourEclipseInstalationDirectory  
    $ cd Eclipse.app/Contents/MacOS/  
    $ echo "-Duser.name=Your Name" >> eclipse.ini  
    $ cat eclipse.ini
    
  • Close Terminal and start/open Eclipse again.

1

This is the file we're all looking for (inside your Eclipse workspace):

.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.ui.prefs

You will find an @author tag with the name you want to change. Restart Eclipse and it will work.

For accents you have to use the Unicode format (e.g. '\u00E1' for á).

You can also modify the 'ini' file as prior answers suggest or set the user name var for a global solution. Or override the @author tag in the Preferences menu for a local solution. Those are both valid solutions to this problem.

But if you're looking for 'that' author name that is bothering most of us, is in that file.

3
  • This option actually modifies the setting shown in eclipse preferences (removes the tag {user}), by hand.
    – Alfabravo
    Jan 10, 2014 at 21:38
  • The file location is out of date for Eclipse Luna.
    – Mar
    Jan 9, 2015 at 18:03
  • It is $WORKSPACE/.metadata/.plugins/org.eclipse.core.runtime/.settings/… on Mars, and there are also $WORKSPACE/$PROJECT/.settings/… for each project. As mentioned, these are the workspace or project preferences, that can more comfortably be edited by hand (and explicitely excluded by the OP).
    – Dirk
    Aug 21, 2015 at 18:12
0

edit the file /etc/eclipse.ini, so as to contain entry as;

-Duser.name=myname

Restart the "eclipse" and now, on creation of any new file, with wizard (c/c++/java), it will use "myname" in place of ${user}.

-1

Window -> Preferences -> Java -> Code Style -> Code Templates -> Comments -> Types Chnage the tage infront ${user} to your name.

Before

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

After

/**
 * @author Waqas Ahmed
 *
 * ${tags}
 */

enter image description here

-4

just other option. goto PREFERENCES >> JAVA >> EDITOR >> TEMPLATES, Select @author and change the variable ${user}.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.