如何以编程方式更改eclipse java build path的“order and export tab”中的条目顺序?

时间:2009-08-16 12:20:50

标签: eclipse eclipse-plugin

我需要在eclipse的java构建路径中更改两个jar文件的顺序。 有一些操作junit3.jar将在junit4.jar之前设置,但我无法找到任何线索。

1 个答案:

答案 0 :(得分:0)

您可以获取项目的原始类路径,然后根据其类型和路径查找每个Junit条目,如果它们处于“错误的顺序”,则可以修改原始类路径并将修改后的路径设置为该项目。

下面的代码段概述了如何完成,请注意此示例中没有异常处理:

//get the project by name, you probably want to use another method to 
//obtain it
IProject project = ResourcesPlugin.getWorkspace().getRoot()
        .getProject("foo");
IJavaProject javaProject = JavaCore.create(project);

IClasspathEntry[] entries = javaProject.getRawClasspath();

// find the JUnit 3 and Junit 4 entry index
int junit3Index = -1;
int junit4Index = -1;
for (int i = 0; i < entries.length; i++) {
    if (entries[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
        if (entries[i].getPath().equals(
                JUnitContainerInitializer.JUNIT3_PATH)) {
            junit3Index = i;
        } else if (entries[i].getPath().equals(
                JUnitContainerInitializer.JUNIT4_PATH)) {
            junit4Index = i;
        }
    }
}

if (junit3Index != -1 && junit4Index != -1
        && junit3Index > junit4Index) {
    // swap the two entries
    IClasspathEntry temp = entries[junit4Index];
    entries[junit4Index] = entries[junit3Index];
    entries[junit3Index] = temp;

    //update the project with the modified path
    javaProject.setRawClasspath(entries, new NullProgressMonitor());
}