myeclipse及Eclipse中.classpath、.project、.settings、.mymetadata(myeclipse特有)介绍

引言

  今天在创建java项目的时候遇到了很多的错误,在解决的过程中遇到了一些根本不知道什么作用的文件,然后按照网上的一些做法可以将问题解决,但是这也说明我们在学习的时候很多基础和细节的地方是我们薄弱的地方,我们总是对于经常写代码的那几个文件比较熟悉,这样一旦报错我们就无从下手啊。 下面介绍几个文件。

  一、文件.mymetadata文件 

  1、部署项目用的,把项目部署到tomcat服务器上时,如果没有这个文件,用myeclipse/Eclipse部署是不能部署到关联的tomcat服务器。 

 

[html] view plain copy
 
  1. <?xml version=”1.0” encoding=”UTF-8”?>    
  2. <!--    
  3. type : 工程类型    
  4. name : 工程名称    
  5. id : 工程在工作空间内的唯一标识    
  6. context-root : 网站根路径    
  7. j2ee-spec: J2EE标准    
  8. archive : 打包后war文件    
  9. -->    
  10. <project-module    
  11. type=”WEB”    
  12. name=”PheebsKM”    
  13. id=”myeclipse.1152954865843”    
  14. context-root=”/PheebsKM”    
  15. j2ee-spec=”1.4”    
  16. archive=”PheebsKM.war”>    
  17.     
  18. <attributes>    
  19.   <!-- value : Web根目录名称 -->    
  20.     <attribute name=”webrootdir” value=”WebRoot” />    
  21. </attributes>    
  22. </project-module>    

  2、在MyEclipse/Eclipse更改项目发布到Tomcat的文件夹 
   (1)、在自己的workspace中找到当前使用的项目,找到文件.mymetadata,打开后就会看见标签context- root=”/example”(example是自己应用的名称),可以将其改为别的名称。 
   (2)、选择:工程右键 --》Properties --》Myeclipse --》 Web 
   [Web-root folder]:/WebRoot 
   [Web Context-root]:/ROOT,修改context-root对应的值即可。(默认值:当前项目名称) 
  二、文件:.project
  .project描述了一个Eclipse项目。
  典型内容

[html] view plain copy
 
  1. <span style="font-family:SimSun;font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <projectDescription>  
  3.     <!-- name里的内容代表项目名字,对应了Eclipse项目的名称,不是Maven的finalName -->  
  4.     <name>demo</name>  
  5.     <!-- 项目的注释 -->  
  6.     <comment></comment>  
  7.     <!-- 引用的项目的名字 -->  
  8.     <projects>  
  9.     </projects>  
  10.     <!-- 有序的列表,定义了一系列的构建命令(buildCommand) -->  
  11.     <buildSpec>  
  12.         <buildCommand>  
  13.             <!-- 项目构建命令的名字 -->  
  14.             <name>org.eclipse.wst.jsdt.core.javascriptValidator</name>  
  15.             <!-- 构建命令初始化时需要传递的参数(一般看到的都是空的) -->  
  16.             <arguments>  
  17.             </arguments>  
  18.         </buildCommand>  
  19.         <buildCommand>  
  20.             <name>org.eclipse.jdt.core.javabuilder</name>  
  21.             <arguments>  
  22.             </arguments>  
  23.         </buildCommand>  
  24.         <buildCommand>  
  25.             <name>org.eclipse.wst.common.project.facet.core.builder</name>  
  26.             <arguments>  
  27.             </arguments>  
  28.         </buildCommand>  
  29.         <buildCommand>  
  30.             <name>org.eclipse.m2e.core.maven2Builder</name>  
  31.             <arguments>  
  32.             </arguments>  
  33.         </buildCommand>  
  34.     </buildSpec>  
  35.     <!-- 项目中用到的一些特性的列表 -->  
  36.     <natures>  
  37.         <!-- 每一个特性的的名字 -->  
  38.         <nature>org.eclipse.jem.workbench.JavaEMFNature</nature>  
  39.         <nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>  
  40.         <nature>org.eclipse.jdt.core.javanature</nature>  
  41.         <nature>org.eclipse.m2e.core.maven2Nature</nature>  
  42.         <nature>org.eclipse.wst.common.project.facet.core.nature</nature>  
  43.         <nature>org.eclipse.wst.jsdt.core.jsNature</nature>  
  44.     </natures>  
  45. </projectDescription></span>  

  使用示例
  Maven项目的配置

一个Maven项目要确保有如下的内容,如果没有,可以手工加上下面的BuildCommand和natures:

 

