How to change the value of ${user} variable used in Eclipse templates

Eclipse

Eclipse Problem Overview


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.

Eclipse Solutions


Solution 1 - Eclipse

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/

Solution 2 - Eclipse

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.

Solution 3 - Eclipse

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

Solution 4 - Eclipse

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.

Solution 5 - Eclipse

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

-Duser.name=Whateverpleaseyou

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

Solution 6 - Eclipse

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.

Solution 7 - Eclipse

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.

Solution 8 - Eclipse

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}.

Solution 9 - Eclipse

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

Solution 10 - Eclipse

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRajishView Question on Stackoverflow
Solution 1 - EclipseDavide InglimaView Answer on Stackoverflow
Solution 2 - EclipsesingletonyView Answer on Stackoverflow
Solution 3 - EclipseSumit SinghView Answer on Stackoverflow
Solution 4 - EclipseFrellingView Answer on Stackoverflow
Solution 5 - EclipseAnuj BalanView Answer on Stackoverflow
Solution 6 - EclipseŁukasz SiwińskiView Answer on Stackoverflow
Solution 7 - EclipseFranMowinckelView Answer on Stackoverflow
Solution 8 - EclipseparasrishView Answer on Stackoverflow
Solution 9 - EclipseWaqas AhmedView Answer on Stackoverflow
Solution 10 - EclipseOJVMView Answer on Stackoverflow