如何将文件夹作为库添加到java构建路径中,其中包含多个jar或条目?

3 eclipse eclipse-plugin

首先,我要非常感谢"富有卖家"来解决我的查询,以编程方式更改eclipse java构建路径中的条目顺序.

我想将我的Library文件夹添加到java构建路径中,其中包含几个jar.它应该像classpath容器一样.我尝试使用IClasspathContainer但未能实现.

请帮我....

提前致谢.

俞拉吉.

Ric*_*ler 6

您应该通过实现org.eclipse.jdt.core.classpath.ContainerInitializerextension点来定义新的ClasspathContainer .例如,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>
Run Code Online (Sandbox Code Playgroud)

引用的JUnitContainerInitializer创建并初始化两个JUnit类路径容器.

按照这种方法,您可以实现"文件夹容器".有一篇developerWorks文章介绍了如何执行此操作(您需要注册才能查看该文章).


更新:可以在不注册扩展点的情况下定义容器,但是您必须知道,如果文件夹的内容发生更改,您将无法访问生命周期方法来刷新容器.通过扩展点来做它会好得多.

下面的示例将项目的"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());
}
Run Code Online (Sandbox Code Playgroud)