[html] view plain copy
 
  1. <projectDescription>  
  2.     <buildSpec>  
  3.         <buildCommand>  
  4.             <name>org.eclipse.m2e.core.maven2Builder</name>  
  5.             <arguments>  
  6.             </arguments>  
  7.         </buildCommand>  
  8.     </buildSpec>  
  9.     <natures>  
  10.         <nature>org.eclipse.m2e.core.maven2Nature</nature>  
  11.     </natures>  
  12. </projectDescription>  



 

禁止JavaScript的正确性校验
其实禁止正确性校验是一个不好的习惯,但很多人有这样的需求(唐僧:配置一下exclude路径多好啊,即能进行正确性校验又不会太影响速度),这里给出方案。删除如下的buildCommand即可,也可选择性的删除如下的nature:

[html] view plain copy
 
  1. <buildCommand>  
  2.     <name>org.eclipse.wst.jsdt.core.javascriptValidator</name>  
  3.     <arguments>  
  4.     </arguments>  
  5. </buildCommand>  
  6. <natures>  
  7.     <nature>org.eclipse.wst.jsdt.core.jsNature</nature>  
  8. </natures>  

把一个Java项目变为dynamic web项目
加入如下的buildSpec、nature元素即可:

[html] view plain copy
 
  1. <buildSpec>  
  2.     <buildCommand>  
  3.         <name>org.eclipse.wst.common.project.facet.core.builder</name>  
  4.         <arguments>  
  5.         </arguments>  
  6.     </buildCommand>  
  7. </buildSpec>  
  8. <natures>  
  9.     <nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>  
  10.     <nature>org.eclipse.wst.common.project.facet.core.nature</nature>  
  11. </natures>  



拷贝攻略
.project文件可以从同类型的项目中直接拷贝,但需要修改/projectDescription/name里的项目名称。
参考资料

The project description file

  二、文件:.classpath

  .classpath描述了一个Eclipse项目。

 

[html] view plain copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <classpath>  
  3.     <!-- 含义:src/main/java属于源码,编译后放到target/classes目录下 -->  
  4.     <classpathentry kind="src" output="target/classes" path="src/main/java">  
  5.         <attributes>  
  6.             <attribute name="optional" value="true"/>  
  7.             <attribute name="maven.pomderived" value="true"/>  
  8.         </attributes>  
  9.     </classpathentry>  
  10.     <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">  
  11.         <attributes>  
  12.             <!-- 代表了配置是从POM.xml里来的,受maven管理,非maven项目可以去掉这个 -->  
  13.             <attribute name="maven.pomderived" value="true"/>  
  14.         </attributes>  
  15.     </classpathentry>  
  16.     <!-- 这里的including代表了目录下所有.java文件才会被处理,其他文件一概忽略,不会出现在target/test-classes目录下 -->  
  17.     <classpathentry including="**/*.java" kind="src" output="target/test-classes" path="src/test/java">  
  18.         <attributes>  
  19.             <attribute name="optional" value="true"/>  
  20.             <attribute name="maven.pomderived" value="true"/>  
  21.         </attributes>  
  22.     </classpathentry>  
  23.     <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">  
  24.         <attributes>  
  25.             <attribute name="maven.pomderived" value="true"/>  
  26.         </attributes>  
  27.     </classpathentry>  
  28.     <!-- 这里代表使用标准的JavaSE-1.7 JDK,相比来说如果用default和直接写当前系统中安装的JDK是不推荐的 -->  
  29.     <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">  
  30.         <attributes>  
  31.             <attribute name="maven.pomderived" value="true"/>  
  32.         </attributes>  
  33.     </classpathentry>  
  34.     <!-- 代表了Maven中的dependencies也都放到classpath里 -->  
  35.     <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">  
  36.         <attributes>  
  37.             <attribute name="maven.pomderived" value="true"/>  
  38.             <!-- web工程中把依赖的jar都放到输出的webapp里/WEB-INF/lib下面 -->  
  39.             <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>  
  40.         </attributes>  
  41.     </classpathentry>  
  42.     <!--  -->  
  43.     <classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache-Tomcat v7.0">  
  44.         <attributes>  
  45.             <attribute name="owner.project.facets" value="jst.web"/>  
  46.         </attributes>  
  47.     </classpathentry>  
  48.     <!-- 统一的输出为target/classes -->  
  49.     <classpathentry kind="output" path="target/classes"/>  
  50. </classpath>  

  使用示例
  项目有test/resources或test/java目录,但是不识别为classpath
  酌情加入如下的classpathentry:

[html] view plain copy
 
  1. <classpathentry including="**/*.java" kind="src" output="target/test-classes" path="src/test/java" />  
  2. <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources" />  

  项目是maven工程,但是构建路径貌似怎么也配置不对
Maven是约定优于配置(convention over configuration)的,但是.classpath是配置型的,一般不会出现这种情况,如果出现了,检查maven约定的类路径(比如src/main/java、org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER)中是否有如下的元素:

