專案本質

專案本質允許外掛程式將專案標示為特定種類的專案。例如,Java Development Tools (JDT) 使用 "Java nature" 來新增 Java 特定的操作方式至專案。

專案本質根據每一個專案來安裝,它會在新增至專案時自動配置,而在除去時解除配 置。一個專案可以有多個本質。

若要實作您自己的本質,您需要定義一個延伸項目及提供一個實作 IProjectNature 的類別。

定義本質

org.eclipse.core.resources.natures 延伸點用來新增專案本質定義。 下列標記為假定的 com.example.natures 外掛程式新增本質。

<extension
    point="org.eclipse.core.resources.natures"
    id="mynature"
    name="My Nature">
    <runtime>
        <run class="com.example.natures.MyNature">
        </run>
    </runtime>
</extension>

延伸項目中所識別的類別必須實作平台介面 IProjectNature 。 這個類別實作外掛程式特定操作方式,當配置本質時,使特定本質資訊與專案產生關 聯。

public class MyNature implements IProjectNature {

    private IProject project;

    public void configure() throws CoreException {
        // 新增專案特定本質的資訊,
        // 例如新增建立器
        // 到專案的建置規格。
    }
    public void deconfigure() throws CoreException {
        // 在此處除去特定本質的資訊。
    }
    public IProject getProject() {
        return project;
    }
    public void setProject(IProject value) {
        project = value;
    }
}

當從專案新增及除去本質時,平台傳送 configure()deconfigure() 方法。 您可以實作 configure() 方法來將建 立器新增至專案,如建立器中 所述。

建立本質與專案的關聯性

一旦定義本質及實作其類別後,您必須對專案指定本質。下列片段將我們的新本質指 派給指定的專案。

try {
    IProjectDescription description = project.getDescription();
    String[] natures = description.getNatureIds();
    String[] newNatures = new String[natures.length + 1];
    System.arraycopy(natures, 0, newNatures, 0, natures.length);
    newNatures[natures.length] = "com.example.natures.mynature";
    description.setNatureIds(newNatures);
    project.setDescription(description, null);
} catch (CoreException e) {
    // 有問題
}

本質使用的識別碼為本質延伸項目的完整名稱(外掛程式 ID + 延伸項目 ID)。

一般而言,本質在建立後會新增至專案。 通常您提供一個攫取專案專用資訊的 自訂新專案精靈,然後指定您的本質給它。