프로젝트 속성은 플러그인이 프로젝트를 특정 유형의 프로젝트로 태그화할 수 있도록 합니다. 예를 들어 JDT(Java Development Tool)는 "Java 속성"을 사용하여 프로젝트에 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 { // Add nature-specific information // for the project, such as adding a builder // to a project's build spec. } public void deconfigure() throws CoreException { // Remove the nature-specific information here. } 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) { // Something went wrong }
참고: 네이처 ID는 네이처 확장의 완전한 ID입니다. 확장의 완전한 ID는 플러그인 ID를 plugin.xml 파일의 단순 확장자 ID와 결합하여 작성됩니다. 예를 들어 플러그인 "com.example.natures"의 단순 확장 ID "mynature"가 있는 네이처는 "com.example.natures.mynature"의 이름을 갖습니다.
프로젝트 설명을 프로젝트에 설정할 때까지 속성은 실제로 프로젝트에 지정(구성)되지 않습니다. 속성에 사용되는 ID는 속성 확장의 완전한 이름(플러그인 ID + 확장 ID)입니다.
제한조건을 사용하여 속성을 정의한 경우 작업공간 API를 사용하여 새 속성의 유효성을 검증할 수 있습니다. 예를 들어, 다음 전제조건을 사용하여 속성을 정의하는 것으로 가정합니다.
<extension point="org.eclipse.core.resources.natures" id="myOtherNature" name="My Other Nature"> <runtime> <run class="com.example.natures.MyOtherNature"> </run> </runtime> <requires-nature id="com.example.natures.mynature"/> </extension>
프로젝트에 대한 첫 번째 속성이 없으면 새 속성은 유효하지 않습니다. 플러그인의 디자인에 따라 전제조건 속성이 설치되었는지 확인하거나 전제조건 속성을 추가할 수 있습니다. 어느 방법이든 작업공간 API를 사용하여 프로젝트 속성의 제안된 조합의 유효성을 확인할 수 있습니다.
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.myOtherNature"; IStatus status = workspace.validateNatureSet(natures); // check the status and decide what to do if (status.getCode() == IStatus.OK) { description.setNatureIds(newNatures); project.setDescription(description, null); } else { // raise a user error ... } } catch (CoreException e) { // Something went wrong }
ID로 속성에 대해 작업하는 것 외에 속성, 속성의 제한조건 및 레이블을 설명하는 설명자(IProjectNatureDescriptor)를 얻을 수 있습니다. 설명자의 특정 속성을 조회하거나, 작업공간에서 설명자를 가져올 수 있습니다. 다음 스니펫에서는 새 속성에 대한 프로젝트 속성 설명자를 가져옵니다.
IProjectNatureDescriptor descriptor = workspace.getNatureDescriptor("com.example.natures.myOtherNature");
설치된 모든 속성에 대해 설명자 배열을 가져올 수도 있습니다.
IProjectNatureDescriptor[] descriptors = workspace.getNatureDescriptors();