玩转Eclipse — 项目的.settings目录解密
Eclipse创建一个项目后,都会为该项目创建一个.settings目录,可能很少有人会用到这个目录,所以它往往被大家忽略。最近为了在Eclipse自动创建的setter方法和构造函数的参数前面加个前缀,搜索了大量资料之后,最终在.settings目录下找到了解决方案。因此,特地花时间研究了一下该目录的功能。
从目录的命名就可以看出,它是与项目设置相关的。该目录主要包含两个文件:org.eclipse.jdt.core.prefs和org.eclipse.jdt.ui.prefs,利用NotePad++等软件可以查看和编辑这些文件。
1. org.eclipse.jdt.core.prefs该文件是与Compile相关的配置,一般情况下的内容如下:
eclipse.preferences.version=1editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=trueorg.eclipse.jdt.ui.javadoc=trueorg.eclipse.jdt.ui.text.custom_code_templates=<?xml version\="1.0" encoding\="UTF-8"?><templates><template autoinsert\="false" context\="filecomment_context" deleted\="false" description\="Comment for created Java files" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.filecomment" name\="filecomment">/*\r\n * Licensed Materials - Property of IBM\r\n *\r\n * OCO Source Materials\r\n *\r\n * (C) Copyright IBM Corp. ${year} All Rights Reserved\r\n *\r\n * The source code for this program is not published or other-\r\n * wise divested of its trade secrets, irrespective of what has\r\n * been deposited with the U.S. Copyright Office.\r\n */</template><template autoinsert\="false" context\="newtype_context" deleted\="false" description\="Newly created files" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.newtype" name\="newtype">${filecomment}\r\n${package_declaration}\r\n\r\nimport com.ibm.director.fvt.common.Copyright;\r\n\r\n${typecomment}\r\n@Copyright(Copyright.HEADER + "${year}" + Copyright.FOOTER)\r\n${type_declaration}</template><template autoinsert\="false" context\="classbody_context" deleted\="false" description\="Code in new class type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.classbody" name\="classbody"/><template autoinsert\="false" context\="interfacebody_context" deleted\="false" description\="Code in new interface type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.interfacebody" name\="interfacebody"/><template autoinsert\="false" context\="enumbody_context" deleted\="false" description\="Code in new enum type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.enumbody" name\="enumbody"/><template autoinsert\="false" context\="annotationbody_context" deleted\="false" description\="Code in new annotation type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.annotationbody" name\="annotationbody"/><template autoinsert\="true" context\="gettercomment_context" deleted\="false" description\="Comment for getter method" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.gettercomment" name\="gettercomment">/**\r\n * @return the ${bare_field_name}\r\n */</template><template autoinsert\="true" context\="settercomment_context" deleted\="false" description\="Comment for setter method" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.settercomment" name\="settercomment">/**\r\n * @param ${param} the ${bare_field_name} to set\r\n */</template><template autoinsert\="true" context\="constructorcomment_context" deleted\="false" description\="Comment for created constructors" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.constructorcomment" name\="constructorcomment">/**\r\n * ${tags}\r\n */</template><template autoinsert\="false" context\="typecomment_context" deleted\="false" description\="Comment for created types" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.typecomment" name\="typecomment">/**\r\n * @author yuejming@cn.ibm.com\r\n *\r\n * ${tags}\r\n */</template><template autoinsert\="true" context\="fieldcomment_context" deleted\="false" description\="Comment for fields" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.fieldcomment" name\="fieldcomment">/**\r\n * \r\n */</template><template autoinsert\="true" context\="methodcomment_context" deleted\="false" description\="Comment for non-overriding methods" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.methodcomment" name\="methodcomment">/**\r\n * ${tags}\r\n */</template><template autoinsert\="true" context\="overridecomment_context" deleted\="false" description\="Comment for overriding methods" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.overridecomment" name\="overridecomment">/* (non-Javadoc)\r\n * ${see_to_overridden}\r\n */</template><template autoinsert\="true" context\="delegatecomment_context" deleted\="false" description\="Comment for delegate methods" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.delegatecomment" name\="delegatecomment">/**\r\n * ${tags}\r\n * ${see_to_target}\r\n */</template><template autoinsert\="true" context\="catchblock_context" deleted\="false" description\="Code in new catch blocks" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.catchblock" name\="catchblock">// ${todo} Auto-generated catch block\r\n${exception_var}.printStackTrace();</template><template autoinsert\="true" context\="methodbody_context" deleted\="false" description\="Code in created method stubs" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.methodbody" name\="methodbody">// ${todo} Auto-generated method stub\r\n${body_statement}</template><template autoinsert\="true" context\="constructorbody_context" deleted\="false" description\="Code in created constructor stubs" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.constructorbody" name\="constructorbody">${body_statement}\r\n// ${todo} Auto-generated constructor stub</template><template autoinsert\="true" context\="getterbody_context" deleted\="false" description\="Code in created getters" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.getterbody" name\="getterbody">return ${field};</template><template autoinsert\="true" context\="setterbody_context" deleted\="false" description\="Code in created setters" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.setterbody" name\="setterbody">${field} \= ${param};</template></templates>sp_cleanup.add_default_serial_version_id=truesp_cleanup.add_generated_serial_version_id=falsesp_cleanup.add_missing_annotations=truesp_cleanup.add_missing_deprecated_annotations=truesp_cleanup.add_missing_methods=falsesp_cleanup.add_missing_nls_tags=falsesp_cleanup.add_missing_override_annotations=truesp_cleanup.add_missing_override_annotations_interface_methods=truesp_cleanup.add_serial_version_id=falsesp_cleanup.always_use_blocks=truesp_cleanup.always_use_parentheses_in_expressions=falsesp_cleanup.always_use_this_for_non_static_field_access=falsesp_cleanup.always_use_this_for_non_static_method_access=falsesp_cleanup.convert_to_enhanced_for_loop=falsesp_cleanup.correct_indentation=falsesp_cleanup.format_source_code=falsesp_cleanup.format_source_code_changes_only=truesp_cleanup.make_local_variable_final=falsesp_cleanup.make_parameters_final=truesp_cleanup.make_private_fields_final=falsesp_cleanup.make_type_abstract_if_missing_method=falsesp_cleanup.make_variable_declarations_final=truesp_cleanup.never_use_blocks=falsesp_cleanup.never_use_parentheses_in_expressions=truesp_cleanup.on_save_use_additional_actions=truesp_cleanup.organize_imports=truesp_cleanup.qualify_static_field_accesses_with_declaring_class=falsesp_cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=truesp_cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=truesp_cleanup.qualify_static_member_accesses_with_declaring_class=falsesp_cleanup.qualify_static_method_accesses_with_declaring_class=falsesp_cleanup.remove_private_constructors=truesp_cleanup.remove_trailing_whitespaces=falsesp_cleanup.remove_trailing_whitespaces_all=truesp_cleanup.remove_trailing_whitespaces_ignore_empty=falsesp_cleanup.remove_unnecessary_casts=falsesp_cleanup.remove_unnecessary_nls_tags=falsesp_cleanup.remove_unused_imports=falsesp_cleanup.remove_unused_local_variables=falsesp_cleanup.remove_unused_private_fields=truesp_cleanup.remove_unused_private_members=falsesp_cleanup.remove_unused_private_methods=truesp_cleanup.remove_unused_private_types=truesp_cleanup.sort_members=falsesp_cleanup.sort_members_all=falsesp_cleanup.use_blocks=truesp_cleanup.use_blocks_only_for_return_and_throw=falsesp_cleanup.use_parentheses_in_expressions=falsesp_cleanup.use_this_for_non_static_field_access=falsesp_cleanup.use_this_for_non_static_field_access_only_if_necessary=truesp_cleanup.use_this_for_non_static_method_access=falsesp_cleanup.use_this_for_non_static_method_access_only_if_necessary=true上面的示例中主要有javadoc、template和cleanup相关的配置,在Eclipse的UI界面上对它们的设置在《玩转Eclipse — 自动代码规范化》一文中有详细介绍。这个文件就没有太大必要去手动配置,如果真的不知道在UI界面的哪里进行配置,需要手动配置的话,可以Eclipse的官网关于该文件的示例文档。