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

引言javascript

  今天在建立java項目的時候遇到了不少的錯誤,在解決的過程當中遇到了一些根本不知道什麼做用的文件,而後按照網上的一些作法能夠將問題解決,可是這也說明咱們在學習的時候不少基礎和細節的地方是咱們薄弱的地方,咱們老是對於常常寫代碼的那幾個文件比較熟悉,這樣一旦報錯咱們就無從下手啊。 下面介紹幾個文件。html

  1、文件.mymetadata文件 java

  一、部署項目用的,把項目部署到tomcat服務器上時,若是沒有這個文件,用myeclipse/Eclipse部署是不能部署到關聯的tomcat服務器。 node

 

[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>    

  二、在MyEclipse/Eclipse更改項目發佈到Tomcat的文件夾 
   (1)、在本身的workspace中找到當前使用的項目,找到文件.mymetadata,打開後就會看見標籤context- root=」/example」(example是本身應用的名稱),能夠將其改成別的名稱。 
   (2)、選擇:工程右鍵 --》Properties --》Myeclipse --》 Web 
   [Web-root folder]:/WebRoot 
   [Web Context-root]:/ROOT,修改context-root對應的值便可。(默認值:當前項目名稱) 
  2、文件:.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項目的配置web

一個Maven項目要確保有以下的內容,若是沒有,能夠手工加上下面的BuildCommand和natures:tomcat

 

[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元素便可:app

[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裏的項目名稱。
參考資料eclipse

The project description file

  2、文件:.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,其餘基本無需修改,若是有問題再考慮去改。但若是從別人的機器裏拷貝,須要關注兩點:

   一、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" />

  二、若是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

相關文章
相關標籤/搜索