Eclipse中.setting目錄下文件介紹

原文地址:https://yq.aliyun.com/articles/2597#

Eclipse項目中系統文件介紹

一. 寫在前面

  1. 文章較長,可以直接到感興趣的段落,或者直接關鍵字搜索;
  2. 請原諒作者掌握的編程語言少,這裏只研究Java相關的項目;
  3. 每一個文件僅僅做一個常見內容的簡單介紹,這些知識多數來自於實踐理解和網絡搜索,可能會不全面,更詳細的可以看相關的參考資料。

二. 概述

Eclipse在新建項目的時候會自動生成一些文件。這些文件比如.project、.classpath、.settings目錄下的所有文件等。這些文件是Eclipse項目的元數據,描述了一個Eclipse項目。

通常這些文件裏的內容可以通過配置具體Eclipse項目的Properties來進行修改,而且普通用戶無需知道這些文件具體的用處,但是本着探究問題本質的態度,我們不妨可以瞭解一下這些文件及內容具體是幹什麼用的。當熟悉了這些文件的內容後,往往在項目配置中可以直接查看和修改文件內容,可以起到事半功倍的效果。

三. 項目根目錄下的文件

Eclipse項目根目錄下通常有兩個文件:.project和.classpath,.project是Eclipse項目必須有的文件,而.classpath是Java項目必須有的文件。這兩個文件均是XML格式的文本文件,用普通文本編輯器即可打開。

1. 文件:.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>

使用示例

  1. 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>
    
  2. 禁止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>
    
  3. 把一個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裏的項目名稱。

參考資料

The project description file

2. 文件:.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>

使用示例

  1. 項目有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" />
    
  2. 項目是maven工程,但是構建路徑貌似怎麼也配置不對

    Maven是約定優於配置(convention over configuration)的,但是.classpath是配置型的,一般不會出現這種情況,如果出現了,檢查maven約定的類路徑(比如src/main/java、org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER)中是否有如下的元素:

    <attributes>
        <attribute name="maven.pomderived" value="true"/>
    </attributes>
    
  3. 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>
    
  4. 界面配置方法

在項目Properties配置界面中,具體位置參考下圖:

.classpath的界面配製方法

  1. 可移植的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

四. /.settings目錄下的文件

Eclipse項目.settings目錄下的配置比較雜,各種後綴名的都可以見到,絕大多數是文本文件,格式爲properties(多數以.prefs爲後綴名)或XML(多數以.*、.xml爲文件名)格式的爲主。下面挨個講一些典型的文件。

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>

使用示例

  1. 配置JS的例外(一般用於讓Eclipse不校驗第三方JS文件,避免開啓JS校驗後Eclipse假死)

在.jsdtscope文件的<classpathentry kind="src" path="src/main/webapp"/>增加excluding屬性,寫法見“典型內容”一節。

  1. 界面配置方法

這一部分在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的界面配製方法

拷貝攻略

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文件可以從同類型的項目中直接拷貝,基本無需修改。

7. 文件:org.eclipse.wst.jsdt.ui.superType.container

使用不多,無研究,略去。

典型內容

org.eclipse.wst.jsdt.launching.baseBrowserLibrary

或者也見過

org.eclipse.wst.jsdt.launching.JRE_CONTAINER

拷貝攻略

org.eclipse.wst.jsdt.ui.superType.container文件可以在項目間項目直接拷貝,無需修改。

8. 文件:org.eclipse.wst.jsdt.ui.superType.name

使用不多,無研究,略去。

典型內容

Window

或者也見過

Global

拷貝攻略

org.eclipse.wst.jsdt.ui.superType.name文件可以在項目間項目直接拷貝,無需修改。

9. 文件:org.eclipse.wst.validation.prefs

使用不多,無研究,略去。

典型內容

disabled=06target
eclipse.preferences.version=1

拷貝攻略

org.eclipse.wst.validation.prefs文件可以在項目間項目直接拷貝,無需修改。

五. 寫在後面

有的配置是前後關聯的,不是調整一個配置文件就能完事的,比如修改web工程裏的Java版本,可能需要修改好幾個配置文件。

這篇文章主要介紹了Eclipse項目中常見的自動生成的文件,這些文件都可以通過界面配置來修改,如果大量項目同時修改感覺界面配置比較麻煩,或者純粹是極客類型的,可以學習這些配置文件的內容。普通程序員只需要瞭解有這麼個東西,出了一些界面上配置失靈的情況,可以直接修改文件。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章