2009-08-16 2 views
2

먼저 "Rich 판매자"에게 eclipse java 빌드 경로에서 프로그래밍 방식으로 항목 순서를 변경하는 것에 대한 나의 쿼리를 해결하는 것에 대해 감사 드리고 싶습니다.여러 개의 jar 또는 항목이있는 라이브러리로 java 빌드 경로에 폴더를 추가하는 방법은 무엇입니까?

내 라이브러리 폴더를 여러 개의 병이있는 Java 빌드 경로에 추가하려고합니다. 클래스 패스 컨테이너처럼 동작해야합니다. IClasspathContainer를 사용해 보았지만 구현에 실패했습니다.

도와주세요 ....

미리 감사드립니다.

유 브라 지.

답변

5

org.eclipse.jdt.core.classpath.ContainerInitializerextension 포인트를 구현하여 새 클래스 패스 컨테이너를 정의해야합니다. 예를 들어, org.eclipse.jdt.junit 플러그인의 plugin.xml에

<extension 
    point="org.eclipse.jdt.core.classpathContainerInitializer"> 
    <classpathContainerInitializer 
     class="org.eclipse.jdt.internal.junit.buildpath.JUnitContainerInitializer" 
     id="org.eclipse.jdt.junit.JUNIT_CONTAINER"> 
    </classpathContainerInitializer> 
</extension> 

참조 된 JUnitContainerInitializer가 생성에서 다음을 정의하고이 개 JUnit을 클래스 패스 컨테이너를 초기화한다.

이 방법을 따르면 "폴더 컨테이너"를 구현할 수 있습니다. 이 작업을 수행하는 방법을 보여주는 DeveloperWorks article이 있습니다 (기사를 보려면 등록해야합니다).


업데이트 : 그것은 확장 점을 회원 가입없이 컨테이너를 정의 할 수 있습니다,하지만 당신은 당신이 용기를 새로 라이프 사이클 방법에 액세스 할 수 있습니다 알고 있어야 할 경우 폴더의 내용이 변경. 확장 점을 통해 수행하는 것이 훨씬 낫습니다.

아래 예제에서는 프로젝트의 "lib"폴더를 사용자 지정 컨테이너로 추가하고 해당 폴더에있는 모든 jar 파일을 컨테이너 내의 항목으로 추가합니다. 소스 연결을 관리하지 않습니다.

final String description = "My container"; 

IProject project = ResourcesPlugin.getWorkspace().getRoot() 
     .getProject("foo"); 

//get the lib folder, TODO check if it exists! 
final IFolder folder = project.getFolder("lib"); 

//define a unique path for the custom container 
final IPath containerPath = new Path(
     "my.custom.CLASSPATH_CONTAINER").append(project 
     .getFullPath()); 

IJavaProject javaProject = JavaCore.create(project); 

//create a container that lists all jars in the lib folder 
IClasspathContainer customContainer = new IClasspathContainer() { 
    public IClasspathEntry[] getClasspathEntries() { 
     List<IClasspathEntry> entryList = new ArrayList<IClasspathEntry>(); 
     try { 
      // add any members that are files with the jar extension 
      IResource[] members = folder.members(); 
      for (IResource resource : members) { 
       if (IFile.class.isAssignableFrom(resource 
         .getClass())) { 
        if (resource.getName().endsWith(".jar")) { 
         entryList.add(JavaCore.newLibraryEntry(
           new Path(resource.getFullPath() 
             .toOSString()), null, 
           new Path("/"))); 
        } 
       } 
      } 
     } catch (CoreException e) { 
      // TODO handle the exception 
      e.printStackTrace(); 
     } 
     // convert the list to an array and return it 
     IClasspathEntry[] entryArray = new IClasspathEntry[entryList 
       .size()]; 
     return entryList.toArray(entryArray); 
    } 

    public String getDescription() { 
     return description; 
    } 

    public int getKind() { 
     return IClasspathEntry.CPE_CONTAINER; 
    } 

    public IPath getPath() { 
     return containerPath; 
    } 

    @Override 
    public String toString() { 
     return getDescription(); 
    } 
}; 

//register the custom container so when we add its path it is discovered 
JavaCore.setClasspathContainer(containerPath, 
     new IJavaProject[] { javaProject }, 
     new IClasspathContainer[] { customContainer }, null); 

IClasspathEntry[] entries = javaProject.getRawClasspath(); 

//check if the container is already on the path 
boolean hasCustomContainer = false; 

for (int i = 0; i < entries.length; i++) { 
    if (entries[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER 
      && entries[i].getPath().equals(containerPath)) { 
     hasCustomContainer = true; 
    } 
} 
if (!hasCustomContainer) { 
    IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1]; 

    System.arraycopy(entries, 0, newEntries, 0, entries.length); 

    // add a new entry using the path to the container 
    newEntries[entries.length] = JavaCore 
      .newContainerEntry(customContainer.getPath()); 

    javaProject.setRawClasspath(newEntries, 
      new NullProgressMonitor()); 
} 
+0

사실 저는 plugin.xml을 사용하지 않고 구현해야합니다. 자바 코드만으로 구현해야합니다 ..... 라이브러리 폴더 s 자바 빌드 경로의 라이브러리 탭에서 JRE 라이브러리와 같은 ICON을 가져야합니다. 라이브러리 이름을 확장하면 jar 파일이 표시됩니다. –

+0

잘 모르겠습니다. 코드를 Eclipse 내부에 액세스 할 수 있도록 플러그인에 구현해야합니다. 그렇지 않으면 어떻게 구현합니까? –

+0

감사합니다. Rich, 나는 내 클라이언트의 요구 사항을 얻지 못했고 이제는 plugin.xml을 사용하도록 말할 수 있습니다. 다시 한 번 감사드립니다. 계속 묻습니다. 좋은 밤. –

관련 문제