如何更改 Eclipse 模板中使用的 ${user} 变量的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/246192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the value of ${user} variable used in Eclipse templates
提问by Rajish
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 使用从帐户信息中获取的用户真实姓名,而不是对默认的 @author 模板进行硬编码(在 Linux 中 - 但也欢迎使用 Windows 解决方案)。将它输入到 Eclipse 配置的某个地方也是可以接受的,可惜我找不到正确的地方。
回答by Davide Inglima
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.
似乎最好的办法是user.name
在命令行中重新定义 java变量,或者使用eclipse.ini
eclipse 安装根目录中的文件。
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...
http://morlhon.net/blog/2005/09/07/eclipse-username/是一个死链接...
Here's a new one: https://web.archive.org/web/20111225025454/http://morlhon.net:80/blog/2005/09/07/eclipse-username/
这是一个新的:https: //web.archive.org/web/20111225025454/http: //morlhon.net: 80/blog/2005/09/07/eclipse-username/
回答by singletony
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.
打开 Eclipse,导航到窗口 -> 首选项 -> Java -> 代码样式 -> 代码模板 -> 注释 -> 类型,然后按“编辑”按钮。您可以在生成的评论中将您的姓名从@Author ${user} 更改为@Author Rajish。
回答by Sumit Singh
Windows > Preferences > Java > Code Style > Code Templates > Comments
Or Open eclipse.ini
file and add following.
或打开eclipse.ini
文件并添加以下内容。
-Duser.name=Sumit Singh // Your Name
回答by Frelling
EGit Solution
EGit解决方案
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.
人们期望基于项目、工作空间或环境创建或更改模板变量是标准的 Eclipse 特性。遗憾的是,事实并非如此。更重要的是,鉴于 Eclipse 插件可以定义新的变量和模板,应该有提供解决方案的插件。如果是,那一定很难找到。mmm-TemplateVariable在 Eclipse Marketplace 中可用,它是 Maven 用户朝着正确方向迈出的一步,提供了在模板中包含 version、artifactId 等的能力。
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.
幸运的是,EGit是一个用于 Git 的 Eclipse 工具,它提供了非常灵活的方法来在代码模板中包含许多不同的变量。唯一的要求是您的项目使用 Git。如果您不使用 Git,但对软件开发很认真,那么现在是学习的时候了(Pro Git 书籍)。如果您被迫使用旧版本控制系统,请尝试改变一些想法。
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).
感谢harmsk的努力,EGit 4.0 及更高版本包括在模板中使用Git 配置键值的能力。这允许根据存储库设置(项目)、用户设置(帐户)和/或全局设置(工作站)设置模板值。
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.
以下示例展示了如何为多用户开发工作站设置 Eclipse 和 Git,并使用自定义 Git 配置键${user}
来提供更大的灵活性。尽管该示例基于 Eclipse Mars 和 Git for Windows 的 Windows 10 安装,但该示例适用于使用各自命令行工具运行 Eclipse 和 Git 的 Linux 和 OSX。
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.
为了避免 Git 的user.name
配置键和 Java 的user.name
系统属性之间可能出现的混淆,自定义 Git 配置键 – user.author
– 将用于提供作者的姓名和/或凭据。
Configuring Templates
配置模板
The format of a Git template variable is as follows
Git模板变量的格式如下
${<name>:git_config(<key>)}
${<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→Typestemplate to
where<name>
是任意变量名,<key>
是应该使用其值的 Git 配置键。鉴于此,将Comments→Types模板更改为
/**
* @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.
现在将尝试从 Git 的user.author
配置键解析作者的姓名。如果没有任何进一步的配置,任何新创建的评论都不会在 之后包含名称@author
,因为还没有定义。
Configuring Git
配置 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 系统配置- 此配置步骤对适用于工作站上所有帐户的 Git 系统范围配置进行更改,除非被用户或存储库设置覆盖。因为系统范围的配置是底层 Git 应用程序的一部分(例如 Git for Windows),更改将需要管理员权限。以管理员身份运行 Git Bash、cmd 或 PowerShell。以下命令将设置系统范围的作者。
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 Commentfrom the context menu, ALT-SHIFT-J, or start a JavaDoc comment. The resulting @author
tag should be followed by the warning.
要验证此设置,请创建一个使用 Git 的空 Java 项目或打开一个现有的基于 Git 的项目。创建一个类并使用上下文菜单中的Source→Generate Element Comment,ALT-SHIFT-J或启动 JavaDoc 注释。生成的@author
标签后面应该是警告。
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 全局(用户)配置- 全局或用户配置是与特定用户关联的配置,将覆盖系统范围的配置。这些设置适用于所有基于 Git 的项目,除非被存储库设置覆盖。如果作者姓名因工作、开源贡献或个人等各种项目类型而不同,请在此处设置最常用的。
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.
配置完全局值后,返回到早期使用的测试项目并应用类注释。该@author
标签现在应显示全局设置。
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 存储库(本地)配置- 最后,存储库或本地配置可用于为特定项目配置作者。与之前的配置不同,存储库配置必须在存储库内完成。使用 Git Bash、PowerShell 等导航到测试项目的存储库。
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.
鉴于此,测试项目中的新评论将使用本地定义的作者姓名。其他基于 Git 的项目,仍将使用全局作者姓名。
From Within Eclipse
从 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.
上述配置更改也可以在 Eclipse 中通过其Preferences: Team→Git-Configuration 进行设置。Eclipse 必须以管理员身份运行才能更改系统范围的 Git 配置。
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.
尽管此示例专门处理最常见的问题,即更改${user}
,但此方法可用于更多。但是,除非有特别的意图,否则应该小心不要使用 Git 定义的配置键。
回答by Anuj Balan
Rather than changing ${user}
in eclipse, it is advisable to introduce
与其${user}
在 eclipse 中改变,不如引入
-Duser.name=Whateverpleaseyou
-Duser.name=Whateverpleaseyou
in eclipse.ini
which is present in your eclipse folder.
在eclipse.ini
它存在于你的eclipse文件夹。
回答by ?ukasz Siwiński
dovescrywolf gave tip as a comment on articlelinked by Davide Inglima
dovescrywolf 在评论Davide Inglima 链接的文章时给出了提示
It was was very useful for me on MacOS.
这对我在 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.
- 如果 Eclipse 已打开,请关闭它。
打开 Termnal(bash 控制台)并执行以下操作:
$ pwd /Users/You/YourEclipseInstalationDirectory $ cd Eclipse.app/Contents/MacOS/ $ echo "-Duser.name=Your Name" >> eclipse.ini $ cat eclipse.ini
关闭终端并再次启动/打开 Eclipse。
回答by FranMowinckel
This is the file we're all looking for (inside your Eclipse workspace):
这是我们都在寻找的文件(在您的 Eclipse 工作区中):
.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.ui.prefs
.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.
您将找到一个带有您要更改的名称的 @author 标签。重新启动 Eclipse,它将起作用。
For accents you have to use the Unicode format (e.g. '\u00E1' for á).
对于重音,您必须使用 Unicode 格式(例如,'\u00E1' 代表 á)。
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.
您还可以按照先前的答案建议修改“ini”文件,或为全局解决方案设置用户名 var。或者在本地解决方案的首选项菜单中覆盖@author 标签。这些都是解决这个问题的有效方法。
But if you're looking for 'that' author name that is bothering most of us, is in that file.
但是,如果您正在寻找困扰我们大多数人的“那个”作者姓名,那么它就在该文件中。
回答by parasrish
edit the file /etc/eclipse.ini
, so as to contain entry as;
编辑文件/etc/eclipse.ini
,以便包含条目;
-Duser.name=myname
-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}.
重新启动“eclipse”,现在,在创建任何新文件时,使用向导 (c/c++/java),它将使用“myname”代替 ${user}。
回答by Waqas Ahmed
Window -> Preferences -> Java -> Code Style -> Code Templates -> Comments -> Types Chnage the tage infront ${user} to your name.
Window -> Preferences -> Java -> Code Style -> Code Templates -> Comments -> Types 将前面 ${user} 的标签更改为您的名字。
Before
前
/**
* @author ${user}
*
* ${tags}
*/
After
后
/**
* @author Waqas Ahmed
*
* ${tags}
*/
回答by OJVM
just other option. goto PREFERENCES >> JAVA >> EDITOR >> TEMPLATES, Select @author and change the variable ${user}.
只是其他选择。转到PREFERENCES >> JAVA >> EDITOR >> TEMPLATES,选择 @author 并更改变量 ${user}。