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

标签 eclipse eclipse-plugin

首先,我要非常感谢“Rich seller”解决了我以编程方式更改 eclipse java 构建路径中的条目顺序的查询。

我想将我的 Library 文件夹添加到 java 构建路径,其中有几个 jar。它的行为应该像类路径容器。 我尝试使用 IClasspathContainer 但未能实现。

请帮助我......

提前致谢。

YUVRAJ。

最佳答案

您应该通过实现 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>

引用的 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());
}

关于eclipse - 如何将文件夹添加到 java 构建路径作为库,其中包含多个 jar 或条目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1284482/

相关文章:

eclipse - 如何为HDP 2.2安装eclipse插件

eclipse - Eclipse 的 Youtrack 插件?

java - 单击按钮后显示 TextView

java - 如何在 Eclipse 中使用 UML 符号可视化 Java 代码?

java - 如何使用 Eclipse 从相同代码自动构建不同的 JAR?

java - Eclipse Luna 中的 Hibernate 配置

python - 如何在 Eclipse/Python/Appengine 中使用持久本地 NDB 数据进行调试?

java - 如何在java项目中使用可运行的jar,将其导入libs文件夹

java - 如何询问用户是否要始终显示欢迎页面?

java - 默认构造函数无法处理隐式 super 构造函数抛出的异常类型 SocketException