1. 程式人生 > >myeclipse及Eclipse中.classpath、.project、.settings、.mymetadata(myeclipse特有)介紹

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

  引言

  今天在建立java專案的時候遇到了很多的錯誤,在解決的過程中遇到了一些根本不知道什麼作用的檔案,然後按照網上的一些做法可以將問題解決,但是這也說明我們在學習的時候很多基礎和細節的地方是我們薄弱的地方,我們總是對於經常寫程式碼的那幾個檔案比較熟悉,這樣一旦報錯我們就無從下手啊。 下面介紹幾個檔案。

  一、檔案.mymetadata檔案 

  1、部署專案用的,把專案部署到tomcat伺服器上時,如果沒有這個檔案,用myeclipse/Eclipse部署是不能部署到關聯的tomcat伺服器。 

<?xml version=”1.0” encoding=”UTF-8”?>  
<!--  
type : 工程型別  
name : 工程名稱  
id : 工程在工作空間內的唯一標識  
context-root : 網站根路徑  
j2ee-spec: J2EE標準  
archive : 打包後war檔案  
-->  
<project-module  
type=”WEB”  
name=”PheebsKM”  
id=”myeclipse.1152954865843”  
context-root=”/PheebsKM”  
j2ee-spec=”1.4”  
archive=”PheebsKM.war”>  
  
<attributes>  
  <!-- value : Web根目錄名稱 -->  
    <attribute name=”webrootdir” value=”WebRoot” />  
</attributes>  
</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專案。
  典型內容

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <!-- name裡的內容代表專案名字,對應了Eclipse專案的名稱,不是Maven的finalName -->
    <name>demo</name>
    <!-- 專案的註釋 -->
    <comment></comment>
    <!-- 引用的專案的名字 -->
    <projects>
    </projects>
    <!-- 有序的列表,定義了一系列的構建命令(buildCommand) -->
    <buildSpec>
        <buildCommand>
            <!-- 專案構建命令的名字 -->
            <name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
            <!-- 構建命令初始化時需要傳遞的引數(一般看到的都是空的) -->
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.jdt.core.javabuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.wst.common.project.facet.core.builder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.m2e.core.maven2Builder</name>
            <arguments>
            </arguments>
        </buildCommand>
    </buildSpec>
    <!-- 專案中用到的一些特性的列表 -->
    <natures>
        <!-- 每一個特性的的名字 -->
        <nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
        <nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
        <nature>org.eclipse.jdt.core.javanature</nature>
        <nature>org.eclipse.m2e.core.maven2Nature</nature>
        <nature>org.eclipse.wst.common.project.facet.core.nature</nature>
        <nature>org.eclipse.wst.jsdt.core.jsNature</nature>
    </natures>
</projectDescription>
  使用示例
  Maven專案的配置

一個Maven專案要確保有如下的內容,如果沒有,可以手工加上下面的BuildCommand和natures:

<projectDescription>
    <buildSpec>
        <buildCommand>
            <name>org.eclipse.m2e.core.maven2Builder</name>
            <arguments>
            </arguments>
        </buildCommand>
    </buildSpec>
    <natures>
        <nature>org.eclipse.m2e.core.maven2Nature</nature>
    </natures>
</projectDescription>


禁止JavaScript的正確性校驗
其實禁止正確性校驗是一個不好的習慣,但很多人有這樣的需求(唐僧:配置一下exclude路徑多好啊,即能進行正確性校驗又不會太影響速度),這裡給出方案。刪除如下的buildCommand即可,也可選擇性的刪除如下的nature:
<buildCommand>
    <name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
    <arguments>
    </arguments>
</buildCommand>
<natures>
    <nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
把一個Java專案變為dynamic web專案
加入如下的buildSpec、nature元素即可:
<buildSpec>
    <buildCommand>
        <name>org.eclipse.wst.common.project.facet.core.builder</name>
        <arguments>
        </arguments>
    </buildCommand>
</buildSpec>
<natures>
    <nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
    <nature>org.eclipse.wst.common.project.facet.core.nature</nature>
</natures>


