How to change the value of ${user} variable used in Eclipse templates
Solution 1:
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:
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:
Windows > Preferences > Java > Code Style > Code Templates > Comments
Or Open eclipse.ini
file and add following.
-Duser.name=Sumit Singh // Your Name
Solution 4:
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.