以编程方式将Eclipse格式化程序配置文件XML导出到.prefs [英] Programmatically export eclipse formatter profile XML into .prefs

查看:200
本文介绍了以编程方式将Eclipse格式化程序配置文件XML导出到.prefs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的团队使用在Eclipse配置文件XML文件中定义的代码格式约定,如下所示:

 <?xml版本= 1.0编码= UTF-8独立=否?> 
< profiles version = 12>
< profile kind = CodeFormatterProfile name =此小组的格式约定 version = 12>
< setting id = org.eclipse.jdt.core.formatter.insert_space_after_ellipsis value =插入 />
...

我正在使用eclipse作为命令行格式化程序,使用命令类似于以下内容:

  eclipse -nosplash -application org.eclipse.jdt.core.JavaCodeFormatter \ 
-config $ {JAVA_FORMATTER_CONFIG_FILE} $ {*}

仅当 $ {JAVA_FORMATTER_CONFIG_FILE } 是一个Eclipse偏好设置文件,其文件名类似于 org.eclipse.jdt.core.prefs ,内容:

  eclipse.preferences.version = 1 
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode = enabled
org.eclipse。 jdt.core.compiler.codegen.targetPlatform = 1.8
org.eclipse.jdt.core.compiler.compliance = 1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier = error
org.eclipse.jdt.core.compiler.problem.enumIdentifier =错误
org.eclipse.jdt.core.compiler.source = 1.8

如何以编程方式获取 .prefs 文件

解决方案

从Eclipse Oxygen工作台上手动导入XML配置文件?


  • 导出当前首选项: 文件->导出->首选项

  • 选择 Java代码样式首选项

  • 选择名称和目标并保存(似乎必须使用.epf扩展名)



文件将(其中包括)自定义格式配置

  /instance/org.eclipse .jdt.ui / formatter_profile = _my-java-formatter 
/instance/org.eclipse.jdt.ui/formatter_settings_version=13
/instance/org.eclipse.jdt.ui/org.eclipse.jdt .ui.formatterprofiles =<?xml版本\ = 1.0编码\ = UTF-8独立\ = no?> \n<个人档案版本\ = 13> \ \n< profile kind\ = CodeFormatterProfile name\ = my-java-formatter version\ = 13> \n ...
...

然后您可以运行格式化程序

  eclipse -nosplash -application org.eclipse.jdt.core.JavaCodeFormatter -config eclipse-formatter.epf Test.java 

输出:

  org.eclipse.m2e.logback.configuration:初始化状态位置之前,已激活org.eclipse.m2e.logback.configuration捆绑包。状态位置初始化后将重试。 
org.eclipse.m2e.logback.configuration:登录配置文件:/home/luis/eclipse-workspace/.metadata/.plugins/org.eclipse.m2e.logback.configuration/logback.1.8.3.20180227-2137 .xml
SLF4J:类路径包含多个SLF4J绑定。
SLF4J:在[bundleresource://496.fwk1538849250:1 / org / slf4j / impl / StaticLoggerBinder.class]中找到绑定。
SLF4J:在[bundleresource://496.fwk1538849250:2 /中找到绑定org / slf4j / impl / StaticLoggerBinder.class]
SLF4J:有关说明,请参见http://www.slf4j.org/codes.html#multiple_bindings。
SLF4J:实际绑定的类型为[ch.qos.logback.classic.util.ContextSelectorStaticBinder]
org.eclipse.m2e.logback.configuration:初始化logback
配置名称:eclipse-formatter .epf
开始格式作业...
完成。

首选项导出完成一次,然后您可以共享 eclipse-formatter。 epf 文件。
希望有帮助。


My team uses code formatting conventions defined in an eclipse profile XML file that looks something like this:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<profiles version="12">
<profile kind="CodeFormatterProfile" name="This Team's Formatting Convention"  version="12">
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_ellipsis" value="insert"/>
...

I am using eclipse as a command-line formatter, using a command like the following:

eclipse -nosplash -application org.eclipse.jdt.core.JavaCodeFormatter \
    -config ${JAVA_FORMATTER_CONFIG_FILE} ${*}

This only works when ${JAVA_FORMATTER_CONFIG_FILE} is an eclipse preferences file with a filename like org.eclipse.jdt.core.prefs and contents:

eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8

How do I programmatically obtain the .prefs file without manually importing the XML profile from the eclipse UI?

解决方案

From Eclipse Oxygen workbench,

  • Export current preferences: File -> Export -> Preferences
  • Select "Java Code Style Preferences"
  • Choose a name and a destination and save (seems the .epf extension must be used)

The file will contain (among other things) the custom fomatter configuration

/instance/org.eclipse.jdt.ui/formatter_profile=_my-java-formatter
/instance/org.eclipse.jdt.ui/formatter_settings_version=13
/instance/org.eclipse.jdt.ui/org.eclipse.jdt.ui.formatterprofiles=<?xml version\="1.0" encoding\="UTF-8" standalone\="no"?>\n<profiles version\="13">\n<profile kind\="CodeFormatterProfile" name\="my-java-formatter" version\="13">\n...
...

Then you can run the Formatter as

eclipse -nosplash -application org.eclipse.jdt.core.JavaCodeFormatter -config eclipse-formatter.epf Test.java

Output:

org.eclipse.m2e.logback.configuration: The org.eclipse.m2e.logback.configuration bundle was activated before the state location was initialized.  Will retry after the state location is initialized.
org.eclipse.m2e.logback.configuration: Logback config file: /home/luis/eclipse-workspace/.metadata/.plugins/org.eclipse.m2e.logback.configuration/logback.1.8.3.20180227-2137.xml
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [bundleresource://496.fwk1538849250:1/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [bundleresource://496.fwk1538849250:2/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
org.eclipse.m2e.logback.configuration: Initializing logback
Configuration Name: eclipse-formatter.epf
Starting format job ...
Done.

The preferences export is done once, then you can share eclipse-formatter.epf file. Hope that helps.

这篇关于以编程方式将Eclipse格式化程序配置文件XML导出到.prefs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