[html] view plain copy
 
  1. <attributes>  
  2.     <attribute name="maven.pomderived" value="true"/>  
  3. </attributes>  

  Maven的依赖jar文件放不到/WEB-INF/lib里
  确认或加入如下的配置:

[html] view plain copy
 
  1. <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">  
  2.     <attributes>  
  3.         <attribute name="maven.pomderived" value="true"/>  
  4.         <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>  
  5.     </attributes>  
  6. </classpathentry>  

  界面配置方法
  在项目Properties配置界面中,具体位置参考下图:
  .classpath的界面配制方法

 

  可移植的JDK配置


  JDK配置


  拷贝攻略


  .classpath文件可以从同类型的项目中直接拷贝,有些目录没有的话,注意删掉对应的classpathentry,其他基本无需修改,如果有问题再考虑去改。但如果从别人的机器里拷贝,需要关注两点:

   1、Java SDK的配置:如果Java SDK类型设置的是配置界面中的“Alternate JRE”,那么除非自己机器上装了对方机器名字一样的JDK(不是类型或者版本,而是Installed JREs配置中的名字),否则就需要修改JDK的配置。推荐使用配置界面中的“Execution Environment”来配置,避免绑定具体的JDK版本和实现,如<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7" />

  2、如果Web项目使用了Web容器且绑定了project facet,那么就需要考虑Web容器配置的问题了,以Apache-tomcat为例,需要增加<classpathentry kind="con" path="org.eclipse.jst.server.core.Container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache-Tomcat v7.0" />,而"Apache-Tomecat v7.0"字符串需要与Eclipse Preferences里Server/Runtime Environments里的name一列保持一致。


  参考资料
  JDK为什么建议配置成Execution Environment

1. 文件:.jsdtscope
.jsdtscope文件定义了web项目中的源码路径,也就意味着只有web project才会有这个配置。这些源码Eclipse会进行validate(如果validate没有禁用)。这个文件在实际开发中最大的价值在于定义JS文件的例外路径,在配置界面中配置的话挨个选很烦人。


典型内容

[html] view plain copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <classpath>  
  3.     <classpathentry excluding="**/*.min.js|**/bower_components/*|**/custom/*|**/node_modules/*|**/target/**|**/vendor/*" kind="src" path="src/main/webapp"/>  
  4.     <classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>  
  5.     <classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.WebProject">  
  6.         <attributes>  
  7.             <attribute name="hide" value="true"/>  
  8.         </attributes>  
  9.     </classpathentry>  
  10.     <classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>  
  11.     <classpathentry kind="output" path=""/>  
  12. </classpath>  


使用示例
配置JS的例外(一般用于让Eclipse不校验第三方JS文件,避免开启JS校验后Eclipse假死)
在.jsdtscope文件的<classpathentry kind="src" path="src/main/webapp"/>增加excluding属性,写法见“典型内容”一节。


界面配置方法
这一部分在Eclipse不同版本里不一样,Eclipse 4.5及以后版本可以参考下面的配置,4.4.X版本(更早的版本没有考证)只能配置到具体项目中,不能全局配置。若针对具体项目配置,配置界面在项目的properties里的如下位置:

  .classpath的界面配制方法


若全局进行配置,在Window/Preferences/JavaScript/Include Path中进行配置,如下图:

.classpath的界面配制方法


拷贝攻略
.jsdtscope文件可以从同类型的项目中直接拷贝,基本无需修改。


2. 文件:org.eclipse.core.resources.prefs
org.eclipse.core.resources.prefs文件其实就是规定项目内的文件的编码用的。一般来说一个项目里的文件编码需要一致,特别是文件文本内容本身无法指示文件本身编码的(比较绕,XML文件第一行能指示自身编码,CSS也有这个能力但用得不多),尽量不要多种编码同时存在(最好在编码规范中禁止多重编码同时存在的现象发生)。


典型内容

[html] view plain copy
 
  1. eclipse.preferences.version=1  
  2. encoding//src/main/java=UTF-8  
  3. encoding//src/main/resources=UTF-8  
  4. encoding//src/test/java=UTF-8  
  5. encoding//src/test/resources=UTF-8  
  6. encoding/<project>=UTF-8  



使用示例
一般正常的项目打开后,应该看到是统一的编码,如果存在例外,可以对例外的文件进行转码,然后删除这个文件中例外的那一行。


拷贝攻略
org.eclipse.core.resources.prefs文件可以从同类型的项目中直接拷贝,无需修改。


3. 文件:org.eclipse.jdt.core.prefs
org.eclipse.jdt.core.prefs文件指定了一些Java编译的特性,比如Java版本之类的,看文件每一行的key能猜出具体的用处。


典型内容