拷貝攻略
.project檔案可以從同類型的專案中直接拷貝,但需要修改/projectDescription/name裡的專案名稱。
參考資料

  二、檔案:.classpath

  .classpath描述了一個Eclipse專案。

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <!-- 含義:src/main/java屬於原始碼,編譯後放到target/classes目錄下 -->
    <classpathentry kind="src" output="target/classes" path="src/main/java">
        <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
        <attributes>
            <!-- 代表了配置是從POM.xml裡來的,受maven管理,非maven專案可以去掉這個 -->
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <!-- 這裡的including代表了目錄下所有.java檔案才會被處理,其他檔案一概忽略,不會出現在target/test-classes目錄下 -->
    <classpathentry including="**/*.java" kind="src" output="target/test-classes" path="src/test/java">
        <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <!-- 這裡代表使用標準的JavaSE-1.7 JDK,相比來說如果用default和直接寫當前系統中安裝的JDK是不推薦的 -->
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <!-- 代表了Maven中的dependencies也都放到classpath裡 -->
    <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
            <!-- web工程中把依賴的jar都放到輸出的webapp裡/WEB-INF/lib下面 -->
            <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
        </attributes>
    </classpathentry>
    <!--  -->
    <classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache-Tomcat v7.0">
        <attributes>
            <attribute name="owner.project.facets" value="jst.web"/>
        </attributes>
    </classpathentry>
    <!-- 統一的輸出為target/classes -->
    <classpathentry kind="output" path="target/classes"/>
</classpath>
  使用示例
  專案有test/resources或test/java目錄,但是不識別為classpath
  酌情加入如下的classpathentry:
<classpathentry including="**/*.java" kind="src" output="target/test-classes" path="src/test/java" />
<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)中是否有如下的元素:
<attributes>
    <attribute name="maven.pomderived" value="true"/>
</attributes>
  Maven的依賴jar檔案放不到/WEB-INF/lib裡
  確認或加入如下的配置:
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
    <attributes>
        <attribute name="maven.pomderived" value="true"/>
        <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
    </attributes>
</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檔案的例外路徑,在配置介面中配置的話挨個選很煩人。


典型內容

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry excluding="**/*.min.js|**/bower_components/*|**/custom/*|**/node_modules/*|**/target/**|**/vendor/*" kind="src" path="src/main/webapp"/>
    <classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
    <classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.WebProject">
        <attributes>
            <attribute name="hide" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
    <classpathentry kind="output" path=""/>
</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也有這個能力但用得不多),儘量不要多種編碼同時存在(最好在編碼規範中禁止多重編碼同時存在的現象發生)。


典型內容
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8


使用示例
一般正常的專案開啟後,應該看到是統一的編碼,如果存在例外,可以對例外的檔案進行轉碼,然後刪除這個檔案中例外的那一行。


拷貝攻略
org.eclipse.core.resources.prefs檔案可以從同類型的專案中直接拷貝,無需修改。


3. 檔案:org.eclipse.jdt.core.prefs
org.eclipse.jdt.core.prefs檔案指定了一些Java編譯的特性,比如Java版本之類的,看檔案每一行的key能猜出具體的用處。


典型內容
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
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相關的配置。


典型內容
eclipse.preferences.version=1
activeProfiles=dev
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,這裡可以玩很多種組裝方式。

<?xml version="1.0" encoding="UTF-8"?>
<project-modules id="moduleCoreId" project-version="1.5.0">
    <wb-module deploy-name="inkfish-web">
        <wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
        <wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
        <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
        <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
        <property name="context-root" value="inkfish-web"/>
        <property name="java-output-path" value="/inkfish-web/target/classes"/>
    </wb-module>
</project-modules>


典型內容

使用示例
在某些專案中,從遠端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的版本。



典型內容
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
  <runtime name="Apache Tomcat v8.0"/>
  <fixed facet="wst.jsdt.web"/>
  <installed facet="wst.jsdt.web" version="1.0"/>
  <installed facet="java" version="1.7"/>
  <installed facet="jst.web" version="3.1"/>
</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
 使用不多,無研究,略去。

 典型內容
disabled=06target
eclipse.preferences.version=1

  拷貝攻略
org.eclipse.wst.validation.prefs檔案可以在專案間專案直接拷貝,無需修改。

五. 寫在後面
有的配置是前後關聯的,不是調整一個配置檔案就能完事的,比如修改web工程裡的Java版本,可能需要修改好幾個配置檔案。
這篇文章主要介紹了Eclipse專案中常見的自動生成的檔案,這些檔案都可以通過介面配置來修改,如果大量專案同時修改感覺介面配置比較麻煩,可以學習這些配置檔案的內容。普通程式設計師只需要瞭解有這麼個東西,出了一些介面上配置失靈的情況,可以直接修改檔案