[html] view plain copy
 
  1. eclipse.preferences.version=1  
  2. org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled  
  3. org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7  
  4. org.eclipse.jdt.core.compiler.compliance=1.7  
  5. org.eclipse.jdt.core.compiler.problem.assertIdentifier=error  
  6. org.eclipse.jdt.core.compiler.problem.enumIdentifier=error  
  7. org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning  
  8. org.eclipse.jdt.core.compiler.source=1.7  



使用示例
如果项目中源文件版本不正确,编译出来的目标版本不对,那么可以在这里进行修改。界面中修改的话,可以参考:


.classpath的界面配制方法


拷贝攻略
org.eclipse.jdt.core.prefs文件可以从同类型的项目中直接拷贝,无需修改。


4. 文件:org.eclipse.m2e.core.prefs
org.eclipse.m2e.core.prefs是一些maven相关的配置。


典型内容

[html] view plain copy
 
  1. eclipse.preferences.version=1  
  2. activeProfiles=dev  
  3. resolveWorkspaceProjects=true  


version=1
使用示例
一般在Maven项目开发时和生产环境中配置不一样,可以在pom.xml中指定不同的profile来实现,Eclipse项目开发时指定profile的话(比如指定名叫dev的profile),就可以配置这个文件的activeProfiles属性。如果在界面中配置,在这里:

  .classpath的界面配制方法


拷贝攻略
org.eclipse.m2e.core.prefs文件可以从同类型的项目中直接拷贝,无需修改。


5. 文件:org.eclipse.wst.common.component
org.eclipse.wst.common.component文件规定了项目怎么组装成一个webapp,这里可以玩很多种组装方式。

 

[html] view plain copy
 
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <project-modules id="moduleCoreId" project-version="1.5.0">  
  3.     <wb-module deploy-name="inkfish-web">  
  4.         <wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>  
  5.         <wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>  
  6.         <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>  
  7.         <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>  
  8.         <property name="context-root" value="inkfish-web"/>  
  9.         <property name="java-output-path" value="/inkfish-web/target/classes"/>  
  10.     </wb-module>  
  11. </project-modules></span>  



典型内容

使用示例
在某些项目中,从远程maven仓库下载zip组件(当然war组件最好,是maven自带支持的)解压并放到target,作为webapp一部分,就可以在这里修改组装webapp的方式。如果在界面中配置,在这里:


.classpath的界面配制方法

         .classpath的界面配制方法
拷贝攻略
org.eclipse.wst.common.component文件不可直接拷贝,如需拷贝注意修改deploy-name、某些value等一些与项目名称相关的信息。


6. 文件:org.eclipse.wst.common.project.facet.core.xml
org.eclipse.wst.common.project.facet.core.xml指示了项目中启用那些facet及facet的版本。


典型内容

 

[html] view plain copy
 
  1. <span style="font-family:SimSun;font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <faceted-project>  
  3.   <runtime name="Apache Tomcat v8.0"/>  
  4.   <fixed facet="wst.jsdt.web"/>  
  5.   <installed facet="wst.jsdt.web" version="1.0"/>  
  6.   <installed facet="java" version="1.7"/>  
  7.   <installed facet="jst.web" version="3.1"/></span>  
  8. </faceted-project>  



使用示例
界面配置在下面的位置:

.classpath的界面配制方法
在实际使用中有时候在Eclipse的facet配置了以后又恢复回去了,总是配不成功,那么就可以直接修改这个文件。常见的比如servlet版本不匹配,那么就可以修改jst.web这个facet的version,如果java版本不对,那么可以修改java这个facet的version。

  拷贝攻略
org.eclipse.wst.common.project.facet.core.xml文件可以从同类型的项目中直接拷贝,基本无需修改。

 9. 文件:org.eclipse.wst.validation.prefs
 使用不多,无研究,略去。

 典型内容

 

 

[html] view plain copy
 
  1. <span style="font-family:SimSun;font-size:18px;">disabled=06target  
  2. eclipse.preferences.version=1</span>  

 

  拷贝攻略
org.eclipse.wst.validation.prefs文件可以在项目间项目直接拷贝,无需修改。

五. 写在后面
有的配置是前后关联的,不是调整一个配置文件就能完事的,比如修改web工程里的Java版本,可能需要修改好几个配置文件。
这篇文章主要介绍了Eclipse项目中常见的自动生成的文件,这些文件都可以通过界面配置来修改,如果大量项目同时修改感觉界面配置比较麻烦,可以学习这些配置文件的内容。普通程序员只需要了解有这么个东西,出了一些界面上配置失灵的情况,可以直接修改文件

   参考文章:http://blog.csdn.net/huaweitman/article/details/52351394

posted @ 2018-04-22 23:08  wwfy  阅读(889)  评论(0编辑  收藏  举报